How do I link boost to my program?

10,338

Answer

boost::system must be linked explicitly. In contrast to many other parts of boost, boost system is not header-only. Thus, you must make sure to link it when you compile. You have two options for linking.

Reference: http://www.boost.org/doc/libs/1_47_0/more/getting_started/unix-variants.html#link-your-program-to-a-boost-library

Option 1

Use -lboost_system (or the equivalent file in your /boost_####/stage/lib/ directory). Of course, you also have to set the library path using -L/file/path/to/libraries unless boost system resides in a standard lookup dir.

Example

g++ playground.cc -o playground -L~/boost/stage/lib/ -libboost_filesystem-mgw48-mt-1_54.a

Option 2

Include the full file path to the library at the end of your code.

Example

Run this from the command line. The triple quotes """ are necessary only for paths that contain spaces.

g++ playground.cc -o playground """C:\My Programs\boost_1_54_0\stage\lib\libboost_filesystem-mgw48-mt-1_54.a""" """C:\My Programs\boost_1_54_0\stage\lib\libboost_system-mgw48-mt-1_54.a"""

Note: For a list of files that are not header-only, see http://www.boost.org/doc/libs/1_47_0/more/getting_started/windows.html#header-only-libraries (Same link as in the first paragraph on "header-only").

Share:
10,338
Tim Hutchison
Author by

Tim Hutchison

I'm a Senior Software Engineer at Cohen & Company in Cleveland, OH. We build internal applications using Typescript, Vuejs, Nodejs, and Expressjs that improve the efficiency and operations of the company. In my free time I enjoy playing various sports (primarily golf and ping pong), watching football and hockey, mentoring high schoolers, reading, and spending time with friends.

Updated on June 16, 2022

Comments

  • Tim Hutchison
    Tim Hutchison almost 2 years

    I downloaded Boost 1.54 and ran bootstrap.bat mingw. Then I tried to run the program below. I am getting the error you see below. I have tried copying my boost folder into the mingw include folder and I have tried linking my file to the boost/stage/lib folder, but I have not been successful. I see a lot of questions similar to this question, but none of them explain how to get the link the boost folder to the file.

    Do I have to copy the boost folder to a different directory? Do I have to change my path variable? How can I get the boost library to link to my code?

    Code

    #include <boost/filesystem.hpp>   
    #include <iostream>              
    
    using namespace std; 
    using namespace boost::filesystem;
    
    int main()
    {
        boost::filesystem::directory_iterator iterator(string("."));
        for(; iterator != boost::filesystem::directory_iterator(); ++iterator)
        {
            cout << (iterator->path().filename()) << endl;
        }
    
        boost::filesystem::path full_path( boost::filesystem::current_path() );
        std::cout << "Current path is : " << full_path << std::endl;
       return 0;
    
    }
    

    Error

    C:\Users\212340~1\AppData\Local\Temp\ccZA7umT.o:playground.cc:(.text+0xa4): undefined reference to `boost::filesystem::path::filename() const'
    C:\Users\212340~1\AppData\Local\Temp\ccZA7umT.o:playground.cc:(.text+0x244): undefined reference to `boost::system::generic_category()'
    C:\Users\212340~1\AppData\Local\Temp\ccZA7umT.o:playground.cc:(.text+0x24e): undefined reference to `boost::system::generic_category()'
    C:\Users\212340~1\AppData\Local\Temp\ccZA7umT.o:playground.cc:(.text+0x258): undefined reference to `boost::system::system_category()'
    c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\212340~1\AppData\Local\Temp\ccZA7umT.o: bad reloc address 0x1b in section `.text$_ZNK5boost6system10error_code7messageEv[__ZNK5boost6system10error_code7messageEv]'
    collect2.exe: error: ld returned 1 exit status
    [Finished in 21.0s with exit code 1]
    
  • Tim Hutchison
    Tim Hutchison almost 10 years
    Can you give an example? My guess is I need to do something like g++ C:\My Programs\playground.cc -lboost_system -L C:\My Programs\boost_1_54_0\stage\lib`, but I ran that and it gave me a cannot find -lboost_system` error.
  • gexicide
    gexicide almost 10 years
    Kudos for this extended answer go to the OP firelight who edited it but was rejected while I was not here. Since the answer is good, however, I reposted his edit in order to preseve it.
  • Tim Hutchison
    Tim Hutchison almost 10 years
    Thank you for reposting the edit! I hope it saves some time for others who have the same problem