How to make Boost dylibs universal (i386 & x86_64) on os x?

只谈情不闲聊 提交于 2020-12-09 04:16:48

问题


I'm trying to compile a Boost library into a universal binary file (i.e. a "fat" file that contains builds for both the i386 and x86_64 architectures).

Souring the internet and SO I assembled the following instructions.

  1. Download boost (e.g. from http://www.boost.org/users/download/)

  2. In the downloaded folder, type ./bootstrap.sh (or, in my case ./bootstrap.sh --with-libraries=thread, since I only need the thread library)

  3. type ./b2 install cxxflags="-arch i386 -arch x86"

These steps installed the Boost thread library to /usr/local/lib/ (its standard location). The resulting static library is a universal binary. So far, so good.

$ lipo -i /usr/local/lib/libboost_thread.a
Architectures in the fat file: /usr/local/lib/libboost_thread.a are: i386 x86_64 

The dynamic library, however, only seems to have been compiled for x86_64.

$ lipo -i /usr/local/lib/libboost_thread.dylib
Non-fat file: /usr/local/lib/libboost_thread.dylib is architecture: x86_64

I'd like the .dylib to be universal as well. Does anyone know how I can compile it for i386 as well as x86_64?


回答1:


I was struggling with this as well. The trick seems to be two-fold.

  1. You need to use a different toolset to build the i386 .dylib. clang will build an x86_64 .dylib no matter what I tried, but darwin with the right flags will build an i386 .dylib
  2. Build twice, once for i386, once for x86_64; then use lipo to combine the result into a "fat" .dylib

Here's what I quick threw together to reproducibly get 'fat' .dylibs. Find the ones you need in universal/. The static 'fat' .a libs are left in stage/lib/.

rm -rf i386 x86_64 universal
./bootstrap.sh --with-toolset=clang --with-libraries=filesystem
./b2 toolset=darwin -j8 address-model=32 architecture=x86 -a
mkdir -p i386 && cp stage/lib/*.dylib i386
./b2 toolset=clang -j8 cxxflags="-arch i386 -arch x86_64" -a
mkdir x86_64 && cp stage/lib/*.dylib x86_64
mkdir universal
for dylib in i386/*; do 
  lipo -create -arch i386 $dylib -arch x86_64 x86_64/$(basename $dylib) -output universal/$(basename $dylib); 
done

One-liner:

rm -rf i386 x86_64 universal &&  ./bootstrap.sh --with-toolset=clang --with-libraries=filesystem && ./b2 toolset=darwin -j8 address-model=32 architecture=x86 -a && mkdir -p i386 && cp stage/lib/*.dylib i386 && ./b2 toolset=clang -j8 cxxflags="-arch i386 -arch x86_64" -a && mkdir x86_64 && cp stage/lib/*.dylib x86_64 && mkdir universal && for dylib in i386/*; do lipo -create -arch i386 $dylib -arch x86_64 x86_64/$(basename $dylib) -output universal/$(basename $dylib); done


来源:https://stackoverflow.com/questions/19897761/how-to-make-boost-dylibs-universal-i386-x86-64-on-os-x

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