【问题标题】:Undefined reference to boost::system::generic_category() although libs are given to g++对 boost::system::generic_category() 的未定义引用,尽管库被提供给 g++
【发布时间】:2019-03-26 13:22:27
【问题描述】:

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

我尝试在 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

【问题讨论】:

    标签: c++ boost linker


    【解决方案1】:

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

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

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

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

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


    更多信息:

    【讨论】:

    • 谢谢,我找到了有关链接器标志有时很重要的顺序的信息,但从未强调过来源可能同样重要的事实。
    猜你喜欢
    • 2021-05-16
    • 2013-08-14
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2012-06-30
    • 2012-02-10
    相关资源
    最近更新 更多