How to test C++ Boost installation

once boost is installed on a machine, the fastest way to test the installation is to use some of the libraries from it in test C++ programs, and then try to build them. the following two programs can be used for this purpose:

first.cpp

#include<iostream>
#include<boost/any.hpp>

int main()
{
boost::any a(5);
a = 7.67;
std::cout<<boost::any_cast<double>(a)<<std::endl;
}

build this program using:

$ g++ -o first first.cpp

the second example needs to be linked to a library file.

second.cpp

#include<iostream>
#include<boost/filesystem/operations.hpp>

namespace bfs=boost::filesystem;
int main()
{
bfs::path p("second.cpp");
if(bfs::exists(p))
std::cout<<p.leaf()<<std::endl;
}

$ g++ -o second second.cpp -lfile_system

if the above two programs build and run with out any problems, then boost is installed and working properly on your system.

5 Comments »

  1. Matt said

    There seems to be a bug in first.cpp.

    “std::cout(a)

  2. Matt said

    Comment was cut off above.

    Anyway, boost::cast_any should be boost::any_cast. Please fix.

  3. tabrez said

    thanks a lot for pointing out the subtle typo!
    I usually copy/paste the compiled versions of all the example programs but this one seems to have got a miss somehow.
    thanks for correcting the same.

  4. Matt said

    Good.

    Also, I left another comment earlier, but it looks like there was some problem on the server.

    For the compilation of second.cpp,

    g++ -o second second.cpp -lfile_system
    should be replaced with
    g++ -o second second.cpp -lboost_filesystem

    Anyway, thanks for the samples and setup instructions. From now on, I’ll be using them everytime I install Boost.

  5. tabrez said

    Sorry for the inconvenience caused by the servers. WordPress.com has upgraded the servers to much better configuration few days back and that should help in the future.

    I used -lfile_system just as an indication that the program needs to be linked to a library using an option similar to -l. Exact library name(and the option name in case of Windows) depends on a particular boost installation; for example, for Slackware it is -lboost_filesystem-gcc and for Fedora it is -lboost_filesystem. This information is available in the individual installation posts.

    I think you might have noted that I have moved this blog to http://beans.seartipy.com/ and am currently writing there. This might of interest to you: Boost File System Library

    Thanks very much for taking interest and providing the suggestions!

RSS feed for comments on this post · TrackBack URI

Leave a Comment