C++ BOOST undefined reference to `boost::filesystem::detail::copy_file

前端 未结 4 583
执笔经年
执笔经年 2020-12-15 04:34

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

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

相关标签:
4条回答
  • 2020-12-15 05:23

    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

    0 讨论(0)
  • 2020-12-15 05:31

    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

    0 讨论(0)
  • 2020-12-15 05:32

    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!

    0 讨论(0)
  • 2020-12-15 05:38

    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);
    }
    
    0 讨论(0)
提交回复
热议问题