我希望在我的项目中使用boost::filesystem,直到最近,这是可能的(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的文件系统功能。编译是可以的,但是链接器会抛出一个错误(见下文)。
码
#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 path\n";
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 directory\n";
}
else
cout << p << " does not exist\n";
}
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
发布于 2022-01-27 12:39:39
包含是编译时。共享库在链接时被链接。
您没有显式地告诉编译器查找标头,也没有告诉编译器在哪里找到库。这意味着使用了标准位置。
根据包管理器的不同,可能有以下符号链接:
/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 )或命令行中指示包含/库目录,如:
-I ~/custom/boost_1_77_0/ -L ~/custom/boost_1_77_0/stage/libs
选择/usr/local
的一个优点是,许多发行版支持它,并且可能将它添加到运行时加载程序的路径中(参见ldconfig
)。
https://stackoverflow.com/questions/70877684
复制相似问题