【发布时间】:2017-01-08 08:51:02
【问题描述】:
我遇到了 Boost 文件系统库 (1.60.0) 的问题。在假设我做错了什么花了几个小时撕毁我的代码后,我尝试运行 Boost 自己的文件系统示例并遇到了同样的问题。在 OSX 上使用 gcc 编译时,所有这些都可以正常工作,但在 Ubuntu 14.04 上则不行。
我尝试在代码中定义 BOOST_NO_CXX11_SCOPED_ENUMS,并在运行 g++ 时尝试将其作为参数。我还尝试删除 -std=c++11 (我在一种情况下看到,这似乎对遇到此问题的人有所帮助)。无论如何,它在 Ubuntu 上总是失败。我正在使用的 Boost 示例是这样的:
// tut1
#include <iostream>
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: tut1 path\n";
return 1;
}
std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
return 0;
}
尝试编译的结果如下:
vagrant@testing:~/boost_fs_test$ g++ -I/boost/1_60_0/include -L/boost/1_60_0/lib -lboost_system -lboost_filesystem test2.cpp -o test2
/tmp/cck0AVVX.o: In function `__static_initialization_and_destruction_0(int, int)':
test2.cpp:(.text+0x105): undefined reference to `boost::system::generic_category()'
test2.cpp:(.text+0x111): undefined reference to `boost::system::generic_category()'
test2.cpp:(.text+0x11d): undefined reference to `boost::system::system_category()'
/tmp/cck0AVVX.o: In function `boost::filesystem::file_size(boost::filesystem::path const&)':
test2.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
vagrant@testing:~/boost_fs_test$ g++ -std=c++11 -I/boost/1_60_0/include -L/boost/1_60_0/lib -lboost_system -lboost_filesystem test2.cpp -o test2
/tmp/cceGCjpc.o: In function `__static_initialization_and_destruction_0(int, int)':
test2.cpp:(.text+0x105): undefined reference to `boost::system::generic_category()'
test2.cpp:(.text+0x111): undefined reference to `boost::system::generic_category()'
test2.cpp:(.text+0x11d): undefined reference to `boost::system::system_category()'
/tmp/cceGCjpc.o: In function `boost::filesystem::file_size(boost::filesystem::path const&)':
test2.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 的,一切看起来都正确无误。这些库肯定在正确的位置:
-rw-rw-r-- 1 vagrant vagrant 237886 Jan 8 08:41 libboost_filesystem.a
lrwxrwxrwx 1 vagrant vagrant 29 Jan 8 08:40 libboost_filesystem.so -> libboost_filesystem.so.1.60.0
-rwxrwxr-x 1 vagrant vagrant 126186 Jan 8 08:40 libboost_filesystem.so.1.60.0
-rw-rw-r-- 1 vagrant vagrant 49226 Jan 8 08:41 libboost_system.a
lrwxrwxrwx 1 vagrant vagrant 25 Jan 8 08:40 libboost_system.so -> libboost_system.so.1.60.0
-rwxrwxr-x 1 vagrant vagrant 20469 Jan 8 08:40 libboost_system.so.1.60.0
同样的代码在 OSX 上编译得很好,但是:
jack-burton:boost_fs fny$ g++ -std=c++11 -I/usr/local/Cellar/boost/1.60.0_1/include -L/usr/local/Cellar/boost/1.60.0_1/lib -lboost_system -lboost_filesystem test2.cpp -o test2
jack-burton:boost_fs fny$ ls
test2 test2.cpp
jack-burton:boost_fs fny$ ./test2 test2.cpp
test2.cpp 321
我不确定我在这里缺少什么。出于好奇,我尝试使用 apt 安装 Boost(安装了 1.54)并遇到了同样的问题。
【问题讨论】: