Leanhope
Leanhope

Reputation: 1

Undefined boost reference despite correct linking

I found a lot of similar problems, but I was not able to apply any given solution for me. A few months ago, I worked on a project using the boost library. I compiled simply in the command line, as described on the boost website.

g++ -I pathToBoost file.cpp -o file -lboost_system -lboost_filesystem

The two required linkings to boost_system and boost_filesystem were also done. This worked perfectly, but now suddenly an error occurs when i tried to compile it again.

/usr/bin/ld: /tmp/ccM2BzEo.o: in function `boost::system::generic_category()':
file.cpp:(.text._ZN5boost6system16generic_categoryEv[_ZN5boost6system16generic_categoryEv]+0x7): 

undefined reference to `boost::system::detail::generic_category_instance'

Well, it seems to me that there is an error linking the boost_system library. Since the same thing worked before, is a problem with the compiler possible?

I used

Boost 1.68.0
g++ (GCC) 8.2.1
ManjaroLinux 18.0.3

I hope that somebody can help me here and that I was not just too stupid to see a solution in another thread.

Greetings!

Upvotes: 0

Views: 83

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136495

The order of libraries in linker command line is important. boost_filesystem depends on boost_system, hence the fix:

-lboost_filesystem -lboost_system

Alternatively, you can surround the list of libraries with --start-group and --end-group to make the linker re-scan the libraries multiple times until it either fails or resolves all symbols, so that no specific ordering of libraries in the command line is necessary. However, it may take longer to link. E.g.:

g++ -I pathToBoost file.cpp -o file -Wl,--start-group -lboost_system -lboost_filesystem -Wl,--end-group

Since you specify -I pathToBoost you also need to specify -L<path-to-boost-libs> -Wl,-rpath=<path-to-boost-libs>. Otherwise it uses headers from one version of boost from pathToBoost, but links wrong libraries from your system boost.

Upvotes: 1

Related Questions