Search code examples
boostmakefilelinker

Including boost libraries in make files


I'm learning Boost and am having trouble with my makes files. Here is my basic makefile:

accesstimer: acctime.o btimer.o
    g++ acctime.o btimer.o -o accesstimer

acctime.o: acctime.cpp btimer.h
    g++ -c acctime.cpp 

bentimer.o: btimer.cpp btimer.h
    g++ -c btimer.cpp 

When acctime.cpp has no boost filesystem elements in it this m,ake file works fine. As soon as I add boost filesystem elements I obviously need to make references to the boost libray in the make file this is where I am having issues.

The following line works for a single file compilation:

g++ -I /usr/local/boost/boost_1_39_0 boosttest1.cpp -o bt1 /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a

Now I'm trying to integrate this into the make file. I've tried many based on what information I can find on the web but none are working this is my latest:

accesstimer: acctime.o bentimer.o
    g++ acctime.o bentimer.o -o accesstimer

acctime.o: acctime.cpp bentimer.h
    g++ -c -I /usr/local/boost/boost_1_39_0 acctime.cpp /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a

bentimer.o: bentimer.cpp bentimer.h
    g++ -c bentimer.cpp 

Unfortunately it stlill can't find the Boost libraries, can anyone help? thanks

Having read the advice of the people who've answered I've now got this:

accesstimer: acctime.o bentimer.o
    g++ -L /usr/local/boost/boost_1_39_0 acctime.o /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a bentimer.o -o accesstimer

acctime.o: acctime.cpp bentimer.h
    g++ -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h
    g++ -c bentimer.cpp 

But this still fails to link.

This is the error message I'm getting:

g++ -L /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a acctime.o  bentimer.o -o accesstimer
acctime.o: In function boost::enable_if<boost::filesystem::is_basic_path<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >, bool>::type boost::filesystem::exists<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)':
acctime.cpp:(.text._ZN5boost10filesystem6existsINS0_10basic_pathISsNS0_11path_traitsEEEEENS_9enable_ifINS0_13is_basic_pathIT_EEbE4typeERKS7_[boost::enable_if<boost::filesystem::is_basic_path<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >, bool>::type boost::filesystem::exists<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)]+0x26): undefined reference to `boost::filesystem::detail::status_api(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int&)'
collect2: ld returned 1 exit status
make: *** [accesstimer] Error 1

Following orsogufo's advice (thanks! much appreciated) now have this:

accesstimer: acctime.o bentimer.o
    g++ -L/usr/local/boost/boost_1_39_0/stage/lib -llibboost_filesystem-gcc41-mt.a -llibboost_system-gcc41-mt.a acctime.o bentimer.o -o accesstimer  

acctime.o: acctime.cpp bentimer.h
    g++ -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h
    g++ -c bentimer.cpp 

Looking better, but still can't quite find the library:

g++ -L/usr/local/boost/boost_1_39_0/stage/lib -llibboost_filesystem-gcc41-mt.a -llibboost_system-gcc41-mt.a acctime.o bentimer.o -o accesstimer
/usr/bin/ld: cannot find -llibboost_filesystem-gcc41-mt.a
collect2: ld returned 1 exit status
make: *** [accesstimer] Error 1

I've double checked that location and the library is definately at: /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a

STill no joy, usimg this now:

accesstimer: acctime.o bentimer.o
    g++  -L/usr/local/boost/boost_1_39_0 -lboost_filesystem-gcc41-mt acctime.o bentimer.o -o accesstimer    

acctime.o: acctime.cpp bentimer.h
    g++ -I /usr/local/boost/boost_1_39_0 -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h
    g++ -c bentimer.cpp 

Getting:

g++  -L/usr/local/boost/boost_1_39_0/stage/lib/ -llibboost_filesystem-gcc41-mt acctime.o bentimer.o -o accesstimer
/usr/bin/ld: cannot find -llibboost_filesystem-gcc41-mt
collect2: ld returned 1 exit status
make: *** [accesstimer] Error 1

It's working with this:

accesstimer: acctime.o bentimer.o
    g++ -L/usr/local/boost/boost_1_39_0/stage/lib -lboost_filesystem acctime.o bentimer.o -o accesstimer    

acctime.o: acctime.cpp bentimer.h
    g++ -I /usr/local/boost/boost_1_39_0 -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h
    g++ -c bentimer.cpp 

Thanks for all your help


Solution

  • When you link the object files to create the executable (your first makefile rule) you must pass the location of the boost libraries with the -L flag and the names of the libraries with the -l flag.

    accesstimer: acctime.o bentimer.o
        g++ -L/usr/local/boost/boost_1_39_0/stage/lib -lboost_filesystem acctime.o bentimer.o -o accesstimer
    

    where /usr/local/boost/boost_1_39_0/stage/lib is the directory containing the libraries and boost_filesystem the file name of the library without the beginning lib (modify those two as appropriate).

    The .a file you're trying to link is the wrong one... the library should have no extension.