使用 LLVM 在 OSX 上将问题与 boost::p rogram_options 链接

Linking troubles with boost::program_options on OSX using LLVM

本文关键字:rogram 链接 options boost LLVM OSX 问题 使用      更新时间:2023-10-16

由于 Boost 1.49 的问题,我在C++程序中无法完成链接阶段。我已经切换到C++(-std=c++11 -libc=libc++),它适用于另一段代码(也使用boost)。Boost是使用自制软件安装的:

brew install boost --universal --with-mpi --with-icu

麻烦从boost::program_options开始.我收到这样的链接错误:

  "boost::program_options::validate(boost::any&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, int)", referenced from:
... etc. ...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这有点奇怪,因为在使用的库上做一个 nm 会发现,符号似乎在那里:

nm -U /usr/local/lib/libboost_program_options-mt.dylib  | grep validate
0000000000019880 - 01 0000   FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPSsi
0000000000019880 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPSsi
00000000000199e0 - 01 0000   FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPbi
00000000000199e0 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPbi
0000000000019930 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPSsi
0000000000019930 - 01 0000   FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPSsi
0000000000019c70 - 01 0000   FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPbi
0000000000019c70 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPbi

我已经尝试过通过在安装之前相应地设置 CXX 和 CXX_FLAGS来哄骗自制软件使用 clang 而不是 gcc 编译 boost。不确定我成功了。

指针非常感谢。

您需要使用 clang 和 std11 标志重新编译 boost,libc++ 库与 OSX 中安装的 libstdc++ 不兼容二进制(在更改为 gpl3 之前是 gcc 的早期版本)。如果您的 clang 版本是 3.1 或更高版本,那么您可以使用(否则将 c++11 更改为 c++0x 对于早期版本)。

./bootstrap.sh
mkdir build
sudo ./bjam toolset=clang cxxflags="-std=c++0x -stdlib=libc++" variant=release link=static threading=multi runtime-link=shared --build-dir=Build --layout=system --without-mpi --without-python install --prefix=/usr/local 

您当然可以根据需要更改其中任何一个,除了

toolset=clang cxxflags="-std=c++0x -stdlib=libc++"

这应该适合您。

我想

分享我在 Mac OS X 10.8.5 上使用 xcode 5.0 提供的 clang 5.0.0 构建 Boost 1.54 的(中等痛苦)经验。如果你想要 C++11 功能,编译和链接非常重要 clang++ ,而不是 clang .

图示:采用以下简单程序:

#include <iostream>
#include <string>
int main(int argc, char *argv[]) {
    std::string str = "OK";
    std::cout << str << std::endl;
    return 0;
}

可以使用以下命令构建:

clang++ -std=c++11 -stdlib=libc++ clangstr.cc -o clangstr

但是,如果您尝试以下操作:

clang -std=c++11 -stdlib=libc++ clangstr.cc -o clangstr

然后你得到链接器错误。请注意,clang手册页说语言是通过-std=选项选择的,但这显然是不够的。

教训是,我们必须告诉bjam在编译具有 C++11 支持的 Boost 时显式使用clang++

在这篇非常有用的帖子之后,我将以下内容放入我的tools/build/v2/user-config.jam

using clang : 11
    : "/usr/bin/clang++"
    : <cxxflags>"-std=c++11 -stdlib=libc++ -ftemplate-depth=512" <linkflags>"-stdlib=libc++"
    ;

然后我运行了./b2 clean,然后我用以下命令构建了 Boost:

mkdir -p build/clangstage/
./b2 -j8 --build-dir=build --stagedir=build/clangstage toolset=clang-11 define=BOOST_SYSTEM_NO_DEPRECATED variant=release threading=multi address-model=64 stage

这将构建具有多线程支持的 64 位静态和动态库。如果您需要不同的集合,请相应地更改上面的命令。