无法使用Boost.Filesystem链接程序

ada*_*hie 20 c++ boost cmake

我正在尝试运行程序,在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";
}
Run Code Online (Sandbox Code Playgroud)

和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})
Run Code Online (Sandbox Code Playgroud)

不幸的是,它会产生错

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
Run Code Online (Sandbox Code Playgroud)

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

lal*_*rde 24

Boost文件系统是Boost库之一,由于C++ 0x或C++ 11而导致相对于函数签名更改存在一些ABI问题.cf Boost ticket:https://svn.boost.org/trac/boost/ticket/6779

你有三个解决方案:

  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
    
    Run Code Online (Sandbox Code Playgroud)

    但是这个解决方案并不是一个完整的解决方案,而且我认为它对每个人都不起作用.

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

    我读到它也不适用于所有人.

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

就我自己而言,解决方案1已经解决了这个问题.一旦遇到另一个,我将切换到解决方案3.所以,我还没有测试过它.

  • 请注意,对于选项1,**所有**Boost包含必须定义`BOOST_NO_CXX11_SCOPED_ENUMS`,否则肯定会出现ODR违规. (3认同)

小智 9

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

g++ -o file -lboost_filesystem-mt source_file.cpp
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用的 CMake 是`target_link_libraries (my_executable ${Boost_LIBRARIES})` (2认同)