Adding Boost Library to a C++ project in OS X Eclipse

11,821

Solution 1

Not sure where you do this in Eclipse these days, but under the include paths for Eclipse should be the path to the main boost directory (/Users/jacobschoen/Library/boost_1_45_0?). The compiler line should have something like the following in it, I would think:

Invoking: GCC C++ Compiler

g++ -I/Users/jacobschoen/Library/boost_1_45_0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD (etc..)

Update: Looking at my system, the linker path on yours might be more appropriately:

-I/Users/jacobschoen/Library/boost_1_45_0/stage/lib

Depending, of course, upon how you've installed and built boost -- this is with my most recent attempt with a full source build. Depending upon how you obtained boost, this may or may not be different. I recently redid the boost on my Mac for 64 bit and haven't had much time to try it yet....

Solution 2

Just wanted to be clear on what actually worked, since it was kinda pieced together from a few answers.

  1. Download the boost files and extract them to where you want to put them.
  2. In your terminal navigate to the directory and run ./bootstrap.sh
  3. When that is done run ./bjam (this takes a while so go smoke and get a cup of coffee)
  4. Open up your eclipse Project and go to Project > Properties > C/C++ Build > Settings
  5. Click on MacOS X C++ Linker > Libraries. You should see a split window with the top being for 'Libraries (-l)'. In this section add both boost_system and boost_filesystem. In the bottom section it should be for 'Library Search Path (-L)'. Here you want to put the path to the stage/lib directory inside where you extracted the boost download. It should look similar to below:alt text
  6. Click GCC C++ Compiler > Includes. This will be a single pane where it says 'Include Paths (-I)', well I think it is an I as he font is weird and could be a lower case l also. Anyway in that section add the path to where you put boost without the stage/lib part. It should look like below:alt text

Everything should compile now with out a problem, and if you need to use any other boost libraries it should be just a matter of adding it to the linker section where boost_filesystem and boost_system are. Enjoy.

Solution 3

Add boost_system to the linker list, together with boost_filesystem.

Share:
11,821
Jacob Schoen
Author by

Jacob Schoen

Born and raised in Southeast Louisiana, graduated with CS degree from the University of New Orleans and currently working towards Masters degree in Computer Science also. I have always been interested in computers, and becoming a programmer seemed a natural progression for me. It fits my personality and gives me a new puzzle to solve everyday.

Updated on July 23, 2022

Comments

  • Jacob Schoen
    Jacob Schoen almost 2 years

    I am have been attempting to get a C++ project setup using boost file system library using eclipse. I followed these directions to install boost on my system. The directions where pretty much

    1. download
    2. extract
    3. run bootstrap.sh
    4. run ./bjam architecture=combined

    That seemed to go fine, no errors. I then fired up eclipse and created a new test project called test with a single file called test.cpp. The code in it is:

    #include <stdio.h>
    #include <boost/filesystem.hpp>
    
    int main() {
        boost::filesystem::path path("/Users/schoen"); // random pathname
        bool result = boost::filesystem::is_directory(path);
        printf("Path is a directory : %d\n", result);
        return 0;
    }
    

    This is just something simple to make sure it is all set up correctly. Of course I tried to compile at this point and it failed. Did some googling and found this site. It said to add the boost library to the linker by going to project properties and adding "boost_filesystem". I tried this, and well it didn't work.

    Can someone point me in the right direction or give me a hint to how to set up Boost in an Eclipse project?

    I am new to C++ and Eclipse, and most my experience is in Java with Netbeans. So I am pretty lost at the moment.

    UPDATE

    I just wanted to update on what I have tried based on the answers given.

    Based on Alex's suggestion I added boost_system and boost_filesystem to the linker list. I was still getting the same compiler errors.

    Following the suggestion from rve I added the path to the boost libraries to the Library search path. When this did not work. I cleared out the linker list and tried it with just the library search path. This also did not work.

    I then cleared the Library search path. I then manually edited the command on the linker window to be 'g++ -L/Users/jacobschoen/Library/boost_1_45_0/stage/lib -lboost -lboost_filesystem'. This also did not work.

    In all of these I tried setting the path to boost to be '/Users/jacobschoen/Library/boost_1_45_0' and '/Users/jacobschoen/Library/boost_1_45_0/stage/lib'. Neither worked.

    As requested the comiler error for the above code is:

    **** Build of configuration Debug for project test ****
    
    make all 
    Building file: ../src/test.cpp
    Invoking: GCC C++ Compiler
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/test.d" -MT"src/test.d" -o"src/test.o" "../src/test.cpp"
    ../src/test.cpp:10:32: warning: boost/filesystem.hpp: No such file or directory
    ../src/test.cpp: In function 'int main()':
    ../src/test.cpp:13: error: 'boost' has not been declared
    ../src/test.cpp:13: error: expected `;' before 'path'
    ../src/test.cpp:14: error: 'boost' has not been declared
    ../src/test.cpp:14: error: 'path' was not declared in this scope
    make: *** [src/test.o] Error 1
    

    If any one has any further suggestions I am still trying.

    Second Update On a suggestion by rholmes I added an include library along with the linker list and library search path. So now the compile error is:

    **** Build of configuration Debug for project test ****
    
    make all 
    Building target: test
    Invoking: MacOS X C++ Linker
    g++ -L/Users/jacobschoen/Library/boost_1_45_0 -o "test"  ./src/test.o   -lboost_system -lboost_filesystem
    ld: library not found for -lboost_system
    collect2: ld returned 1 exit status
    make: *** [test] Error 1
    

    Any ideas?

  • rve
    rve over 13 years
    It is a problem with Eclipse. The error is a linker error about missing functions. You have to tell Eclipse to link against the boost_filesystem library. I do not know Eclipse but using gcc you just add something like -L/path/to/boost/libs -lboost_filesystem
  • rve
    rve over 13 years
    Other programs using boost can work without linking against a library because the are header only libraries.
  • Jacob Schoen
    Jacob Schoen over 13 years
    I will try this evening when I get home. I had tried boost_filesystem and boost, but not boost_system. So maybe that will do it.
  • Jacob Schoen
    Jacob Schoen over 13 years
    @rve I will give that a shot along with Alex's suggestion and see what happens. @Nav I am glad to know that I am not only in being puzzled. I thought I followed the directions correctly, but just couldn't ever get it to work.
  • Jacob Schoen
    Jacob Schoen over 13 years
    Still a no go, I even tried with adding the Library Search Path where I have boost on my system
  • Jacob Schoen
    Jacob Schoen over 13 years
    @rholmes Thanks for the help. I think I am closer now. Any other ideas based on the update?
  • Jacob Schoen
    Jacob Schoen over 13 years
    With the latest error about it not finding boost_system, you have any ideas?
  • rholmes
    rholmes over 13 years
    For my system, the library path is at .../boost_1_45_0/stage/lib
  • Jacob Schoen
    Jacob Schoen over 13 years
    @rholmes Thanks man. That did it. And welcome to SO by the way. Hopefully I can help you out in the future in return.
  • rholmes
    rholmes over 13 years
    @jschoen - Thanks for the welcome! I've run across SO in many of my searches - glad that my comments helped! BTW - is there a link to the type of markup that SO accepts? Standard wiki? Thanks!
  • Jacob Schoen
    Jacob Schoen over 13 years
    @rholmes I think this has most of the markup - stackoverflow.com/editing-help also there is a list FAQ here meta.stackexchange.com/questions/tagged/faq. And if you have other questions about SO check out Meta meta.stackoverflow.com where you can ask questions about SO our the SO engine in general. Also if you get addicted to whole format of SO check out Stackexchange to find a whole slew of sites they run stackexchange.com. Enjoy!
  • rholmes
    rholmes over 13 years
    @jschoen Thanks for the info - nice writeup on your ultimate solution, BTW.
  • Alessandro
    Alessandro almost 11 years
    This is exactly what I need but for Linux OS. I navigate to the settings pane but the only tabs available are the Binary Parsers and the Error Parsers. Any ideas on how to do this? I'm using Eclipse 3.7.1 on Red Hat Enterprise Linux
  • adhg
    adhg almost 11 years
    @jschoen awesome explanation!
  • Brett Bonner
    Brett Bonner over 10 years
    for later Boost builds for Mac, ( > 1.5x) I needed to add "-mt" to my linker libraries options. For example, if I wanted to include libboost_regex-mt.a to the linker, I would specify "boost_regex-mt".
  • Abhishek Kannan
    Abhishek Kannan about 10 years
    I followed this . But I get this error. ld: 1 duplicate symbol for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [cpp] Error 1