C++ thread can't find function as parameter(linker) [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I'm building multiplayer ping pong. So far the movement of the players are good. I am now focusing on the ball movement. So I add tick function which ticks the ball.
The error line is inside main.cpp: (3rd line)(The other 2 are working fine)
client_network_thread_sendto = thread(sendto_network_loop, &running, &me);
client_network_thread_recvfrom = thread(recvfrom_network_loop, &running, &opponent);
client_tick_thread = thread(tick_loop, &me, &opponent, &ball);
The linker:
make
clang++-5.0 -Wall -std=c++11 -Iinclude -g -c src/network.cpp -o build/network.o
clang++-5.0 -Wall -std=c++11 -Iinclude -g -c src/server.cpp -o build/server.o
clang++-5.0 -Wall -std=c++11 -Iinclude -g -c src/geometry.cpp -o build/geometry.o
clang++-5.0 -Wall -std=c++11 -Iinclude -g -c src/player.cpp -o build/player.o
clang++-5.0 -Wall -std=c++11 -Iinclude -g -c src/client.cpp -o build/client.o
clang++-5.0 -Wall -std=c++11 -Iinclude -g -c src/defaults.cpp -o build/defaults.o
clang++-5.0 -Wall -std=c++11 -Iinclude -g -c src/main.cpp -o build/main.o
clang++-5.0 -lglut -lGL -lGLU -lpthread build/network.o build/server.o build/geometry.o build/player.o build/client.o build/defaults.o build/main.o -o bin/main.out
build/main.o: In function `main':
/home/shlomi/Desktop/CPP_OpenGL_Pong_Multiplayer/src/main.cpp:199: undefined reference to `tick_loop(player*, player*, moving_circle*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Makefile:31: recipe for target 'link' failed
make: *** [link] Error 1
client.h:
void sendto_network_loop(bool* running, player* me);
void recvfrom_network_loop(bool* running, player* opponent);
void tick_loop(player* me, player* opponent, moving_circle* ball);
client.cpp: (Implementations)
void tick_loop(player* me, player* opponent, moving_circle* ball) {
ball->tick();
}
I tried to clean and rebuild, searching the same question on the internet, and asking friends.

This is a linker error. When you try to create your executable from main.cpp you must not be including client.cpp. Show your build commands maybe?

Related

make error: “g++: error: bin/main.o: No such file or directory”

I'm trying to compile a program I wrote, and the structure of the directory is the following:
ctrace > bin, include, src, makefile
bin>
include > Agent.h, Graph.h, Session.h, Tree.h
src> Agent.cpp, ContactTracer.cpp, Graph.cpp, main.cpp, Session.cpp, Tree.cpp, Virus.cpp
The error I'm getting is the following:
g++: error: bin/main.o: No such file or directory
Makefile:5: recipe for target 'cTrace' failed
make: *** [cTrace] Error 1
and this is my make file:
# All Targets
all: cTrace
cTrace: bin/Session.o bin/Agent.o bin/Graph.o bin/Tree.o bin/Virus.o bin/ContactTracer.o
#echo 'Building target: cTrace'
g++ bin/Session.o bin/Agent.o bin/Graph.o bin/Tree.o bin/Virus.o bin/ContactTracer.o bin/main.o -o cTrace
#echo 'target cTrace built successfully'
bin/main.o: src/main.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/main.o src/main.cpp
bin/Session.o: src/Session.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude -o bin/Session.o src/Session.cpp
bin/Agent.o: src/Agent.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude src/Agent.cpp -o bin/Agent.o
bin/Graph.o: src/Graph.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude src/Graph.cpp -o bin/Graph.o
bin/Tree.o: src/Tree.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude src/Tree.cpp -o bin/Tree.o
bin/Virus.o: src/Virus.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude src/Virus.cpp -o bin/Virus.o
bin/ContactTracer.o: src/ContactTracer.cpp
g++ -g -Wall -Weffc++ -std=c++11 -c -Iinclude src/ContactTracer.cpp -o bin/ContactTracer.o
clean:
rm -rf bin/*.o cTrace
What could be the issue? all directories exist, my guess is that main.o is never generated but I can't see why as I don't fully understand all the flags in the makefile. Any tips on how to write a better makefile would be appreciated.
On a side note, I was able to run this exact same project using a CMake generated by CLion.
Thanks in advance.
You are missing bin/main.o as dependency for cTrace target. That's why make does not compile it, and resulting object file is missing. Just add bin/main.o to your list of dependencies in line 4.
Problem seems to be that bin/main.o should be mentioned as a dependency of cTrace
cTrace: bin/Session.o bin/Agent.o bin/Graph.o bin/Tree.o bin/Virus.o bin/ContactTracer.o bin/main.o
At present building cTrace doesn't cause the make file to try and build bin/main.o and so you get an error about a missing file.

How to compile with cutil?

I am using a code from someone else. When compiling it I got the following error: " référence indéfinie vers « CUtil::CUtil() ".
I guess I am missing a package or library? Or is it a problem with my compiler? (gcc version 5.4.0).
I am on ubuntu.
Any idea how I should procees to be able to compile?
Edit: it is a linkage issue. I have all the files I need in mu directory and here is the script to compile:
g++ -c -Wall -fPIC GeoConvert.cpp
g++ -c -Wall -fPIC GlobalFunctions.cpp
g++ -c -Wall -fPIC Util.cpp
g++ -c -Wall -fPIC TbwInput.cpp
g++ -shared -o libTbwInput.so GeoConvert.o GlobalFunctions.o Util.o TbwInput.o
g++ -c -Wall -fPIC Atmosphere.cpp
g++ -c -Wall -fPIC GlobalFunctions.cpp
g++ -c -Wall -fPIC Interface.cpp
g++ -c -Wall -fPIC WriteInputFile.cpp
g++ -shared -o libInterface.so Atmosphere.o GlobalFunctions.o Interface.o WriteInputFile.o
g++ -c -Wall -fPIC CalllTBWinterface.cpp
#g++ -c -Wall -fPIC stdafx.cpp
g++ -shared -o libTBWinterfaceWrapper.so CalllTBWinterface.o #stdafx.o
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:'/home/bomble/TIARE/HasanKhan/MDO_GITHUB/All Libraries/1.TBWinterfaceWrapper/'
g++ -o TBWinterfaceWrapper TBWinterfaceWrapper.cpp -L'/home/bomble/TIARE/HasanKhan/MDO_GITHUB/All Libraries/1.TBWinterfaceWrapper' -lTbwInput -lInterface -lTBWinterfaceWrapper
All but the last step go fine. And then I have error about things not find even if they are in the cpp compiled at the beginning....

Running HTTP server example from Boost Asio

I'm getting errors when trying to run the HTTP server example that comes with the source of the boost library, under the path: boost_1_59_0/libs/asio/example/cpp11/http/server/.
I already ran this following commands in the boost_1_59_0 directory:
$ ./bootstrap.sh
$ sudo ./bjam install
$ sudo ./b2 install
After installing all targets, i tried to compile the main.cpp and the server.cpp with this command: g++ -std=c++0x -o main main.cpp -I "/home/user/Desktop/boost_1_59_0" -L "/home/user/Desktop/boost_1_59_0/libs/" -lboost_system.
Any suggestion on how to compile this server example?
I linked all files from the boost_1_59_0/libs/asio/example/cpp11/http/server/ folder after the main.cpp, as #Richard Hodges suggested. It still didn't work, i got errors concerning lpthread, so i added it to the compiling options. The program compiled but it failed the execution, returning an error saying that it didn't find the library libboost_system.so.1.59.0. I tried linking the folders with -L /path/to/library but it didn't work.
Solution:
My compilation command:
g++ -std=gnu++0x -o main main.cpp server.cpp connection.cpp connection_manager.cpp reply.cpp mime_types.cpp request_handler.cpp request_parser.cpp -I "/home/user/Desktop/boost_1_59_0" -lboost_system -lpthread
I solved it with this commands:
$ LD_LIBRARY_PATH="/usr/local/lib/"
$ sudo ldconfig
And then I just ran the executable and it worked!
Here's a simple makefile I just concocted that works:
all:server
CPPFLAGS+=-std=c++11 -Wall -pedantic
CPPFLAGS+=-g -O2
CPPFLAGS+=-pthread
LDFLAGS+=-lboost_system
%.o:%.cpp
$(CXX) $(CPPFLAGS) $^ -c -o $#
server:$(patsubst %.cpp,%.o,$(wildcard *.cpp))
$(CXX) $(CPPFLAGS) $^ -o $# $(LDFLAGS)
It runs make:
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection.cpp -c -o connection.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection_manager.cpp -c -o connection_manager.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread main.cpp -c -o main.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread mime_types.cpp -c -o mime_types.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread reply.cpp -c -o reply.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread request_handler.cpp -c -o request_handler.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread request_parser.cpp -c -o request_parser.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread server.cpp -c -o server.o
g++ -std=c++11 -Wall -pedantic -g -O2 -pthread connection.o connection_manager.o main.o mime_types.o reply.o request_handler.o request_parser.o server.o -o server -lboost_system
And the test program runs:
$ ./server 0.0.0.0 9889 . &
$ GET http://localhost:9889/main.cpp > main.cpp.0
Check the files
$ md5sum main.cpp*
be5dc1c26b5942101a7895de6baedcee main.cpp
be5dc1c26b5942101a7895de6baedcee main.cpp.0
Don't forget to kill the server when you're done

C++ linking error when linking postgresql

When compiling my code I run into an issue as follows:
io.cpp:21: undefined reference to `PQconnectdb'
as well as all other instances of missing postgres function calls occurring in my code. Obviously this is a linking problem, I'm just not sure what the link issue is.
I'm compiling with the following:
mpiCC -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ decisioning_mpi.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ io.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ calculations.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ rules.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ Instrument.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ Backtest_Parameter_CPO.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ Backtest_Trade_CPO.cpp
g++ -c -O2 -g -Wall -Werror -I /usr/include/postgresql/ Data_Bar.cpp
mpiCC -o decisioning_mpi -O2 -g -Wall -Werror -L/usr/lib -lm -lpq decisioning_mpi.o
io.o calculations.o rules.o Instrument.o Backtest_Parameter_CPO.o Backtest_Trade_CPO.o Data_Bar.o
It should be noted that this is the correct directory for libpq-fe.h and that I'm linking pq, so I'm not exactly sure why the postgres functions aren't linking correctly. I'm running Ubuntu 12.04 and installed psql (PostgreSQL) 9.1.6 from synaptic. As well I'll short circuit this, I am using #include "libpq-fe.h".
Any ideas on how I can get this linking issue resolved?
put -L/usr/lib/ -lm -lpq in the end of link command, the linker can then find the symbols
mpiCC -o decisioning_mpi -O2 -g -Wall -Werror decisioning_mpi.o io.o \
calculations.o rules.o Instrument.o Backtest_Parameter_CPO.o \
Backtest_Trade_CPO.o Data_Bar.o -L/usr/lib -lm -lpq
GCC Link Reference:
http://www.network-theory.co.uk/docs/gccintro/gccintro_18.html

Problem with my makefile

I think I have a problem with my makefile. I'm writing this program:
Q2.cpp contains the main.
Agent.cpp Agent.h
Timing.cpp Timing.h
RandomDouble.cpp RandomDouble.cpp
And I'm using the header randoma.h in RandomDouble.cpp.
I downloaded the randomaelf64.a file and I wrote this makefile:
Q2 : Q2.o Agent.o Timing.o RandomDouble.o
g++ -Wall -g randomaelf64.a RandomDouble.o Q2.o Agent.o Timing.o -o Q2
Q2.o : Q2.cpp Agent.h Timing.h
g++ -Wall -g -c Q2.cpp -o Q2.o
Agent.o : Agent.cpp Agent.h Timing.h RandomDouble.h PrintQ2.h
g++ -Wall -g -c Agent.cpp -o Agent.o
RandomDouble.o : RandomDouble.cpp RandomDouble.h randoma.h
g++ -Wall -g -c RandomDouble.cpp -o RandomDouble.o
Timing.o : Timing.cpp Timing.h Agent.h
g++ -Wall -g -c Timing.cpp -o Timing.o
clear :
rm *.o Q2
Except for the first command, each g++.. command is working when I do it separately.
Even when I add a main() to RandomDouble.cpp and run:
g++ -Wall -g randomael64.a RandomDouble.cpp -o rand
it's working. So I think that maybe the problem is with my makefile.
When I run make I get this error:
RandomDouble.o: In function `InitSeed()':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:11: undefined reference to `MersenneRandomInit'
RandomDouble.o: In function `InitSeed(int)':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:16: undefined reference to `MersenneRandomInit'
RandomDouble.o: In function `GetRandomDouble()':
/cs/stud/ofrenk33/CPP/ex1/RandomDouble.cpp:21: undefined reference to `MersenneRandom'
Agent.o: In function `Agent::SendMessage()':
/cs/stud/ofrenk33/CPP/ex1/Agent.cpp:31: undefined reference to
...
make: *** [Q2] Error 1
I need to say that there are functions declared in randoma.h which are in the randomaelf64.a library.
How do I fix this problem?
You need to link against the library after all your own object files. The linker will only include as much of the library as it needs, and if there are no unresolved references yet, then none of the library will be needed. Change the first rule to:
Q2 : Q2.o Agent.o Timing.o RandomDouble.o
g++ -Wall -g RandomDouble.o Q2.o Agent.o Timing.o randomaelf64.a -o Q2