Linking troubles with boost::program_options on OSX using LLVM

╄→尐↘猪︶ㄣ 提交于 2019-11-28 10:11:37

You will need to recompile boost with clang and std11 flags, the libc++ library is not binary compatible with the installed libstdc++ in OSX (very early version of gcc prior to changing to gpl3). If your version of clang is 3.1 or over then you can use (otherwise change c++11 to c++0x for earlier versions).

./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 

You can of course alter any of these as you wish except

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

This should work for you.

I would like to share my (moderately painful) experience of building Boost 1.54 on Mac OS X 10.8.5 with clang 5.0.0 as supplied by Xcode 5.0 . If you want the C++11 features it is very important to compile and link with clang++, not with clang.

Illustration: take the following simple program:

#include <iostream>
#include <string>

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

Can be built with the following command:

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

however, if you try this instead:

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

then you get linker errors. Note that the clang manpage says that the language is selected by the -std= option, but this is clearly not enough.

The lesson is that we have to tell bjam to explicitly use clang++ when compiling Boost with C++11 support.

Following this very useful post, I put the following into my tools/build/v2/user-config.jam:

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

Then I ran ./b2 clean, then I built Boost with the following command:

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

This builds the 64-bit static and dynamic libraries with multithreading support. If you need a different set then change the command above accordingly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!