Can#39;t link program using Boost.Filesystem(无法使用 Boost.Filesystem 链接程序)
问题描述
我正在尝试在 Ubuntu 12.10 上使用 boost::filesystem 的示例代码运行程序,但它不想构建.
I'm trying to run program, using sample code of boost::filesystem on Ubuntu 12.10, but it doesn't want to build.
#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) << '
';
else if (is_directory(p))
cout << p << "is a directory
";
else
cout << p << "exists, but is neither a regular file nor a directory
";
}
else
cout << p << "does not exist
";
}
和 CMake 代码:
And CMake code:
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
这个问题的原因是什么,如何解决?
What is the reason of this problem and how to solve it?
推荐答案
Boost 文件系统是 Boost 库之一,由于 C++0x 或 C++11 导致的函数签名更改存在一些 ABI 问题.cf Boost 票:https://svn.boost.org/trac/boost/ticket/6779
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 头文件中使用 #include 禁止 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
但是这个解决方案并不是一个完整的解决方案,我读到它并不适用于所有人.
But this solution is not a complete one and I read it does not work for everybody.
使用 C++11 选项(与您的应用程序使用的选项相同)构建 BOOST:http://hnrkptrsn.github.io/2013/02/26/c11-and-boost-setup-guide
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.
设置专用于您的应用程序的交叉编译器,您可以在其中在专用环境中重建所需的所有库.这确保了一致性和稳定性以及更多的可维护性,当然是推荐的解决方案.我还没有读过它是否已经过测试 - 可能是的,并且可能有效.无论如何,交叉编译现在在计算机科学中已经很好地掌握了.你会发现很多好的教程和对它的支持.在 Linux Gentoo 中,他们有很棒的 sys-devel/crossdev 包,这使它变得非常容易.
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 已经解决了问题.一遇到其他的就切换到方案三.所以,我还没有测试过.
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.
这篇关于无法使用 Boost.Filesystem 链接程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法使用 Boost.Filesystem 链接程序
基础教程推荐
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01