gpt4 book ai didi

c++ - 未定义对 boost::system::generic_category() 的引用,尽管库已提供给 g++

转载 作者:行者123 更新时间:2023-11-30 04:49:33 30 4
gpt4 key购买 nike

我在这里缺少哪些链接器标志?

我尝试在 Ubuntu 18.04 LTS 上编译它但它失败了,它在 Debian 9 docker 镜像中运行:

#include <boost/dll.hpp>

// Trying to compile it with:
// g++ -o program -lboost_filesystem -ldl -lboost_system program.cpp

int main() {
boost::dll::program_location();
return 0;
}

我得到的错误是:

/tmp/ccKlWUUd.o: In function `__static_initialization_and_destruction_0(int, int)':
program.cpp:(.text+0x68): undefined reference to `boost::system::generic_category()'
program.cpp:(.text+0x74): undefined reference to `boost::system::generic_category()'
program.cpp:(.text+0x80): undefined reference to `boost::system::system_category()'
/tmp/ccKlWUUd.o: In function `boost::system::error_code::error_code()':
program.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
/tmp/ccKlWUUd.o: In function `boost::system::error_category::std_category::equivalent(int, std::error_condition const&) const':
program.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition[_ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition]+0xb8): undefined reference to `boost::system::generic_category()'
program.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition[_ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition]+0xf3): undefined reference to `boost::system::generic_category()'
/tmp/ccKlWUUd.o: In function `boost::system::error_category::std_category::equivalent(std::error_code const&, int) const':
program.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0xb8): undefined reference to `boost::system::generic_category()'
program.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0xf3): undefined reference to `boost::system::generic_category()'
program.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0x1d2): undefined reference to `boost::system::generic_category()'
/tmp/ccKlWUUd.o: In function `boost::dll::detail::report_error(boost::system::error_code const&, char const*)':
program.cpp:(.text._ZN5boost3dll6detail12report_errorERKNS_6system10error_codeEPKc[_ZN5boost3dll6detail12report_errorERKNS_6system10error_codeEPKc]+0x2a): undefined reference to `dlerror'
/tmp/ccKlWUUd.o: In function `boost::filesystem::read_symlink(boost::filesystem::path const&, boost::system::error_code&)':
program.cpp:(.text._ZN5boost10filesystem12read_symlinkERKNS0_4pathERNS_6system10error_codeE[_ZN5boost10filesystem12read_symlinkERKNS0_4pathERNS_6system10error_codeE]+0x36): undefined reference to `boost::filesystem::detail::read_symlink(boost::filesystem::path const&, boost::system::error_code*)'
collect2: error: ld returned 1 exit status

这里的系统是:

gcc: 7.0.3
boost: 1.65.1
libc6: 2.73

最佳答案

您的构建命令顺序错误,顺序很重要。

GCC 从左到右读取,在它知道需要符号时从库中获取符号。当您将 program.cpp 放在最后时,在所有列出的库都已被识别并丢弃之前,您不会知道它。

program.cpp 放在第一位,然后是它需要的库。

g++ -o program program.cpp -lboost_filesystem -ldl -lboost_system

是的,这有点奇怪。 (更奇怪的是,它在 Debian 上运行!虽然显然只有一些“最近的”Linuxy 发行版默认 --as-needed on,这就是导致您看到的行为的原因,表明该行为不一定保证。也许 Debian 9 完全不这样做。)


更多信息:

关于c++ - 未定义对 boost::system::generic_category() 的引用,尽管库已提供给 g++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55358251/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com