C++ 无法使用 Boost.Filesystem 链接程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15634114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:34:15  来源:igfitidea点击:

Can't link program using Boost.Filesystem

c++boostcmake

提问by adashie

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

我正在尝试使用 Ubuntu 12.10 上的 boost::filesystem 示例代码运行程序,但它不想构建。

#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:

和 CMake 代码:

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?

这个问题的原因是什么以及如何解决?

回答by lalebarde

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 Boost ticket : https://svn.boost.org/trac/boost/ticket/6779

Boost 文件系统是 Boost 库之一,由于 C++0x 或 C++11,与函数签名更改相关的 ABI 问题。cf Boost 票:https: //svn.boost.org/trac/boost/ticket/6779

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.

  2. 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.

  3. 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.

  1. 使用 #include 禁止程序中包含的相关 Boost 头文件中的 C++11 范围枚举(参见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
    

    但是这个解决方案不是一个完整的解决方案,我读到它并不适用于所有人。

  2. 使用 C++11 选项(与您的应用程序使用的选项相同)构建 BOOST:http: //hnrkptrsn.github.io/2013/02/26/c11-and-boost-setup-guide

    我也读过它并不适合所有人。

  3. 设置专用于您的应用程序的交叉编译器,您可以在其中在专用环境中重建所需的所有库。这确保了一致性和稳定性以及更多的可维护性,当然是推荐的解决方案。我没有读过它是否已经过测试 - 可能是的,并且可能有效。无论如何,交叉编译现在在计算机科学中已经很好地掌握了。你会发现很多很好的教程和对它的支持。在 Linux Gentoo 中,他们有很棒的 sys-devel/crossdev 包,这使它变得非常容易。

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.

在我自己的情况下,解决方案 1 已经解决了问题。一遇到另一个,我就会切换到解决方案3。所以,我还没有测试过。

回答by Nicolai Tufar

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

链接时需要添加 libboost_filesystem 库。或者 libboost_filesystem-mt 如果您的应用程序是多线程的。像这样:

g++ -o file -lboost_filesystem-mt source_file.cpp

回答by Morad

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

对我有用的解决方案是使用“-c”进行编译,然后像这样创建可执行文件:

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

回答by Akinna

You need to add the following libraries:

您需要添加以下库:

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

If you use a Makefile:

如果您使用 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)

回答by Cyrille

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

对于某些 boost 模块,您必须编译库链接它们(使用 bootstrap.sh)。在您的情况下,您必须编译和链接Filesystem,并且可能System也是

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
  • ./bootstrap.sh (bjam)
  • rm -rf bin.v2 阶段(在 2 个 bjam 命令之间)
  • ./bjam 发布工具集=gcc 地址-模型=64 cxxflags=-fPIC
  • ./bjam 调试工具集=gcc 地址-模型=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.

如果您在 Windows 上进行链接,则不必手动链接您的库,因为它们是使用 pragma 自动链接的。在 Linux 上,你必须这样做。

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
  • Boost.Filesystem
  • Boost.GraphParallel
  • Boost.IOStreams
  • 增强型MPI
  • Boost.ProgramOptions
  • Boost.Python
  • Boost.Regex
  • Boost.Serialization
  • 升压信号
  • 升压系统
  • Boost.Thread
  • Boost.Wave