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