2014-08-27 69 views

回答

16

./配置這樣我就可以將它構建到兩個體系結構到相同的.a文件中嗎?

您必須小心OpenSSL和多元庫,因爲庫不是多元安全的。這是因爲每個配置都有自己的<openssl/opensslconf.h>文件,每個平臺的BIGNUM都不相同。

由於OpenSSL的構建系統形成命令,供應-arch x86_64 -arch i386將導致構建失敗。另見Getting libcrypto ar error while compiling OpenSSL for Mac

以下詳述的相同步驟也適用於iOS。唯一改變的是-arch。對於iOS,您可能會使用armv7,armv7s,arm64i386(用於32位仿真器調試)和x86_64(用於64位仿真器調試)。

還有另一個不那麼明顯的技巧需要。 OpenSSL硬編碼的一些默認路徑基於--prefix--openssldir,因此您必須爲安裝目錄構建32位,安裝並移動它;然後爲安裝目錄構建64位,安裝,然後移動它;然後在安裝目錄中創建胖庫。另請參閱How to determine the default location for openssl.cnf?

最後,取代OS X的提供的OpenSSL。 OpenSSL 1.0.x和1.1.x是而不是二進制兼容Apple的0.9.8版本的OpenSSL。由於不兼容,以下程序使用$HOME/ssl。您可以使用/usr/local/ssl或任何適合您口味的其他位置。


開始之前,OpenSSL wiki上有一個頁面,其頁面號爲Compilation and Installationconfig有很多選項可供選擇。選擇適合你口味的。我總是用no-ssl2,並且通常使用no-ssl3,no-comp。在移動設備上,我使用no-srpno-psk,no-hw,no-dsono-engines


以下是建立圖書館的說明。您將配置,構建,安裝,然後移動您在多元構建中支持的每個架構。

32位

make clean && make dclean 

KERNEL_BITS=32 ./config no-ssl2 no-ssl3 --prefix=$HOME/ssl 
make depend 
make 
make install_sw 

mv $HOME/ssl/include/openssl/opensslconf.h $HOME/ssl/include/openssl/opensslconf-x86.h 
mv $HOME/ssl/include/openssl/bn.h $HOME/ssl/include/openssl/bn-x86.h 
mv $HOME/ssl/ $HOME/ssl-x86 

64位

make clean && make dclean 

KERNEL_BITS=64 ./config no-ssl2 no-ssl3 --prefix=$HOME/ssl 
make depend 
make 
make install_sw 

mv $HOME/ssl/include/openssl/opensslconf.h $HOME/ssl/include/openssl/opensslconf-x64.h 
mv $HOME/ssl/include/openssl/bn.h $HOME/ssl/include/openssl/bn-x64.h 
mv $HOME/ssl/ $HOME/ssl-x64 

你需要複製一個套頭(這並不重要) ,複製opensslconf-x86.h,opensslconf-x64.hbn-x86.hbn-x64.h,創建一個新的<openssl/opensslconf.h>,創建一個新的<openssl/bn.h>,最後創建多元化庫。

rm -rf $HOME/ssl 

mkdir -p $HOME/ssl/bin 
mkdir -p $HOME/ssl/include/openssl 
mkdir -p $HOME/ssl/lib 

cp $HOME/ssl-x86/openssl.cnf $HOME/ssl/openssl.cnf 
cp $HOME/ssl-x86/include/openssl/* $HOME/ssl/include/openssl 
cp $HOME/ssl-x86/include/openssl/opensslconf-x86.h $HOME/ssl/include/openssl/opensslconf-x86.h 
cp $HOME/ssl-x64/include/openssl/opensslconf-x64.h $HOME/ssl/include/openssl/opensslconf-x64.h 
cp $HOME/ssl-x86/include/openssl/bn-x86.h $HOME/ssl/include/openssl/bn-x86.h 
cp $HOME/ssl-x64/include/openssl/bn-x64.h $HOME/ssl/include/openssl/bn-x64.h 

新<opensslconf.h>

如果您還沒有這樣做,創建$HOME/ssl/include/openssl/opensslconf.h。要確保你使用一個新的頭文件保護(OPENSSL_MULTIARCH_CONF_HEADER):

cat $HOME/ssl/include/openssl/opensslconf.h 
#ifndef OPENSSL_MULTIARCH_CONF_HEADER 
#define OPENSSL_MULTIARCH_CONF_HEADER 

#if __i386 || __i386__ 
# include "opensslconf-x86.h" 
#elif __x86_64 || __x86_64__ || __amd64 || __amd64__ 
# include "opensslconf-x64.h" 
#else 
# error Unknown architecture 
#endif 

#endif /* OPENSSL_MULTIARCH_CONF_HEADER */ 

新<bn.h>

創建$HOME/ssl/include/openssl/bn.h。要確保你使用一個新的頭文件保護(OPENSSL_MULTIARCH_BN_HEADER):

cat $HOME/ssl/include/openssl/bn.h 
#ifndef OPENSSL_MULTIARCH_BN_HEADER 
#define OPENSSL_MULTIARCH_BN_HEADER 

#if __i386 || __i386__ 
# include "bn-x86.h" 
#elif __x86_64 || __x86_64__ || __amd64 || __amd64__ 
# include "bn-x64.h" 
#else 
# error Unknown architecture 
#endif 

#endif /* OPENSSL_MULTIARCH_BN_HEADER */ 

在這一點上,你必須位於$HOME/ssl-x86圖書館的x86版本和位於庫的64位編譯$HOME/ssl-x64。您將它們與lipo結合起來,編號爲$HOME/ssl

lipo -create $HOME/ssl-x86/lib/libcrypto.a \ 
      $HOME/ssl-x64/lib/libcrypto.a \ 
      -output $HOME/ssl/lib/libcrypto.a 

lipo -create $HOME/ssl-x86/lib/libssl.a \ 
      $HOME/ssl-x64/lib/libssl.a \ 
      -output $HOME/ssl/lib/libssl.a 

lipo -create $HOME/ssl-x86/bin/openssl \ 
      $HOME/ssl-x64/bin/openssl \ 
      -output $HOME/ssl/bin/openssl 

共享庫

如果shared配置,那麼你需要執行:

lipo -create $HOME/ssl-x86/lib/libcrypto.1.0.0.dylib \ 
      $HOME/ssl-x64/lib/libcrypto.1.0.0.dylib \ 
      -output $HOME/ssl/lib/libcrypto.1.0.0.dylib 

lipo -create $HOME/ssl-x86/lib/libssl.1.0.0.dylib \ 
      $HOME/ssl-x64/lib/libssl.1.0.0.dylib \ 
      -output $HOME/ssl/lib/libssl.1.0.0.dylib 

然後,您需要重新創建軟鏈接:

ln -s $HOME/ssl/lib/libcrypto.dylib $HOME/ssl/lib/libcrypto.1.0.0.dylib 
ln -s $HOME/ssl/lib/libssl.dylib $HOME/ssl/lib/libssl.1.0.0.dylib 

最後,測試一下東西。驗證庫multiarch:

ls $HOME/ssl/lib/ 
libcrypto.a libssl.a 

lipo -info $HOME/ssl/lib/libcrypto.a 
Architectures in the fat file: $HOME/ssl/lib/libcrypto.a are: i386 x86_64 
lipo -info $HOME/ssl/lib/libssl.a 
Architectures in the fat file: $HOME/ssl/lib/libssl.a are: i386 x86_64 

然後測試程序:

#include <openssl/opensslconf.h> 
#include <openssl/ssl.h> 

int main(int argc, char* argv[]) 
{ 
    SSL_library_init(); 
    return 0; 
} 

和:

$ clang -arch i386 -arch x86_64 -I $HOME/ssl/include test.c -o test.exe -L $HOME/ssl/lib -lssl -lcrypto 
$ DYLD_LIBRARY_PATH=$HOME/ssl/lib; ./test.exe 
$ 

DYLD_LIBRARY_PATH使用的情況下,你建在OS X的動態庫


如果需要,您可以刪除非multiarch安裝:

rm -rf $HOME/ssl-x86 
rm -rf $HOME/ssl-x64 
+0

非常感謝。你搖滾! – 2014-09-25 03:53:28