boost :: program_options的链接错误

时间:2015-03-30 19:05:51

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

我无法弄清楚使用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

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

很遗憾,MacPorts不为gcc提供任何boost内置版本!

相关问题