This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
Following the example here: http://www.learncpp.com/cpp-tutorial/19-header-files/
Relating to add.h and main.cpp
When I try to compile main.cc (I just used another extension), I get the following:
/tmp/cckpbRW.o:main.cc:(.text+0x9d):undefined reference to 'add(int, int)' collect2: ld returned 1 exit status
How can I fix this issue?
Thanks.
Your didn't link your main object to your add one, so when the linker tries to build the executables it cannot find the definition of symbol add(int, int) it uses.
You should compile main object, add object and link them together, like this:
g++ -c -o main.o main.cpp
g++ -c -o add.o add.cpp
g++ -o executable main.o add.o
or
g++ -o executable main.cpp add.cpp
this will compile add.cpp and main.cpp together
Looks like you are not linking the second .cpp file into final executable. Either compile and link them at the same time:
$ c++ -Wall -Werror -pedantic -g -otest1 add.cpp main.cpp
or compile them separately and then link:
$ c++ -Wall -Werror -pedantic -g -c main.cpp
$ c++ -Wall -Werror -pedantic -g -c add.cpp
$ c++ -Wall -Werror -pedantic -g -otest1 add.o main.o
Related
Good day!
I installed the fmt library in Ubuntu. Added it in my project
#include "fmt/core.h"
#include "fmt/format.h"
#include "fmt/format-inl.h"
to use fmt::format_int и fmt::format. I added library headers in several cpp files of my project. During linkage I got the mistake "multiple definition":
obj/container.o: In function fmt::v7::format_error::~format_error()': container.cpp:(.text+0x40e): multiple definition of fmt::v7::format_error::~format_error()'
obj/line.o:line.cpp:(.text+0x40e): first defined here
I've read something about this mistake. It is recommended to divide declaration and implementation in h and cpp files, to set some status to objects that causes mistake and so on. But all this recommendations imply editing of library (not my!) code!
What is wrong?
I do the following
compilation of files - one by one
g++ -std=c++11 -Wall -o obj/line.o -c /home/...//line.cpp
g++ -std=c++11 -Wall -o obj/container.o -c /home/...//container.cpp
g++ -std=c++11 -Wall -o obj/geometryObject.o -c /...//geometryObject.cpp
g++ -std=c++11 -Wall -o obj/model.o -c /home/...//model.cpp
g++ -std=c++11 -Wall -o obj/point.o -c /home/...//point.cpp
g++ -std=c++11 -Wall -o obj/main.o -c /home/...//main.cpp
Linking - error here
g++ -std=c++11 -Wall -o myapp obj/line.o obj/container.o obj/geometryObject.o obj/model.o obj/point.o obj/main.o
You shouldn't be including fmt/format-inl.h because it's an internal header. Please see the documentation for the list of public headers and what they provide.
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?
This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 5 years ago.
There are linker errors to Symbols defined by SFML, but i cannot see how they occur despite that I linked the lib.
I'm using make, which I currently learn and I want to build a minimalistic dev-environment with it.
Give a holler if you need anymore information than the following. I'd just like to minimize the questions size.
XXX#XXX ~/Documents/dev/cpp/proj/beep $ make clean
rm -f build/*.o build/release/*.o build/debug/*.o build/test/*.o
XXX#XXX ~/Documents/dev/cpp/proj/beep $ make tests
//test obj first
g++ -std=c++14 -Wall -pthread -Iinclude -c test/Packager.ut.cpp -o build/test/Packager.ut.o -g3
//now the src obj
g++ -std=c++14 -Wall -pthread -Iinclude -c src/ClientAddress.cpp -o build/debug/ClientAddress.o -g3
g++ -std=c++14 -Wall -pthread -Iinclude -c src/Packager.cpp -o build/debug/Packager.o -g3
g++ -std=c++14 -Wall -pthread -Iinclude -c src/Package.cpp -o build/debug/Package.o -g3
Built debug object files.
//now the first test itself
g++ -std=c++14 -Wall -pthread -Iinclude -lsfml-network build/test/Packager.ut.o build/debug/ClientAddress.o build/debug/Packager.o build/debug/Package.o -g3 -o bin/test/Packager.ut
build/test/Packager.ut.o: In function `main':
/home/XXX/Documents/dev/cpp/proj/beep/test/Packager.ut.cpp:69: undefined reference to `sf::IpAddress::IpAddress(char const*)'
build/debug/ClientAddress.o: In function `nw::udp::ClientAddress::ClientAddress()':
/home/XXX/Documents/dev/cpp/proj/beep/src/ClientAddress.cpp:21: undefined reference to `sf::IpAddress::IpAddress(char const*)'
build/debug/ClientAddress.o: In function `nw::udp::operator==(nw::udp::ClientAddress const&, nw::udp::ClientAddress const&)':
/home/XXX/Documents/dev/cpp/proj/beep/src/ClientAddress.cpp:33: undefined reference to `sf::operator==(sf::IpAddress const&, sf::IpAddress const&)'
...
and so on ... every mentionings of sf:: inside the files are quoted
I get the same error pattern if I try to compile the other tests (for ClientAddress for example)
Of course i now want to know what i linked wrong how there. As you can see the lib is linked with -lsfml-network. I also checked the SMFL installation, so it is at least less likely a lib file gone missing from standard directory.
I guess there is an error in my usage of g++ , compiling and linking orders or smth.
My project tree:
>bin
----mainexec
--->test
----.ut
>build
--->debug
----.o
--->release
----.o
--->test
----.ut.o
>src
---- .cpp
>include
---- .h
>test
---- .ut.cpp
As a second part of the question, I'd like to ask if there is a better way to build tests, because I simply link every src-obj with my test-obj, even if there are way more src-obj linked than it actually needs. It should work and I do not have to maintain my test dependencies all the time, which later would be very cumbersome. What is common ?
sfml-network has a dependency on sfml-system. Try add -lsfml-system before -lsfml-network in your linker command in your makefile
I had an error unexpected during my program compilation.
(I must use standard 2011 to std::regex.)
In my MinGw cmd, I go to the folder in which my programme is, and write:
g++ -Wall -c -std=c++0x main.cpp
here every thing is ok, but after when I write:
g++ -Wall -o -std=c++0x main.exe main.o
it tell me :
g++.exe: error: main.exe: No such file or directory
WHY ?
Because you're writing the switches in the wrong order. -o requires that the destination filename be after it.
Here, you've told GCC to link main.exe and main.o into the executable -std=c++0x, and main.exe doesn't even exist yet.
g++ -Wall -o -std=c++0x main.exe main.o
# ^^^^^^^^^^^^^ ????????
Instead, tell it to link main.o into the executable main.exe, in C++0x mode:
g++ -Wall -std=c++0x -o main.exe main.o
# ^^^^^^^^^^^
Why this does not work, file test.c:
#include <event.h>
int main(void)
{
event_init();
return 0;
}
Then:
gcc -o test.o -c test.c runs OK, but
Link:
g++ -o test -levent test.o produces
test.o: In function `main':
test.c:(.text+0x5): undefined reference to `event_init'
collect2: ld returned 1 exit status
So it cannot be linked as C++. How to solve this? I need to link it as C++ and compile as C.
This question has been asked many times. On Linux, you should put libraries after object and source files in the compilation command. So try
g++ -Wall -g -c mytest.cc
g++ -Wall -g mytest.o -levent -o mytest
Avoid calling your test program test which is an existing utility or shell builtin.
As a newbie, remember to always compile with all warnings asked -Wall and for debugging -g and learn to use gdb