2014-08-27 41 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