无法使用Xcode成功编译boost

Unable to successfully compile with boost using Xcode

本文关键字:成功 编译 boost Xcode      更新时间:2023-10-16

我正在运行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)

我有boost 1.58.0安装使用brew, Xcode 7.0.1。我在构建选项下使用默认编译器(Apple LLVM 7.0),并在标题搜索路径中指定/usr/local/Cellar/boost/1.58.0/include,在库搜索路径中指定/usr/local/Cellar/boost/1.58.0/lib。此外,我已经检查了目录,以确保正确的文件存在。

任何建议将不胜感激!谢谢,如果还有什么问题,请告诉我。

看起来像是在编译,但不是链接。我相信您需要将libboost_program_options.dylib添加到您的应用程序的链接二进制库。详情请参阅本页(步骤4)。

我添加库的方式是选择我的目标,然后链接二进制与库,然后选择添加其他。对我来说最困难的部分是导航到/usr/local文件夹(我是Xcode开发的新手)。我的方法是打开Finder,然后选择go -> go to Folder…然后转到/usr/local文件夹,然后从窗口底部拖动到收藏夹。然后,当你在Xcode中单击Add Other时,它就会显示出来,你可以轻松地导航到/usr/local/lib目录并找到boost库。