Zedd
Zedd

Reputation: 345

Linking/compiling a program that uses boost/filesystem.hpp

I'm trying to use the boost/filesystem library in some code that I am writing. I seem to be having a hard time getting it to compile. I'm running Debian Wheezy, and have boost version 1.49(which is what comes if you install using apt-get). I'm trying to compile an example that is available with the documentation

#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;

int main(int argc, char* argv[])
{
  if (argc < 2)
  {
    std::cout << "Usage: tut1 path\n";
    return 1;
  }
  std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
  return 0;
}

I use the following command:

g++ temp.cc -o temp /usr/lib/libboost_filesystem.a

I get a number of errors such as:

/usr/lib/libboost_filesystem.a(operations.o): In function `boost::filesystem3::detail::dir_itr_close(void*&, void*&)':
(.text+0x4d): undefined reference to `boost::system::system_category()'
/usr/lib/libboost_filesystem.a(operations.o): In function `boost::filesystem3::detail::directory_iterator_increment(boost::filesystem3::directory_iterator&, boost::system::error_code*)':
(.text+0xe3): undefined reference to `boost::system::system_category()'

This is probably some linking error right? Any ideas on how I could solve it?

UPDATE #1: I tried running it with the -lboost_filesyste and -L /usr/lib. It gives me the following error:

/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'

Upvotes: 15

Views: 24682

Answers (3)

Archie
Archie

Reputation: 6844

You are not linking the library properly. Also, as others mentioned, boost_filesystem needs also boost_system library. Use:

g++ temp.cc -o temp -lboost_system -lboost_filesystem

Command line param -l foo links libfoo.a library. If the static library is not in default library location, use command -L /custom/library/dir. But I believe /usr/lib is automatically taken into consideration by GCC.


Edit

According to your comment below it looks like you are not compiling the file with main() function, or you have a typo in main() name. Make sure that temp.cc contains one and only one of these functions:

int main();
int main(int argc, char** argv);

Of course you do remember that upper/lower case matters. :)

Upvotes: 34

Marshall Clow
Marshall Clow

Reputation: 16660

Boost.Filesystem uses things in Boost.System. You have to link against that, too.

The error messages that you are seeing:

/usr/lib/libboost_filesystem.a(operations.o): In function 
`boost::filesystem3::detail::dir_itr_close(void*&, void*&)':
(.text+0x4d): undefined reference to `boost::system::system_category()'

that's a reference to Boost.System

Add -lboost_system and you should be good to go (or, at least better off).

Upvotes: 3

Charles Pehlivanian
Charles Pehlivanian

Reputation: 2133

Compile with -lboost_filesystem

Upvotes: 1

Related Questions