23

I'm trying to run program, using sample code of boost::filesystem on Ubuntu 12.10, but it doesn't want to build.

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

void fun(const string& dirPath);
int main()
{
    fun("/home");
    return 0;
}

void fun(const string& dirPath)
{
    path p (dirPath); 

    if (exists(p))  
    {
        if (is_regular_file(p))   
            cout << p << " size is " << file_size(p) << '\n';

        else if (is_directory(p))    
            cout << p << "is a directory\n";

        else
            cout << p << "exists, but is neither a regular file nor a directory\n";
    }
    else
        cout << p << "does not exist\n";
}

And CMake code:

project(tttest)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
FIND_PACKAGE(Boost 1.53 COMPONENTS filesystem system REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Boost_LIBRARIES})

Unfortunately it generates errors

CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::exists(boost::filesystem::path const&)':
main.cpp:(.text._ZN5boost10filesystem6existsERKNS0_4pathE[_ZN5boost10filesystem6existsERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::is_directory(boost::filesystem::path const&)':
main.cpp:(.text._ZN5boost10filesystem12is_directoryERKNS0_4pathE[_ZN5boost10filesystem12is_directoryERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::is_regular_file(boost::filesystem::path const&)':
main.cpp:(.text._ZN5boost10filesystem15is_regular_fileERKNS0_4pathE[_ZN5boost10filesystem15is_regular_fileERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::file_size(boost::filesystem::path const&)':
main.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

What is the reason of this problem and how to solve it?

4
  • 2
    try to build with auto-detected boost version: FIND_PACKAGE(Boost COMPONENTS filesystem system REQUIRED)
    – podshumok
    Commented Mar 26, 2013 at 12:07
  • I added cmake to the tags. This problem appears to be cmake related.
    – drescherjm
    Commented Mar 26, 2013 at 13:41
  • 1
    Is boost 1.53 recognized by your version of CMake? You may want to print out what ${Boost_LIBRARIES} contains in your CMakeLists.txt to better understand what is happening. MESSAGE( status ${Boost_LIBRARIES} ) should do that.
    – drescherjm
    Commented Mar 26, 2013 at 13:44
  • Could you set the choosed answer please?
    – lalebarde
    Commented Jul 11, 2020 at 6:00

5 Answers 5

28

Boost filesystem is one of the Boost library that have some ABI problem relative to function signature change due to C++0x or C++11 (cf old Boost trac-ticket).

You have three solutions:

  1. Inhibit C++11 scoped enums in concerned Boost header files included in your programs with #include (cf http://www.ridgesolutions.ie/index.php/2013/05/30/boost-link-error-undefined-reference-to-boostfilesystemdetailcopy_file/):

     #define BOOST_NO_CXX11_SCOPED_ENUMS
     #include <boost/filesystem.hpp>
     #undef BOOST_NO_CXX11_SCOPED_ENUMS
    

But this solution is not a complete one and I read it does not work for everybody.

  1. Build BOOST with the C++11 option (the same options you use for your application): http://hnrkptrsn.github.io/2013/02/26/c11-and-boost-setup-guide

I read also it does not work for everybody.

  1. Set up a cross compiler dedicated to your application where you rebuild all the libraries you need in a dedicated environment. That ensures coherence plus stability plus more maintainability, and is certainly the solution to recommend. I have not read if it has been tested - probably yes, and probably it works. Anyway, cross compiling is well mastered now in computer science. You will find many good tutorials and support for it. In Linux Gentoo, they have the marvelous sys-devel/crossdev package that makes it very easy.

In my own case, solution 1 has solved the problem. As soon as I encounter another one, I will switch to solution 3. So, I have not yet tested it.

2
  • Just one improvement in above if you are using libboost below 1.51 use need to use #define BOOST_NO_SCOPED_ENUMS #include "boost/filesystem.hpp" #undef BOOST_NO_SCOPED_ENUMS Commented Oct 26, 2015 at 18:47
  • 3
    Note that for option 1, all Boost includes must have BOOST_NO_CXX11_SCOPED_ENUMS defined, or ODR violations will surely follow.
    – ildjarn
    Commented Jan 26, 2016 at 15:53
9

You need to add libboost_filesystem library when linking. Or libboost_filesystem-mt if your application is multi-threaded. Like this:

g++ -o file -lboost_filesystem-mt source_file.cpp
1
  • 2
    And in case you are using CMake that'd be target_link_libraries (my_executable ${Boost_LIBRARIES})
    – Adrian
    Commented Apr 26, 2017 at 10:38
3

The solution that worked for me is to compile with "-c" and then create the executable like this:

g++ -c -o main.o main.cpp
g++ -o my_prog main.o -lboost_system -lboost_filesystem
1

For some boost modules, you have to compile libraries and link them (using bootstrap.sh). In your case, you have to compile and link Filesystem, and probalbly System too

Have a look here

For example:

  • ./bootstrap.sh (bjam)
  • rm -rf bin.v2 stage (between 2 bjam commands)
  • ./bjam release toolset=gcc address-model=64 cxxflags=-fPIC
  • ./bjam debug toolset=gcc address-model=64 cxxflags=-fPIC

If you are linking on Windows, you don't have to manually link your libraries, since they are automatically linked using pragma. On Linux, you have to do it.

According to documentation, these modules need you to acquire or build a library :

  • Boost.Filesystem
  • Boost.GraphParallel
  • Boost.IOStreams
  • Boost.MPI
  • Boost.ProgramOptions
  • Boost.Python
  • Boost.Regex
  • Boost.Serialization
  • Boost.Signals
  • Boost.System
  • Boost.Thread
  • Boost.Wave
1
  • 1
    This is on linux and I expect boost would have been installed via the package manager in ubuntu.
    – drescherjm
    Commented Mar 26, 2013 at 13:42
1

You need to add the following libraries:

g++ -o file -lboost_system -lboost_filesystem sourcefile.cpp

If you use a Makefile:

CC=gcc
CXX=g++
PROG = program
CXXFLAGS := -std=c++1y -g -Wall
LDFLAGS = -L/usr/local/lib -L/usr/lib/x86_64-linux-gnu
LIBS= -lboost_system -lboost_filesystem

SRCS= main.cpp

OBJS=$(subst .cpp,.o,$(SRCS))

all: $(OBJS)
     $(CXX) $(CXXFLAGS) -o $(PROG) $(OBJS) $(LIBS) $(LDFLAGS)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.