Boost::filesystem::directory_迭代器在升级到v1.78.0后导致链接器错误



我想在我的项目中使用boost::文件系统,直到最近这才是可能的(v1.65.1(。几天前,我不得不将我的boost安装升级到1.78.0,并按照网站上的说明从源代码构建库。我执行了以下行:

wget https://boostorg.jfrog.io/artifactory/main/release/1.78.0/source/boost_1_78_0.tar.gz
tar xzvf boost_1_78_0.tar.gz
cd boost_1_78_0/
./bootstrap.sh --prefix=/usr/
./b2
sudo ./b2 install

boost的测试代码利用了boost的文件系统功能。编译很好,但链接器会抛出一个错误(请参阅下文(。

代码

#include <iostream>
#include <boost/filesystem.hpp>
using std::cout;
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: tut3 pathn";
return 1;
}
path p(argv[1]);
try
{
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 containing:n";
for (directory_entry const& x : directory_iterator(p))
cout << "    " << x.path() << 'n';
}
else
cout << p << " exists, but is not a regular file or directoryn";
}
else
cout << p << " does not existn";
}
catch (filesystem_error& ex)
{
cout << ex.what() << 'n';
}
return 0;
}

编译和链接器命令(由eclipse生成(

g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/main.d" -MT"src/main.d" -o "src/main.o" "../src/main.cpp"
g++  -o "test"  ./src/main.o   -lboost_system -lboost_filesystem

错误

./src/main.o: In function »boost::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const&, boost::filesystem::directory_options)«:
/usr/include/boost/filesystem/directory.hpp:326: Warning: undefined reference to »boost::filesystem::detail::directory_iterator_construct(boost::filesystem::directory_iterator&, boost::filesystem::path const&, unsigned int, boost::system::error_code*)«
makefile:45: recipe for target 'test' failed
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
"make all" terminated with exit code 2. Build might be incomplete.

如果我删除了包含boost::filesystem::directory_iterator的行,链接就起作用了。我不知道如何解决这个问题。我最初认为旧版本的boost可能会干扰,因为它仍然驻留在/usr/lib/x86_64-linux-gnu/libboost_filesystem.so.1.65.1中,但当检查文件中包含的版本时,它会显示新版本。

这里发生了什么?

解决方案

最后,我删除了原始安装和新版本,并在/usr/local下重新安装了数据包。帮助遇到相同问题的人的快速演练:

// remove the dirs under <prefix>/lib/libboost* and <prefix>/include/boost* first
sudo apt purge -y libboost-all-dev libboost*
sudo apt autoremove
// then either install the package via the manager or copy the sources to /usr/local/ or another suitable place
sudo apt install libboost-all-dev    // option with aptitude

includes是编译时。共享库在链接时进行链接。

您没有明确告诉查找头,也没有告诉编译器库的位置。这意味着使用标准位置。

根据您的软件包管理器的不同,可能会有以下符号链接:

/usr/lib/x86_64-linux-gnu/libboost_filesystem.so.1.65.1
/usr/lib/x86_64-linux-gnu/libboost_filesystem.so -> libboost_filesystem.so.1.65.1

通常情况下,用文件覆盖已安装软件包的一部分是一个坏主意。不在最后,因为例如,这样的符号链接可能不会更新,或者如果更新了,它们可能会破坏您安装的许多依赖关系。

通常,更喜欢使用安全前缀(例如/usr/local(或本地构建,并在构建工具(如Eclipse(或命令行(如(中指示include/library目录

-I ~/custom/boost_1_77_0/ -L ~/custom/boost_1_77_0/stage/libs

选择/usr/local的一个优点是,许多发行版都支持它,并且可能会将它添加到运行时加载程序的路径中(请参阅ldconfig(。

最新更新