Logo Questions Linux Laravel Mysql Ubuntu Git Menu

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

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.


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?

like image 246
Jacob Schoen Avatar asked Dec 14 '10 01:12

Jacob Schoen

3 Answers

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.

like image 178
Jacob Schoen Avatar answered Nov 15 '22 19:11

Jacob Schoen

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:


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

like image 37
rholmes Avatar answered Nov 15 '22 20:11


Add boost_system to the linker list, together with boost_filesystem.

like image 31
Alex F Avatar answered Nov 15 '22 19:11

Alex F