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.

Advertisements

13 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!

  6. Hans said

    first.cpp works fine but a get errors on second cpp like that :

    ilesystem::path::assign(const char*, const std::codecvt&)’:
    /usr/local/include/boost/filesystem/path.hpp:208: error: invalid conversion from ‘const char*’ to ‘int’
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘boost::filesystem::path& boost::filesystem::path::assign(const Source&, const std::codecvt&)’:
    /usr/local/include/boost/filesystem/path.hpp:215: error: request for member ‘clear’ in ‘((boost::filesystem::path*)this)->boost::filesystem::path::m_pathname’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘boost::filesystem::path& boost::filesystem::path::assign(InputIterator, InputIterator, const std::codecvt&)’:
    /usr/local/include/boost/filesystem/path.hpp:229: error: request for member ‘clear’ in ‘((boost::filesystem::path*)this)->boost::filesystem::path::m_pathname’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp:232: error: wrong number of template arguments (1, should be 3)
    /usr/include/c++/4.4/bits/basic_string.h:104: error: provided for ‘template class std::basic_string’
    /usr/local/include/boost/filesystem/path.hpp:233: error: invalid type in declaration before ‘(’ token
    /usr/local/include/boost/filesystem/path.hpp:234: error: request for member ‘c_str’ in ‘s’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp:234: error: request for member ‘c_str’ in ‘s’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp:234: error: request for member ‘size’ in ‘s’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘boost::filesystem::path& boost::filesystem::path::operator+=(const char*)’:
    /usr/local/include/boost/filesystem/path.hpp:243: error: invalid conversion from ‘const char*’ to ‘int’
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘boost::filesystem::path& boost::filesystem::path::concat(InputIterator, InputIterator, const std::codecvt&)’:
    /usr/local/include/boost/filesystem/path.hpp:282: error: wrong number of template arguments (1, should be 3)
    /usr/include/c++/4.4/bits/basic_string.h:104: error: provided for ‘template class std::basic_string’
    /usr/local/include/boost/filesystem/path.hpp:283: error: invalid type in declaration before ‘(’ token
    /usr/local/include/boost/filesystem/path.hpp:284: error: request for member ‘c_str’ in ‘s’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp:284: error: request for member ‘c_str’ in ‘s’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp:284: error: request for member ‘size’ in ‘s’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘void boost::filesystem::path::clear()’:
    /usr/local/include/boost/filesystem/path.hpp:325: error: request for member ‘clear’ in ‘((boost::filesystem::path*)this)->boost::filesystem::path::m_pathname’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘void boost::filesystem::path::swap(boost::filesystem::path&)’:
    /usr/local/include/boost/filesystem/path.hpp:334: error: request for member ‘swap’ in ‘((boost::filesystem::path*)this)->boost::filesystem::path::m_pathname’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘const char* boost::filesystem::path::c_str() const’:
    /usr/local/include/boost/filesystem/path.hpp:358: error: request for member ‘c_str’ in ‘((const boost::filesystem::path*)this)->boost::filesystem::path::m_pathname’, which is of non-class type ‘const int’
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘bool boost::filesystem::path::empty() const’:
    /usr/local/include/boost/filesystem/path.hpp:441: error: request for member ‘empty’ in ‘((const boost::filesystem::path*)this)->boost::filesystem::path::m_pathname’, which is of non-class type ‘const int’
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘bool boost::filesystem::path::has_filename() const’:
    /usr/local/include/boost/filesystem/path.hpp:447: error: request for member ‘empty’ in ‘((const boost::filesystem::path*)this)->boost::filesystem::path::m_pathname’, which is of non-class type ‘const int’
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘bool boost::filesystem::path::has_leaf() const’:
    /usr/local/include/boost/filesystem/path.hpp:485: error: request for member ‘empty’ in ‘((const boost::filesystem::path*)this)->boost::filesystem::path::m_pathname’, which is of non-class type ‘const int’
    /usr/local/include/boost/filesystem/path.hpp: At global scope:
    /usr/local/include/boost/filesystem/path.hpp:595: error: invalid use of ‘::’
    /usr/local/include/boost/filesystem/path.hpp:595: error: expected ‘;’ before ‘m_pos’
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘bool boost::filesystem::path::iterator::equal(const boost::filesystem::path::iterator&) const’:
    /usr/local/include/boost/filesystem/path.hpp:585: error: ‘m_pos’ was not declared in this scope
    /usr/local/include/boost/filesystem/path.hpp:585: error: ‘const class boost::filesystem::path::iterator’ has no member named ‘m_pos’
    /usr/local/include/boost/filesystem/path.hpp: In function ‘size_t boost::filesystem::hash_value(const boost::filesystem::path&)’:
    /usr/local/include/boost/filesystem/path.hpp:642: error: request for member ‘begin’ in ‘((const boost::filesystem::path*)x)->boost::filesystem::path::native()’, which is of non-class type ‘const int’
    /usr/local/include/boost/filesystem/path.hpp:642: error: request for member ‘end’ in ‘((const boost::filesystem::path*)x)->boost::filesystem::path::native()’, which is of non-class type ‘const int’
    /usr/local/include/boost/filesystem/path.hpp: In function ‘std::basic_ostream& boost::filesystem::operator<<(std::basic_ostream&, const boost::filesystem::path&)’:
    /usr/local/include/boost/filesystem/path.hpp:659: error: wrong number of template arguments (1, should be 3)
    /usr/include/c++/4.4/bits/basic_string.h:104: error: provided for ‘template class std::basic_string’
    /usr/local/include/boost/filesystem/path.hpp:659: error: no matching function for call to ‘boost::filesystem::path::string() const’
    /usr/local/include/boost/filesystem/path.hpp: In function ‘std::basic_istream& boost::filesystem::operator>>(std::basic_istream&, boost::filesystem::path&)’:
    /usr/local/include/boost/filesystem/path.hpp:666: error: wrong number of template arguments (1, should be 3)
    /usr/include/c++/4.4/bits/basic_string.h:104: error: provided for ‘template class std::basic_string’
    /usr/local/include/boost/filesystem/path.hpp:666: error: invalid type in declaration before ‘;’ token
    /usr/local/include/boost/filesystem/path.hpp: At global scope:
    /usr/local/include/boost/filesystem/path.hpp:677: error: expected unqualified-id before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:677: error: expected ‘)’ before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:677: error: expected initializer before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:678: error: expected unqualified-id before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:678: error: expected ‘)’ before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:678: error: expected initializer before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:679: error: expected unqualified-id before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:679: error: expected ‘)’ before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:679: error: expected initializer before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:680: error: expected unqualified-id before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:680: error: expected ‘)’ before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:680: error: expected initializer before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:681: error: expected unqualified-id before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:681: error: expected ‘)’ before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:681: error: expected initializer before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:682: error: expected unqualified-id before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:682: error: expected ‘)’ before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp:682: error: expected initializer before ‘&’ token
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘boost::filesystem::path& boost::filesystem::path::append(InputIterator, InputIterator, const std::codecvt&)’:
    /usr/local/include/boost/filesystem/path.hpp:693: error: expected initializer before ‘sep_pos’
    /usr/local/include/boost/filesystem/path.hpp:694: error: wrong number of template arguments (1, should be 3)
    /usr/include/c++/4.4/bits/basic_string.h:104: error: provided for ‘template class std::basic_string’
    /usr/local/include/boost/filesystem/path.hpp:695: error: invalid type in declaration before ‘(’ token
    /usr/local/include/boost/filesystem/path.hpp:696: error: request for member ‘c_str’ in ‘s’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp:696: error: request for member ‘c_str’ in ‘s’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp:696: error: request for member ‘size’ in ‘s’, which is of non-class type ‘int’
    /usr/local/include/boost/filesystem/path.hpp:697: error: ‘sep_pos’ was not declared in this scope
    /usr/local/include/boost/filesystem/path.hpp: In member function ‘boost::filesystem::path& boost::filesystem::path::append(const Source&, const std::codecvt&)’:
    /usr/local/include/boost/filesystem/path.hpp:707: error: expected initializer before ‘sep_pos’
    /usr/local/include/boost/filesystem/path.hpp:709: error: ‘sep_pos’ was not declared in this scope
    /usr/local/include/boost/filesystem/path.hpp: At global scope:
    /usr/local/include/boost/filesystem/path.hpp:719: error: expected initializer before ‘path’
    /usr/local/include/boost/filesystem/path.hpp:723: error: expected initializer before ‘path’
    /usr/local/include/boost/filesystem/path.hpp:727: error: expected initializer before ‘path’
    /usr/local/include/boost/filesystem/path.hpp:731: error: expected initializer before ‘path’
    /usr/local/include/boost/filesystem/path.hpp:735: error: expected initializer before ‘path’
    /usr/local/include/boost/filesystem/path.hpp:739: error: expected initializer before ‘path’
    /usr/local/include/boost/filesystem/path.hpp:743: error: expected initializer before ‘path’
    /usr/local/include/boost/filesystem/path.hpp:747: error: expected initializer before ‘path’
    In file included from second.cpp:4:
    /usr/local/include/boost/filesystem/operations.hpp:990: error: expected unqualified-id before ‘&’ token
    /usr/local/include/boost/filesystem/operations.hpp:990: error: expected ‘)’ before ‘&’ token
    /usr/local/include/boost/filesystem/operations.hpp:990: error: expected ‘;’ before ‘&’ token
    second.cpp:12: error: expected ‘;’ at end of input
    second.cpp:12: error: expected ‘}’ at end of input
    second.cpp:12: error: expected unqualified-id at end of input
    second.cpp:12: error: expected ‘}’ at end of input
    second.cpp:12: error: expected ‘}’ at end of input
    root@hans-MS-7379VP:/usr/local#

  7. […] I think I have boost installed properly so I am trying to use the test “first.cpp” found here: […]

  8. […] properties -> Linker ->Libraries -> Add option -> Other -> -lfile_system as noted here. I have run a separate test using #include<boost/any.hpp> so I am not currently doubting that […]

  9. […] ran the second.cpp test found at: tabreziqbal.wordpress.com/2006… (I used the correct commands to run from the comments, which was g++ -o second second.cpp […]

  10. […] ran the second.cpp test found at: https://tabreziqbal.wordpress.com/2006/03/16/how-to-test-c-boost-installation/ (I used the correct commands to run from the comments, which was g++ -o second second.cpp […]

  11. houmingc said

    i apt-get install boost into /usr/include/boost
    how to link the directory to the gnu g++?

  12. […] I cruise we have boost hallowed morally so we am perplexing to use a exam "first.cpp" found here: […]

  13. Appreciating the persistence you put into your website and
    detailed information you present. It’s awesome to come across a blog every once in a while that isn’t
    the same out of date rehashed material. Excellent read!
    I’ve saved your site and I’m adding your RSS feeds to my Google account.

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: