尝试编译openssl库libcrypto.a的包装器时出错

时间:2014-03-26 16:55:32

标签: android android-ndk openssl wrapper linker-errors

我正在尝试为libcrypto.a周围的android包装器构建一个动态库,如

所述

http://wiki.openssl.org/index.php/FIPS_Library_and_Android#Using_FIPS_OpenSSL_in_a_real_Application

我按照页面上的建议生成了libcrypto.a,但是当我尝试编译我的wrapper.c时,我收到一个链接器错误,'atexit'的多个定义

这是我用来编译的命令行:

arm-linux-androideabi-gcc wrapper.c -fPIC -shared -I/usr/local/ssl/android-14/include -Wl,-Bstatic -lcrypto -L/usr/local/ssl/android-14/lib -o libwrapper.so --sysroot=/Users/scoleman/android-ndk-r9d/platforms/android-14/arch-arm -Wl, -Bdynamic

结果如下:

/Users/scoleman/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: /Users/scoleman/android-ndk-r9d/platforms/android-14/arch-arm/usr/lib/libc.a(atexit.o): multiple definition of 'atexit'
/Users/scoleman/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: /Users/scoleman/android-ndk-r9d/platforms/android-14/arch-arm/usr/lib/crtbegin_so.o: previous definition here
/Users/scoleman/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: error: cannot find -ldl
collect2: ld returned 1 exit status

这是我的wrapper.c:

#include <string.h>
#include <jni.h>

int MY_FIPS_mode()   {
   int mode = mode = FIPS_mode();
   return mode;
}

1 个答案:

答案 0 :(得分:3)

Here's the command line I'm using to compile:

arm-linux-androideabi-gcc wrapper.c -fPIC -shared -I/usr/local/ssl/android-14/include -Wl,-Bstatic -lcrypto -L/usr/local/ssl/android-14/lib -o libwrapper.so --sysroot=/Users/scoleman/android-ndk-r9d/platforms/android-14/arch-arm -Wl, -Bdynamic

我能够使用您的命令行复制该问题。我接近这个的方法是(为了便于阅读而添加换行符):

arm-linux-androideabi-gcc wrapper.c -fPIC -shared -o libwrapper.so
    --sysroot=.../android-ndk-r9d/platforms/android-14/arch-arm
    -I/usr/local/ssl/android-14/include
    /usr/local/ssl/android-14/lib/libssl.a
    /usr/local/ssl/android-14/lib/libcrypto.a
    -ldl

--sysroot将引入适用于Android 4.0(API 14)的平台标头和库。这应该是定义atexit的地方。我认为-Bstatic-Bdynamic可能会使事情变得复杂,因为atexitlibc.a都提供了crtbegin_so.o

我避免使用-Bstatic-Bdynamic。当我想要静态链接时,我特意调出静态库的完整路径名,比如/usr/local/ssl/android-14/lib/libcrypto.a。请记住,存档是目标文件的集合(*.o),因此您可以在任何可以使用目标文件的地方使用它。


使用页面和源代码中的setenv-android.sh脚本,我 无法复制:

$ . ./setenv-android.sh 
ANDROID_NDK_ROOT: /opt/android-ndk-r9
ANDROID_EABI: arm-linux-androideabi-4.6
ANDROID_API: android-14
ANDROID_SYSROOT: /opt/android-ndk-r9/platforms/android-14/arch-arm
ANDROID_TOOLCHAIN: /opt/android-ndk-r9/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin
CROSS_COMPILE: arm-linux-androideabi-
ANDROID_DEV: /opt/android-ndk-r9/platforms/android-14/arch-arm/usr

$ cat wrapper.c 
#include <string.h>
#include <jni.h>
#include <openssl/evp.h>

int MY_FIPS_mode()   {
    int mode = FIPS_mode();
    return mode;
}

$ arm-linux-androideabi-gcc wrapper.c -fPIC -shared -o libwrapper.so \
>     --sysroot=$ANDROID_SYSROOT \
>     -I/usr/local/ssl/android-14/include \
>     /usr/local/ssl/android-14/lib/libssl.a \
>     /usr/local/ssl/android-14/lib/libcrypto.a \
>     -ldl

$ ls
libwrapper.so       setenv-android.sh   wrapper.c

$ arm-linux-androideabi-readelf -h libwrapper.so 
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          52 (bytes into file)
  Start of section headers:          244660 (bytes into file)
  Flags:                             0x5000000, Version5 EABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         7
  Size of section headers:           40 (bytes)
  Number of section headers:         34
  Section header string table index: 33

$ arm-linux-androideabi-nm -D libwrapper.so | grep MY_FIPS_mode
00009fa4 T MY_FIPS_mode