fny*_*y82 2 c++ ubuntu boost c++11
我遇到了 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;
}
Run Code Online (Sandbox Code Playgroud)
尝试编译的结果如下所示:
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
Run Code Online (Sandbox Code Playgroud)
我三次检查了 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
Run Code Online (Sandbox Code Playgroud)
不过,同样的代码在 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
Run Code Online (Sandbox Code Playgroud)
我不太确定我在这里缺少什么。出于好奇,我尝试使用 apt 安装 Boost(安装了 1.54)并遇到了同样的问题。
好吧,我通过一些令人沮丧的尝试和错误解决了这个问题。:)
在 CentOS 和 Ubuntu 上,我通过安装 Boost 使其正常工作,如下所示:
sudo ./b2 install link=static --with-system --with-filesystem
然后,编译:
g++ -std=c++11 -I/usr/local/include -L/usr/local/lib test.cpp -lboost_system -lboost_filesystem -o test
关键似乎是 gcc 命令中库的位置。将它们放在源文件之后似乎可以解决问题。我当时正打算将 Boost 完全从项目中剔除,但幸运的是,这确实有效,并且使我不必重写大量代码。这可能是一个极其愚蠢的错误,但直到……