Search code examples
c++boostincludeg++include-path

C++ file compiling: -L and -I arguments don't work for boost library


There are similar questions but their answers did not work for my issue. I have a c++ program with #include <boost/test/unit_test.hpp> on top (among other includes).

To compile correctly, if I understood, I should do the command:

g++ -g -L/path_to_boost_lib -lboost_lib myprog.cpp -o myprog.exe

If i do a locate, I get /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.so. Hence I edited my call to g++ by doing:

g++ -g -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework myprog.cpp -o myprog.exe

But I still get errors of the type undefined reference to boost::unit_test.

I also tried the option -I/usr/include/ which contains the boost folder, without success.


Solution

  • It's because of the order. The GCC linker goes through the artifacts left-to-right, and every unknown symbol it encounters in an object file must be resolved by an artifact occurring afterwards.

    The right command is thus:

    g++ -g myprog.cpp -L/usr/lib/x86_64-linux-gnu -lboost_unit_test_framework -o myprog.exe
    

    See this answer for a more thorough explanation.

    I suggest using a build tool like CMake that takes care of such low-level details for you.