Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 702
dB' Avatar asked Oct 21 '22 20:10

dB'


1 Answers

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
like image 147
bruth Avatar answered Oct 23 '22 16:10

bruth