29

I have no clue why boost::filesystem::copy_file is making trouble for me.

undefined reference to `boost::filesystem::detail::copy_file

// g++ -std=c++11 test.cpp -lboost_filesystem -lboost_system -lrt -lboost_wave

#include <boost/filesystem.hpp>

int main()
{

    boost::filesystem::create_directory("aaa");
    // ok


    boost::filesystem::copy_file("f1","f2");
    // /tmp/ccNWZltB.o: In function `boost::filesystem::copy_file(boost::filesystem::path const&, boost::filesystem::path const&)':
    // test.cpp:(.text._ZN5boost10filesystem9copy_fileERKNS0_4pathES3_[_ZN5boost10filesystem9copy_fileERKNS0_4pathES3_]+0x26): undefined reference to `boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*)'
    // collect2: error: ld returned 1 exit status


    return 0;
}

I got no inspiration from the source code of boost or its help:

  inline
  void copy_file(const path& from, const path& to,   // See ticket #2925
                 BOOST_SCOPED_ENUM(copy_option) option, system::error_code& ec)
                                       {detail::copy_file(from, to, option, &ec);}

Even such a simple example does not work for me.

Platform: Linux Ubuntu 64

ar2015
  • 5,558
  • 8
  • 53
  • 110

4 Answers4

19

There is a workaround for this problem, replace

#include <boost/filesystem.hpp>

with

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

Or, preferably, add -DBOOST_NO_CXX11_SCOPED_ENUMS to your compiler flags

Antonio
  • 19,451
  • 13
  • 99
  • 197
J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
  • What to replace with this code? `boost/filesystem.hpp` is already included. – ar2015 Jan 26 '16 at 04:49
  • 2
    This is just begging for ODR violations. If you're going to define `BOOST_NO_CXX11_SCOPED_ENUMS` you need to define it globally. – ildjarn Jan 26 '16 at 07:25
  • 2
    Definitely use the `-DBOOST_NO_CXX11_SCOPED_ENUMS` it makes little sense to use the other method as the `boost_filesystem` library lacks the scoped enum implementation anyway. Its also easy to miss an include and there may be other boost libraries that use the include. Either way, thanks for the post - it definitely helped. – Voltaire Feb 26 '19 at 12:59
10

If you run into this problem make sure to include both -lboost_system and -lboost_filesystem in your call to g++

Example working Makefile

BINARY = output
FILE_OBJECTS = main.o fileLoader.o
BOOST = -lboost_system -lboost_filesystem
GCC = g++ -std=c++17
FLAGS = -Wall -pedantic -Wextra

build: $(FILE_OBJECTS)
    $(GCC) $(FLAGS) $(FILE_OBJECTS) -o $(BINARY) $(BOOST)

main.o: main.cpp fileLoader.o
    $(GCC) $(FLAGS) -c main.cpp

fileLoader.o: fileLoader.cpp
    $(GCC) $(FLAGS) -c fileLoader.cpp

clean:
    rm -rf *.o $(BINARY)

Example working code

#include <boost/filesystem.hpp>

void create_data_file(std::string file_path)
{
    boost::filesystem::path p(file_path);
    boost::filesystem::create_directory(p);
}
lukejkw
  • 1,054
  • 13
  • 19
  • 2
    In case this doesn't work and people don't read the other post adding the flag `-DBOOST_NO_CXX11_SCOPED_ENUMS` will work. See J J Hakalas post. – Voltaire Feb 26 '19 at 13:05
  • 1
    This worked for me! I'm new to g++ under VSCode, and not yet ready to wrestle with the intricacies of makefiles. But on the basis of this post, I just added the two lines **"-lboost_system",** and **"-lboost_filesystem",** to file **tasks.json** (before the **"-o",** line), and suddenly all my (linker?) error messages have disappeared. So many thanks! – FumbleFingers Jan 05 '21 at 13:43
  • This does not work for me with copy_file() (I already used -lboost_system in front of -lboost_file_system before). The problem arised after using -std=c++11. – Ernie Mur Dec 21 '21 at 14:11
1

I could not compile a file that included the header boost/filesystem.hpp either. This is how I solved it: I commented out the line boost/filesystem.hpp and all the lines that were using Boost, and then compiled the file. I then uncommented all the lines in the files and compiled again, and then it worked. I was compiling with the flag -lboost_system both times!

Bojack
  • 459
  • 1
  • 5
  • 18
0

In older boost versions it is BOOST_NO_SCOPED_ENUMS, not BOOST_NO_CXX11_SCOPED_ENUMS

see boost::filesystem::copy_file() missing symbol in c++11