如何使用指定的编译器(比如GCC)安装Boost

How to install Boost with specified compiler (say GCC)

本文关键字:GCC 比如 安装 Boost 编译器 何使用      更新时间:2023-10-16

我想用指定的编译器安装boost,例如我在<gcc_49_root>中安装的gcc-4.9.1。目前的操作系统是Mac OS X 10.9.4,但我希望这个安装过程可以在其他操作系统上工作。boost的文档对这个场景非常不透明。我做了如下尝试:

$ ./bootstrap.sh
-n Building Boost.Build engine with toolset darwin...
tools/build/src/engine/bin.macosxx86_64/b2
-n Detecting Python version...
2.7
-n Detecting Python root...
/System/Library/Frameworks/Python.framework/Versions/2.7
-n Unicode/ICU support for Boost.Regex?...
not found.
Generating Boost.Build configuration in project-config.jam...

插入using gcc : 4.9.1 : <gcc_49_root>/bin/g++-4.9 : ;project-config.jam .

$ ./b2 --prefix=<...> toolset=gcc-4.9.1 install

但遇到错误:

Jamfile</Users/dongli/Shares/works/packman/test/packages/Boost/boost_1_56_0/libs/context/build>.gas64 bin.v2/libs/context/build/gcc-4.9.1/release/address-model-64/architecture-x86/threading-multi/asm/make_x86_64_sysv_macho_gas.o
FATAL:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../libexec/as/x86_64/as: I don't understand '-' flag!
clang: error: no input files
    cpp -x assembler-with-cpp "libs/context/src/asm/make_x86_64_sysv_macho_gas.S" | as --64 -o "bin.v2/libs/context/build/gcc-4.9.1/release/address-model-64/architecture-x86/threading-multi/asm/make_x86_64_sysv_macho_gas.o"
...failed Jamfile</Users/dongli/Shares/works/packman/test/packages/Boost/boost_1_56_0/libs/context/build>.gas64 bin.v2/libs/context/build/gcc-4.9.1/release/address-model-64/architecture-x86/threading-multi/asm/make_x86_64_sysv_macho_gas.o...
gcc.link.dll bin.v2/libs/atomic/build/gcc-4.9.1/release/threading-multi/libboost_atomic.dylib
ld: unknown option: -h
collect2: error: ld returned 1 exit status
    "/usr/local/opt/gcc/bin/g++-4.9"    -o "bin.v2/libs/atomic/build/gcc-4.9.1/release/threading-multi/libboost_atomic.dylib" -Wl,-h -Wl,libboost_atomic.dylib -shared -Wl,--start-group "bin.v2/libs/atomic/build/gcc-4.9.1/release/threading-multi/lockpool.o"  -Wl,-Bstatic  -Wl,-Bdynamic  -Wl,--end-group
...failed gcc.link.dll bin.v2/libs/atomic/build/gcc-4.9.1/release/threading-multi/libboost_atomic.dylib...

我该如何处理这些错误?提前感谢!

Apple的链接器ld(ld64)与其他UNIX/GNU链接器不同,不支持-h(soname),——start-group,——end-group等选项。当您指定gcc工具集时,您得到的这些错误("未知选项")是试图将不支持的标志传递给Apple的ld的结果。

我的破解方法是首先在项目配置文件中包含"darwin":

using gcc : 4.9.1 : <gcc_49_root>/bin/g++-4.9 : <linker-type>darwin ;

下一步删除{BOOST_DIR}/tools/build/src/tools/gcc中不支持的标志。从"actions link.dll bind LIBRARIES"块中的长命令:

remove/comment out this portion:
... $(HAVE_SONAME)-Wl,$(SONAME_OPTION)$(SPACE)-Wl,$(<[-1]:D=) ...

之后,Boost库构建没有错误,并在其他gcc4.9编译的代码中工作良好。

$ ./bootstrap.sh --with-toolset=gcc 
$ ./b2 --toolset=gcc-4.9.1

更新(2015年5月):我最近在Yosemite(10.10.1)上做了一个新的gcc 5.1.0和Boost 1.58.0。

我用的是Mac Yosemite,这对我很有效。

打开"工具/构建//user-config示例。并更改

# Configure gcc (default version).
# using gcc ;
# Configure specific gcc version, giving alternative name to use.

using Darwin: 5: g++-5;

然后打开"tools/build/src/tools/darwin. conf"。然后删除下面的行(这一步可能不需要。两种方法都试试);

"$(CONFIG_COMMAND)" -dynamiclib -Wl,-single_module -install_name "$(<:B)$(<:S)" -L"$(LINKPATH)" -o "$(<)" "$(>)" "$(LIBRARIES)" -l$(FINDLIBS-SA) -l$(FINDLIBS-ST) $(FRAMEWORK_PATH) -framework$(_)$(FRAMEWORK:D=:S=) $(OPTIONS) $(USER_OPTIONS)

最后一步,编译并安装

$ ./bootstrap.sh --with-libraries=all --with-toolset=darwin --prefix=/usr/local/boost_for_gcc
$ ./b2
$ ./b2 install
现在你可以像下面这样编译你的代码
$ g++ -o main main.cpp -L/usr/local/boost_for_gcc/lib -I/usr/local/boost_for_gcc/include -lboost_regex

参考:http://qiita.com/misho/items/0c0b3ca25bb8f62aa681