Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I link boost to my program?

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]
like image 505
Tim Hutchison Avatar asked Jul 17 '14 20:07

Tim Hutchison


1 Answers

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").

like image 108
gexicide Avatar answered Sep 28 '22 05:09

gexicide