boost::filesystem Undefined symbols for architecture x86_64

I am very new with using GNU. I am trying to start using the boost filesystem library, and I keep getting these errors. I am trying to get the current working directory, by using boost::filesystem.

My code:

boost::filesystem::path full_path( boost::filesystem::detail::current_path() );
cout << "Current path is : " << full_path << endl;

My command:

g++ -I boost_1_58_0 main.cpp -o example

Result:

Undefined symbols for architecture x86_64:
  "boost::filesystem::detail::current_path(boost::system::error_code*)", referenced from:
      _main in main-1c56eb.o
  "boost::system::system_category()", referenced from:
      ___cxx_global_var_init2 in main-1c56eb.o
  "boost::system::generic_category()", referenced from:
      ___cxx_global_var_init in main-1c56eb.o
      ___cxx_global_var_init1 in main-1c56eb.o
ld: symbol(s) not found for architecture x86_64

Can someone please explain what the error is asking for? What did I do wrong?

Upvotes: 3

Views: 4369

Answers (1)

Ilya Popov
Ilya Popov

Reputation: 4010

boost.filesystem is not a header-only library. You have to link to the library using -L and -l flags. (And make sure the library is already properly built). You need to link to both boost_system and boost_filesystem libraries. The command line could look like:

g++ -Iboost_1_58_0 -Lboost_1_58_0/lib/ -lboost-filesystem -lboost_system main.cpp -o example

(replace the -L argument with the path where the libboost-filesystem.so file resides)

Then, before you are able run the executable, you have to let the loader know where to look for the libraries. You shell be able do that using the following command:

export DYLD_LIBRARY_PATH=/path/to/boost/bib:$DYLD_LIBRARY_PATH

To make it automatic, I would recommend using a build system like cmake instead of just a command line.

Upvotes: 3

Related Questions