Article: Using Boost C++ libraries in Eclipse »
FERDY CHRISTANT - AUG 29, 2007 (06:11:24 PM)
Every now and then I have a play with C++ on my Ubuntu laptop. I'm not particularly experienced in C++, but enjoy its raw power, portability and complexity. I still have the ambition of building something cool with OpenGL one day, but finding the time is problematic.
Anyway, for a very simple test program I wanted to list the files in a directory using C++. Surprisingly, there is not a built-in way to do this in a portable way in C++. Instead, one has to rely on external libraries. In my search to do this as standardized as possible, I ended up at Boost, a large collection of reusable libraries.
This post lists the steps to install Boost and make use of it in Eclipse under Ubuntu, it is likely to work in a similar way in other distributions and even on Windows.
In Ubuntu, version 1.33.1 of Boost is included in the package manager, whilst the current version is 1.34.1. We now have two options, installing the latest version manually, or using the package manager to install the 1.33.1 version. I went for the 1.33.1 version, because I am lazy and prefer using stable libraries that are packaged for my distrubution. To install 1.34, see the Getting Started section on the Boost site.
Boost consists of a general library and a number of individual libraries that you may or may not need. In my case I needed the general library and one specific library, the file system library. In the Synaptic package manager I therefore selected the following packages and marked them for installation:
- libboost-dbg
- libboost-dev
- libboost-doc
- libboost-filesystem1.33.1
- libboost-filesystem-dev
After hitting "apply", the packages are downloaded and installed. With the installation done, it is time to put it to the test. In Eclipse, create a new managed C++ project, and add a source file to it called "main.cpp". Next, we paste example code in the source file. Note that the Boost website contains an example that lists the files in a directory. This example will work if you installed Boost 1.34.x, but not if you installed 1.33, like I did. The 1.33 example is part of the 1.33.1 download. Once you have the correct sample code, paste it into the new source file you created.
Almost done. The last step to perform is to link to the file system library. You can use of the general library functions of Boost without explicit linking, but the individual libraries you must link to from your project. Luckily, this is easy. Im Eclipse, right-click on your project and go to Properties. Next, open the Libraries section under GCC C++ Linker. In the Libraries (-l) panel add an entry with the value "boost_filesystem" and click OK.
Done! Boost is installed and you have set up your project in Eclipse successfully. After saving main.cpp, your project is built automatically, unless you disabled this in Eclipse. Finally, test the program by initating it from the command line, and passing it a file path. This should result in a directory listing of the speccified path. Here is an example run:
Whilst this may sound like a lot of work for listing a directory, do know that this is just a simple example to test Boost. Once properly configured, Boost opens up a world of useful, portable functionality.



Comments: 12
Reviews: 7
Average rating:
Highest rating: 5
Lowest rating: 5
COMMENT: STEFAN
SEP 6, 06:38:27 AM
COMMENT: BRICE
MAY 30, 2008 - 17:51:13
COMMENT: JABBA LACI

JUN 21, 2008 - 08:10:31 AM
COMMENT: ANONYMOUS
OCT 6, 2008 - 07:45:50 AM
COMMENT: ANO
MAY 13, 2009 - 02:37:30 AM
COMMENT: MATHEUS
MAY 18, 2009 - 04:59:04 PM
COMMENT: EDNSPACE

SEP 29, 2009 - 07:24:18 PM
-lboost_filesystem-mt
Thanks for the great article it made it possible to find my error.
thanks «
COMMENT: DC
JAN 27, 2010 - 09:47:12 PM
http://www.boost.org/doc/libs/1_34_1/libs/filesystem/example/simple_ls.cpp
I was able to use boost with your little bit of help. Thanks! «
COMMENT: DA
JUN 8, 2010 - 18:51:21
COMMENT: AA
OCT 14, 2010 - 11:02:51 AM
COMMENT: UBSI
OCT 18, 2010 - 12:36:30 PM
COMMENT: MARIO
JUL 2, 2011 - 18:07:43
filesystem.cpp:(.text+0x29): undefined reference to `boost::system::get_system_category()'
filesystem.cpp:(.text+0x35): undefined reference to `boost::system::get_generic_category()'
filesystem.cpp:(.text+0x41): undefined reference to `boost::system::get_generic_category()'
filesystem.cpp:(.text+0x4d): undefined reference to `boost::system::get_generic_category()'
filesystem.cpp:(.text+0x59): undefined reference to `boost::system::get_system_category()'
collect2: ld returned 1 exit status
-l boost_filesystem ist is..
Thank you !! «