我正在运行 OS X 10.11 (El Captain) 并尝试使用 C++ 和 boost 编译程序。具体来说,我正在尝试使用 boost::program_options
类。以下代码将无法编译:
#include <iostream>
#include <boost/program_options.hpp>
int main(int argc, const char *argv[]) {
boost::program_options::options_description desc("Options");
return 0;
}
并返回错误:
Undefined symbols for architecture x86_64:
"boost::program_options::options_description::m_default_line_length", referenced from:
_main in test.o
processArgs(int, char const**) in test.o
"boost::program_options::options_description::options_description(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned int, unsigned int)", referenced from:
_main in test.o
processArgs(int, char const**) in test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我使用 brew、Xcode 7.0.1 安装了 boost 1.58.0。我在构建选项下使用默认编译器 (Apple LLVM 7.0),并在 header 搜索路径和 /usr/中指定了
。此外,我检查了目录以确保存在正确的文件。/usr/local/Cellar/boost/1.58.0/include
库搜索路径中的 local/Cellar/boost/1.58.0/lib
如有任何建议,我们将不胜感激!谢谢,如果您还有其他问题,请告诉我。
最佳答案
它看起来像是在编译,但它不是链接。我相信您需要在 Link Binary With Libraries 下将 libboost_program_options.dylib
添加到您的应用程序。参见 this page了解详情(第 4 步)。
我添加库的方式是选择我的目标,然后选择 Link Binary With Libraries,然后选择 Add Other。对我来说最难的部分是导航到/usr/local 文件夹(我是 Xcode 开发的新手)。我这样做的方法是转到 Finder,然后选择转到 -> 转到文件夹...然后转到文件夹/usr/local,然后将其从窗口底部拖到收藏夹。当您在 Xcode 中单击“添加其他”时,它就会显示出来,您可以轻松导航到/usr/local/lib 目录并找到 boost 库。
关于c++ - 无法使用Xcode通过boost成功编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33060323/