简体   繁体   中英

OSX: Linker error for boost/filesystem with Xcode 8 and C++ 11

This is my first question on SO. Here goes:

I'm trying to use the boost library - specifically the filesystem part - in a C++ project in Xcode 8.

The problem:

Apple Mach-O Linker (ld) Error Group "boost::filesystem::detail::dir_itr_close(void*&, void*&)", referenced from:

"boost::filesystem::detail::directory_iterator_construct(boost::filesystem::directory_iterator&, boost::filesystem::path const&, boost::system::error_code*)", referenced from:

"boost::filesystem::detail::directory_iterator_increment(boost::filesystem::directory_iterator&, boost::system::error_code*)", referenced from:

"boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:

"boost::system::system_category()", referenced from:

"boost::system::generic_category()", referenced from:

clang: error: linker command failed with exit code 1 (use -v to see invocation)

The code:

int main(int argc, char *argv[])
{
std::string dir_path = "foo/bar";

if ( !boost::filesystem::exists( dir_path ) ) return false;

boost::filesystem::directory_iterator end_itr; // default construction 
yields past-the-end
for ( boost::filesystem::directory_iterator itr( dir_path );
itr != end_itr;
++itr )
{
std::cout << itr->path();
}

return 0;

}

What I've done:

  1. brew install boost --build-from-source --c++11 . I read somewhere that passing this --c++11 flag should fix it, and the --build-from-source is required so that the flag is actually passed to the b2 compiler.
  2. #include <boost/filesystem.hpp> in my project.
  3. Added /usr/local/include to my Header Search Paths under Build Settings . (Also tried adding /usr/local/include/boost and /usr/local/include/boost/system there, to no avail. Also, setting the recursive option on any of my Header Search Paths makes Xcode freak out).
  4. Added /usr/local/lib to my Library Search Paths .
  5. Added -lboost_system -lboost_filesystem to my Other Linker Flags . (Also tried adding -lboost_system-mt , which I saw other answers suggesting).
  6. Tried to do what this answer says, but I have no Link Libraries to Binary section in my Build Phases section. Just Target Dependencies and Compile Sources .
  7. By the way, restarting Xcode and/or my laptop didn't work. I read somewhere that someone could get it to work by reinstalling Xcode after having installed boost, but umm.. idk.

Additional information

  • Just trying to #include <boost/filesystem.hpp> causes the last 2 linker errors (about the boost::system::system_category() and generic_category() ) to occur. However, with #include <boost/regex.hpp> , the program will build successfully.
  • I also successfully installed boost following the steps here and were able run the programs from the boost/filesystem tutorial.
  • I took into consideration passing the files I need to work with as argvs[] to my program, but I could be dealing with tens of thousands of files per folder and I suspect that might get ugly.
  • OS version: 10.12.4
  • Xcode version: 8.3.2

I am very new to C++ and almost as new to Xcode. I've spent about 8h trying to get this to work.

Thanks a lot for the help!

I got the same error and this is how I fixed it.

Build Settings > Header Search Paths > (Add) /usr/local/include
Build Settings > Library Search Paths > (Add) /usr/local/lib
Build Settings > Other Linker Flags > (Add) -l boost_system -l boost_filesystem

I hope this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM