【问题标题】:Linking troubles with boost::program_options on OSX using LLVM使用 LLVM 在 OSX 上将问题与 boost::program_options 联系起来
【发布时间】:2012-06-18 11:26:42
【问题描述】:

由于 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。不确定我是否成功了。

非常感谢指针。

【问题讨论】:

标签: c++ boost c++11 osx-lion clang


【解决方案1】:

您需要使用 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++"

这应该适合你。

【讨论】:

  • 您好,感谢您的指点。想了那么多。但是,我现在似乎无法让 Boost 为我编译动态库。我试过这个:'sudo ./bjam toolset=clang cxxflags="-std=c++11 -stdlib=libc++" variant=release link=shared threading=multi runtime-link=shared --build-dir=Build -- layout=system --without-mpi --without-python install --prefix=/usr/local' 但只有静态的。
  • 我终于通过 Boost.Build-Forum 得到了答案:boost.2283326.n4.nabble.com/… 简而言之:我在调用 bjam 时缺少 'linkflags':./b2 toolset=clang cxxflags="-std=c ++11 -stdlib=libc++" linkflags="-stdlib=libc++"
【解决方案2】:

我想分享我在 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= 选项选择的,但这显然还不够。

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

this very useful post 之后,我将以下内容放入我的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 位静态和动态库。如果您需要不同的设置,请相应地更改上面的命令。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 2016-01-30
    • 2012-07-13
    • 2011-01-25
    • 1970-01-01
    • 2012-07-29
    • 2018-06-21
    相关资源
    最近更新 更多