【发布时间】:2019-11-28 12:35:53
【问题描述】:
我正在尝试将 openssl 添加到 android studio 项目中。
我在项目中有如下打开的 ssl 二进制文件:
我将以下内容放入我的 CMakeLists.txt 文件中
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(import-lib-location ${CMAKE_CURRENT_SOURCE_DIR}/../../../libs)
include_directories(include/)
add_library(crypto SHARED IMPORTED )
set_target_properties(crypto
PROPERTIES IMPORTED_LOCATION ${import-lib-location}/${ANDROID_ABI}/libcrypto.a)
add_library(ssl STATIC IMPORTED )
set_target_properties(ssl
PROPERTIES IMPORTED_LOCATION
${import-lib-location}/${ANDROID_ABI}/libssl.a)
add_library( # Sets the name of the library.
openssl
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
openssl.c)
target_link_libraries( # Specifies the target library.
openssl
ssl
crypto
)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
openssl
# Links the target library to the log library
# included in the NDK.
${log-lib})
但是,我无法构建代码,因为它不断抛出错误,效果如下:
[2/2] Linking C shared library /Users/lena/Dev/testprojects/open/openssl/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libopenssl.so
FAILED: /Users/lena/Dev/testprojects/open/openssl/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libopenssl.so
: && /Users/lena/Library/Android/sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang --target=armv7-none-linux-androideabi27 --gcc-toolchain=/Users/lena/Library/Android/sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64 --sysroot=/Users/lena/Library/Android/sdk/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -march=armv7-a -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -shared -Wl,-soname,libopenssl.so -o /Users/lena/Dev/testprojects/open/openssl/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libopenssl.so CMakeFiles/openssl.dir/openssl.c.o /Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libssl.a /Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a -llog -latomic -lm && :
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_init: error: undefined reference to 'inflateInit_'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_init: error: undefined reference to 'deflateInit_'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_finish: error: undefined reference to 'inflateEnd'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_finish: error: undefined reference to 'deflateEnd'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_compress_block: error: undefined reference to 'deflate'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function zlib_stateful_expand_block: error: undefined reference to 'inflate'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_write: error: undefined reference to 'deflateInit_'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_write: error: undefined reference to 'deflate'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_write: error: undefined reference to 'zError'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_read: error: undefined reference to 'inflateInit_'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_read: error: undefined reference to 'inflate'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_read: error: undefined reference to 'zError'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_ctrl: error: undefined reference to 'deflate'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_ctrl: error: undefined reference to 'zError'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_free: error: undefined reference to 'inflateEnd'
/Users/lena/Dev/testprojects/open/openssl/app/src/main/cpp/../../../libs/armeabi-v7a/libcrypto.a(c_zlib.o):c_zlib.c:function bio_zlib_free: error: undefined reference to 'deflateEnd'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
我该如何解决?
【问题讨论】:
-
你从哪里得到这些静态库的?它们也是使用 Android NDK 构建的吗?
-
您为
crypto库目标分配了IMPORTED_LOCATION属性,因此您可能希望将此目标设为IMPORTED:add_library(crypto STATIC IMPORTED)。顺便说一句,你为 ssl 库做了同样的事情,但也做错了。正确的方法是:add_library(ssl STATIC IMPORTED). -
@Michael 他们是根据github.com/leenjewel/openssl_for_ios_and_android中的说明构建的
-
@Tsyvarev 我这样做了,并编辑了我的 Q,你能看看吗?