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

标签 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";
}

以及 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})

不幸的是它会产生错误

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

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

最佳答案

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

你有三个解决方案:

  1. 使用#include (cf http://www.ridgesolutions.ie/index.php/2013/05/30/boost-link-error-undefined-reference-to-boostfilesystemdetailcopy_file/ ) 抑制程序中包含的相关 Boost 头文件中的 C++11 范围枚举:

    #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 包,这让它变得非常简单。

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

关于c++ - 无法使用 Boost.Filesystem 链接程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15634114/

相关文章:

c++ - 创建中间件以使相机兼容 ONVIF

c++ - 如何从我的 jamfile 中将 cxxflags 传递给 Boost 库?

c++ - boost::copy_graph 的vertex_copy 是如何工作的?

c++ - 找不到 LMDB(缺少 : LMDB_INCLUDE_DIR LMDB_LIBRARIES)

python - Boost:Python: undefined symbol :_ZN5boost6python15instance_holder8allocateEP7_objectmmm

c++ - 如何比较 time_t 和 std::filesystem::file_time_type

c++ - 保持插入顺序的 C+11 关联容器?

c++ - 这个同步对象实现线程安全吗?

c++ - 为 std::pair<int, int> 重载运算符>>

cmake - 在 CMake 中创建目录