Android:如何编译出能在android系统上使用的C++库

目录

一、下载Crypto++ 7.0原码

二、在Windows上安装Cygwin、Android-NDK、Android-SDK

三、在Cygwin中设置环境变量

四、修改setenv-android.sh文件

五、在Cygwin中生成环境变量

六、在Cygwin中生成库


本文以Crypto++ 7.0为例,简单讲一下如何在Windows上编译出能在android系统上使用的C++库。

一、下载Crypto++ 7.0原码

推荐下载地址:https://github.com/weidai11/cryptopp/archive/CRYPTOPP_7_0_0.zip

解压后可以找到setenv-android.sh、GNUmakefile-cross等文件

二、在Windows上安装Cygwin、Android-NDK、Android-SDK

安装完成后,使用Android-NDK中的make_standalone_toolchain生成一个单独的toolchain
Android-NDK安装目录:C:/Android/android-ndk-r16b
单独的toolchain的生成目录:C:/Android/arm-linux-androideabi

三、在Cygwin中设置环境变量

export ANDROID_NDK_ROOT=/cygdrive/C/Android/android-ndk-r16b
export AOSP_TOOLCHAIN_PATH=/cygdrive/C/Android/arm-linux-androideabi/bin
export AOSP_ABI=armeabi-v7a
export AOSP_API_VERSION=21

 这些变量的值需要根据使用的系统环境做相应修改。

四、修改setenv-android.sh文件

将AOSP_TOOLCHAIN_PATH=""注释掉,不然会报错

五、在Cygwin中生成环境变量

执行source setenv-android.sh,运行结果如下:

ANDROID_NDK_ROOT: /cygdrive/C/Android/android-ndk-r16b
AOSP_TOOLCHAIN_PATH: /cygdrive/C/Android/arm-linux-androideabi/bin
AOSP_ABI: armeabi-v7a
AOSP_API: android-21
AOSP_SYSROOT: /cygdrive/C/Android/android-ndk-r16b/sysroot
AOSP_LD_SYSROOT: /cygdrive/C/Android/android-ndk-r16b/platforms/android-21/arch-arm
AOSP_FLAGS: -D__ANDROID_API__=21 -march=armv7-a -mfpu=neon -mfloat-abi=softfp -Wl,--fix-cortex-a8 -funwind-tables -fexceptions -frtti
AOSP_SYS_ARCH_INC: /cygdrive/C/Android/android-ndk-r16b/sysroot/usr/include/arm-linux-androideabi
AOSP_STL_INC: /cygdrive/C/Android/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/include
AOSP_STL_LIB: /cygdrive/C/Android/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_shared.so
AOSP_BITS_INC: /cygdrive/C/Android/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include
CPU FEATURES: cpu-features.h and cpu-features.c are present

*******************************************************************************
You are using GNU's runtime and STL library. Please ensure the resulting
binary meets licensing requirements. If you can't use GNU's runtime
and STL library, then reconfigure with stlport or llvm. Also see
http://code.google.com/p/android/issues/detail?id=216331
*******************************************************************************

*******************************************************************************
It looks the the environment is set correctly. Your next step is build
the library with 'make -f GNUmakefile-cross'. You can create a versioned
shared object using 'HAS_SOLIB_VERSION=1 make -f GNUmakefile-cross'
*******************************************************************************

六、在Cygwin中生成库

由于命令不认识/cygdrive/C,所以重新设置AOSP_SYSROOT

export AOSP_SYSROOT=C:/Android/arm-linux-androideabi/sysroot

执行make -f GNUmakefile-cross static shared

arm-linux-androideabi-ar r libcryptopp.a cryptlib.o cpu.o integer.o 3way.o adler32.o algebra.o algparam.o arc4.o aria-simd.o aria.o ariatab.o asn.o authenc.o base32.o base64.o basecode.o bfinit.o blake2-simd.o blake2.o blowfish.o blumshub.o camellia.o cast.o casts.o cbcmac.o ccm.o chacha.o channels.o cmac.o crc-simd.o crc.o default.o des.o dessp.o dh.o dh2.o dll.o dsa.o eax.o ec2n.o eccrypto.o ecp.o elgamal.o emsa2.o eprecomp.o esign.o files.o filters.o fips140.o fipstest.o gcm-simd.o gcm.o gf256.o gf2_32.o gf2n.o gfpcrypt.o gost.o gzip.o hex.o hmac.o hrtimer.o ida.o idea.o iterhash.o kalyna.o kalynatab.o keccak.o luc.o mars.o marss.o md2.o md4.o md5.o misc.o modes.o mqueue.o mqv.o nbtheory.o neon-simd.o network.o oaep.o osrng.o padlkrng.o panama.o pkcspad.o poly1305.o polynomi.o ppc-simd.o pssr.o pubkey.o queue.o rabin.o randpool.o rc2.o rc5.o rc6.o rdrand.o rdtables.o rijndael-simd.o rijndael.o ripemd.o rng.o rsa.o rw.o safer.o salsa.o scrypt.o seal.o seed.o serpent.o sha-simd.o sha.o sha3.o shacal2-simd.o shacal2.o shark.o sharkbox.o simon-simd.o simon.o skipjack.o sm3.o sm4.o socketft.o sosemanuk.o speck-simd.o speck.o square.o squaretb.o sse-simd.o strciphr.o tea.o tftables.o threefish.o tiger.o tigertab.o trdlocal.o ttmac.o tweetnacl.o twofish.o vmac.o wait.o wake.o whrlpool.o xtr.o xtrcrypt.o zdeflate.o zinflate.o zlib.o cpu-features.o
arm-linux-androideabi-ar: creating libcryptopp.a
arm-linux-androideabi-ranlib libcryptopp.a

arm-linux-androideabi-g++ -shared  -o libcryptopp.so -DNDEBUG -g2 -O3 -fPIC -pipe -D__ANDROID_API__=21 -march=armv7-a -mfpu=neon -mfloat-abi=softfp -Wl,--fix-cortex-a8 -funwind-tables -fexceptions -frtti -DANDROID --sysroot=C:/Android/arm-linux-androideabi/sysroot -Wa,--noexecstack -I/cygdrive/C/Android/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/include -I/cygdrive/C/Android/android-ndk-r16b/sysroot/usr/include/arm-linux-androideabi -I/cygdrive/C/Android/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -Wl,--exclude-libs,ALL cryptlib.o cpu.o integer.o 3way.o adler32.o algebra.o algparam.o arc4.o aria-simd.o aria.o ariatab.o asn.o authenc.o base32.o base64.o basecode.o bfinit.o blake2-simd.o blake2.o blowfish.o blumshub.o camellia.o cast.o casts.o cbcmac.o ccm.o chacha.o channels.o cmac.o crc-simd.o crc.o default.o des.o dessp.o dh.o dh2.o dll.o dsa.o eax.o ec2n.o eccrypto.o ecp.o elgamal.o emsa2.o eprecomp.o esign.o files.o filters.o fips140.o fipstest.o gcm-simd.o gcm.o gf256.o gf2_32.o gf2n.o gfpcrypt.o gost.o gzip.o hex.o hmac.o hrtimer.o ida.o idea.o iterhash.o kalyna.o kalynatab.o keccak.o luc.o mars.o marss.o md2.o md4.o md5.o misc.o modes.o mqueue.o mqv.o nbtheory.o neon-simd.o network.o oaep.o osrng.o padlkrng.o panama.o pkcspad.o poly1305.o polynomi.o ppc-simd.o pssr.o pubkey.o queue.o rabin.o randpool.o rc2.o rc5.o rc6.o rdrand.o rdtables.o rijndael-simd.o rijndael.o ripemd.o rng.o rsa.o rw.o safer.o salsa.o scrypt.o seal.o seed.o serpent.o sha-simd.o sha.o sha3.o shacal2-simd.o shacal2.o shark.o sharkbox.o simon-simd.o simon.o skipjack.o sm3.o sm4.o socketft.o sosemanuk.o speck-simd.o speck.o square.o squaretb.o sse-simd.o strciphr.o tea.o tftables.o threefish.o tiger.o tigertab.o trdlocal.o ttmac.o tweetnacl.o twofish.o vmac.o wait.o wake.o whrlpool.o xtr.o xtrcrypt.o zdeflate.o zinflate.o zlib.o --sysroot=/cygdrive/C/Android/android-ndk-r16b/platforms/android-21/arch-arm /cygdrive/C/Android/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_shared.so
arm-linux-androideabi-g++: error: /cygdrive/C/Android/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_shared.so: No such file or directory
make: *** [libcryptopp.so] Error 1

 从执行结果的最后几生中,可以看到libcryptopp.a生成成功,而libcryptopp.so生成失败。失败的原因就是由/cygdrive/C生成的,去掉/cygdrive/C以及多余的--sysroot,使用如下命令:

arm-linux-androideabi-g++ -shared  -o libcryptopp.so -DNDEBUG -g2 -O3 -fPIC -pipe -D__ANDROID_API__=21 -march=armv7-a -mfpu=neon -mfloat-abi=softfp -Wl,--fix-cortex-a8 -funwind-tables -fexceptions -frtti -DANDROID --sysroot=C:/Android/arm-linux-androideabi/sysroot -Wa,--noexecstack -I/cygdrive/C/Android/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/include -I/cygdrive/C/Android/android-ndk-r16b/sysroot/usr/include/arm-linux-androideabi -I/cygdrive/C/Android/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -Wl,--exclude-libs,ALL cryptlib.o cpu.o integer.o 3way.o adler32.o algebra.o algparam.o arc4.o aria-simd.o aria.o ariatab.o asn.o authenc.o base32.o base64.o basecode.o bfinit.o blake2-simd.o blake2.o blowfish.o blumshub.o camellia.o cast.o casts.o cbcmac.o ccm.o chacha.o channels.o cmac.o crc-simd.o crc.o default.o des.o dessp.o dh.o dh2.o dll.o dsa.o eax.o ec2n.o eccrypto.o ecp.o elgamal.o emsa2.o eprecomp.o esign.o files.o filters.o fips140.o fipstest.o gcm-simd.o gcm.o gf256.o gf2_32.o gf2n.o gfpcrypt.o gost.o gzip.o hex.o hmac.o hrtimer.o ida.o idea.o iterhash.o kalyna.o kalynatab.o keccak.o luc.o mars.o marss.o md2.o md4.o md5.o misc.o modes.o mqueue.o mqv.o nbtheory.o neon-simd.o network.o oaep.o osrng.o padlkrng.o panama.o pkcspad.o poly1305.o polynomi.o ppc-simd.o pssr.o pubkey.o queue.o rabin.o randpool.o rc2.o rc5.o rc6.o rdrand.o rdtables.o rijndael-simd.o rijndael.o ripemd.o rng.o rsa.o rw.o safer.o salsa.o scrypt.o seal.o seed.o serpent.o sha-simd.o sha.o sha3.o shacal2-simd.o shacal2.o shark.o sharkbox.o simon-simd.o simon.o skipjack.o sm3.o sm4.o socketft.o sosemanuk.o speck-simd.o speck.o square.o squaretb.o sse-simd.o strciphr.o tea.o tftables.o threefish.o tiger.o tigertab.o trdlocal.o ttmac.o tweetnacl.o twofish.o vmac.o wait.o wake.o whrlpool.o xtr.o xtrcrypt.o zdeflate.o zinflate.o zlib.o C:/Android/android-ndk-r16b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_shared.so

就可以成功生成libcryptopp.so。

libcryptopp.a和libcryptopp.so就是可以在Android上使用的库。

 

参考文档

Android (Command Line)

你可能感兴趣的:(android,C++)