2

I'm running into problems with Boost's filesystem lib (1.60.0). After spending a couple of hours tearing through my code assuming it was something I was doing wrong, I tried running Boost's own filesystem examples and had the same problem. All of this works fine when compiling with gcc on OSX, but not on Ubuntu 14.04.

I tried defining BOOST_NO_CXX11_SCOPED_ENUMS both in the code, and also tried it as an argument when running g++. I also tried removing -std=c++11 (I saw in one case, that seemed to help someone who ran into this problem). Regardless, it always fails on Ubuntu. The Boost example I'm using is this:

// tut1
#include <iostream>

#define BOOST_NO_CXX11_SCOPED_ENUMS
#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;
}

And the result of trying to compile that looks like this:

vagrant@testing:~/boost_fs_test$ g++ -I/boost/1_60_0/include -L/boost/1_60_0/lib -lboost_system -lboost_filesystem test2.cpp -o test2
/tmp/cck0AVVX.o: In function `__static_initialization_and_destruction_0(int, int)':
test2.cpp:(.text+0x105): undefined reference to `boost::system::generic_category()'
test2.cpp:(.text+0x111): undefined reference to `boost::system::generic_category()'
test2.cpp:(.text+0x11d): undefined reference to `boost::system::system_category()'
/tmp/cck0AVVX.o: In function `boost::filesystem::file_size(boost::filesystem::path const&)':
test2.cpp:(.text._ZN5boost10filesystem9file_sizeERKNS0_4pathE[_ZN5boost10filesystem9file_sizeERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)'
collect2: error: ld returned 1 exit status

vagrant@testing:~/boost_fs_test$ g++ -std=c++11 -I/boost/1_60_0/include -L/boost/1_60_0/lib -lboost_system -lboost_filesystem test2.cpp -o test2
/tmp/cceGCjpc.o: In function `__static_initialization_and_destruction_0(int, int)':
test2.cpp:(.text+0x105): undefined reference to `boost::system::generic_category()'
test2.cpp:(.text+0x111): undefined reference to `boost::system::generic_category()'
test2.cpp:(.text+0x11d): undefined reference to `boost::system::system_category()'
/tmp/cceGCjpc.o: In function `boost::filesystem::file_size(boost::filesystem::path const&)':
test2.cpp:(.text._ZN5boost10filesystem9file_sizeERKNS0_4pathE[_ZN5boost10filesystem9file_sizeERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)'
collect2: error: ld returned 1 exit status

I triple-checked how I installed Boost and everything looks correct. The libs are definitely in the right place:

-rw-rw-r-- 1 vagrant vagrant 237886 Jan  8 08:41 libboost_filesystem.a
lrwxrwxrwx 1 vagrant vagrant     29 Jan  8 08:40 libboost_filesystem.so -> libboost_filesystem.so.1.60.0
-rwxrwxr-x 1 vagrant vagrant 126186 Jan  8 08:40 libboost_filesystem.so.1.60.0
-rw-rw-r-- 1 vagrant vagrant  49226 Jan  8 08:41 libboost_system.a
lrwxrwxrwx 1 vagrant vagrant     25 Jan  8 08:40 libboost_system.so -> libboost_system.so.1.60.0
-rwxrwxr-x 1 vagrant vagrant  20469 Jan  8 08:40 libboost_system.so.1.60.0

The same exact code compiles fine on OSX, though:

jack-burton:boost_fs fny$ g++ -std=c++11 -I/usr/local/Cellar/boost/1.60.0_1/include -L/usr/local/Cellar/boost/1.60.0_1/lib -lboost_system -lboost_filesystem test2.cpp -o test2
jack-burton:boost_fs fny$ ls
test2       test2.cpp
jack-burton:boost_fs fny$ ./test2 test2.cpp
test2.cpp 321

I'm not really sure what I'm missing here. For the sake of curiosity, I tried using apt to install Boost (which installed 1.54) and had the same problem.

2 Answers 2

5

Alright, I figured this out with some frustrating trial and error. :)

On both CentOS and Ubuntu, I got it working by installing Boost as follows:

sudo ./b2 install link=static --with-system --with-filesystem

Then, compiling with:

g++ -std=c++11 -I/usr/local/include -L/usr/local/lib test.cpp -lboost_system -lboost_filesystem -o test

The key seems to be the location of the libs in the gcc command. Putting them after the source files seemed to do the trick. I was at the point of ripping Boost out of the project entirely, but thankfully this worked and saved me from having to rewrite a fair bit of code. It's probably an incredibly stupid mistake, but TIL...

Sign up to request clarification or add additional context in comments.

Comments

0

it looks like you are linking against boost compiled as a shared library, try compiling with -DBOOST_ALL_DYN_LINK or explicitly compiling boost as a static library ./b2 link=static

5 Comments

No luck with either, it seems. I tried BOOST_ALL_DYN_LINK first. Then I tried adding link=static when running b2 again. I see it has .a files in the lib folder now, but it's still failing to compile the test code with the same error as before.
are you sure the include paths are correct? those paths in your build command seem to indicate that you're installing boost to the root directory and rearranging the include paths, which seems unusual. have you tried doing it the normal way sudo ./b2 install?
Yes, the /boost path I was trying was definitely correct. I had not tried installing it without specifying the install location, so I just tried it using sudo ./b2 install without any other parameters. With and without explicitly passing the path to include and lib under /usr/local when trying to compile the test program, the same problem occurs. :( The appropriate libs are definitely there under /usr/local/lib.
For the sake of curiosity, I tried this on a different VM. I spun up an Ubuntu 16.04 box with Vagrant, installed Boost 1.60, and hit the same problem. I tried using Boost 1.63 and had the same problem, also. EDIT: It compiled on the CentOS 7 VM, but the executable fails to run: error while loading shared libraries: libboost_system.so.1.60.0: cannot open shared object file: No such file or directory. When I try to force it to link to the .a files, it fails with the same error that I was getting on Ubuntu.
Ok, so I got it working on CentOS but it still doesn't work on Ubuntu. I deleted all of the boost libs from /usr/local/lib, and re-ran b2: sudo ./b2 install link=static --with-system --with-filesystem. This installed only the static libs, so it compiles (and runs) correctly when I build with: g++ -std=c++11 -I/usr/local/include -L/usr/local/lib -lboost_system -lboost_filesystem test.cpp -o test. This approach does not work with Ubuntu though. Argh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.