首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Boost::filesystem::directory_iterator在升级到v1.78.0后导致链接器错误

Boost::filesystem::directory_iterator在升级到v1.78.0后导致链接器错误
EN

Stack Overflow用户
提问于 2022-01-27 11:19:46
回答 1查看 552关注 0票数 1

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

代码语言:javascript
运行
复制
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的文件系统功能。编译是可以的,但是链接器会抛出一个错误(见下文)。

代码语言:javascript
运行
复制
#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生成)

代码语言:javascript
运行
复制
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

误差

代码语言:javascript
运行
复制
./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下重新安装了数据包。帮助遇到同样问题的人的快速演练:

代码语言:javascript
运行
复制
// 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-27 12:39:39

包含是编译时。共享库在链接时被链接。

您没有显式地告诉编译器查找标头,也没有告诉编译器在哪里找到库。这意味着使用了标准位置。

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

代码语言:javascript
运行
复制
/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 )或命令行中指示包含/库目录,如:

代码语言:javascript
运行
复制
 -I ~/custom/boost_1_77_0/ -L ~/custom/boost_1_77_0/stage/libs

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70877684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档