error on boost filesystem

2019-08-30 10:24发布

问题:

I try to use boost filesystem on my Mac. I only added the following header and got an error when I tried to compile

# include <boost/filesystem.hpp>

the error is

Undefined symbols for architecture x86_64:
"boost::system::generic_category()", referenced from:
__static_initialization_and_destruction_0(int, int) in cclyDZox.o
"boost::system::system_category()", referenced from:
__static_initialization_and_destruction_0(int, int) in cclyDZox.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

any help is appreciated

回答1:

You need to add the boost_system library:

-lboost_system

to your build command. Note that you will also need to provide the -lboost_system library too.

If your project thereafter builds but moans with something like

dyld: Library not loaded: libboost_filesystem.dylib

you just need to set the environment variable

DYLD_LIBRARY_PATH

to include your

$BOOST_HOME/lib

directory as mentioned here.



回答2:

I think you should do

g++ -I~/Documents/boost_1_53_0/include -L~/Documents/boost_1_53_0/stage/lib -std=c++11 test1ver1.cpp -lboost_filesystem -lboost_system

The

  • -I flag points to where the Boost headers are
  • -L flag points to where the Boost libs are
  • -lboost_filesystem and -lboost_system flags enabled the link of your binary and the Boost Filesystem and System shared libraries. (Those should be always after the object or source files)

For instance:

#include <boost/filesystem.hpp>

int main() {
   boost::filesystem::path path_household_csv("./test");
}