【问题标题】:linking error for boost::program_optionsboost::program_options 的链接错误
【发布时间】:2015-03-30 19:05:51
【问题描述】:

我无法找出使用 boost::program_options 的链接错误。这是一个示例 C++ 代码:

# sample_code.cpp
#include <boost/program_options.hpp>
int main()
{
    boost::program_options::options_description description("Test");
}

我还在 Yosemite 10.10.2 上使用 MacPorts 安装了 boost 1.57.0。这是相关的库:

/opt/local/lib/libboost_program_options-mt.a

让我们编译和链接这段代码:

alias g++='/opt/local/bin/g++-mp-5 -std=gnu++14 -I/opt/local/include -L/opt/local/lib'

g++ sample_code.cpp -lboost_program_options-mt

在链接过程中失败:

Undefined symbols for architecture x86_64:
"boost::program_options::options_description::options_description(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, unsigned int)"
ld: symbol(s) not found for architecture x86_64

有什么想法吗?

【问题讨论】:

  • 如果你在别名使用中颠倒编译器参数的顺序是否有效?如果是这样,stackoverflow.com/a/24675715/962089
  • 并非如此。我还尝试了 clang++ 和 CMake 及其所有与 boost 相关的参数,但没有任何帮助!
  • “不是真的”是什么意思?库确实.cpp之后。
  • 我修复了上面的代码并再次测试,但错误是一样的: $ g++ sample_code.cpp -lboost_program_options-mt Undefined symbols for architecture x86_64: "boost::program_options::options_description:: options_description(std::__cxx11::basic_string, std::allocator > const&, unsigned int, unsigned int)”,引用自:_main in ccIZf7tk.o ld: symbol( s) 未找到架构 x86_64 collect2:错误:ld 返回 1 退出状态
  • macports 是用 g++ 还是 clang++ 编译 boost 的?两者都需要使用 same c++ 标准库进行编译,否则会出现类似的链接错误。 clang++ 使用 libc++。 g++(正确的 gcc,而不是 clang 包装器)使用 libstdc++。

标签: c++ c++11 boost boost-program-options


【解决方案1】:

解决方案很简单:您必须使用相同版本的编译器(或者至少它们应该兼容)来编译 boost 和您的程序。当我的 boost 使用 GCC 4.8.3 编译并且我的代码使用 GCC 5.3.0 编译时,我遇到了链接错误。使用 GCC 5.3.0 构建 boost 后,链接问题消失了。

【讨论】:

    【解决方案2】:

    正如 Petesh 在上面的 cmets 中指出的那样,问题原来是 MacPorts 如何使用 clang++ 而不是 g++ 构建 boost

    不幸的是,MacPorts 不为boost 提供任何gcc 内置变体!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 2013-01-02
      • 2014-05-18
      相关资源
      最近更新 更多