android - 如何正确指定 CMakeLists 文件
问题描述
我正在尝试将 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.
我该如何解决?
解决方案
您得到的错误是因为缺少zlib 依赖项。添加以下内容:
find_library(zlib-lib z)
target_link_libraries(openssl ${zlib-lib})
或者更好的是,添加zlib-lib
到ssl
目标的INTERFACE_LINK_LIBRARIES
属性:
find_library(zlib-lib z)
set_property(TARGET ssl APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${zlib-lib})
推荐阅读
- flutter - 如何在同一个容器中拥有标签和输入文本
- spring-integration - 如何配置此 JdbcMessageHandler 以从消息中提取参数而不是静态 bean?
- javascript - 即使在 if 语句中也从函数返回
- scipy - 使用 Scipy 将可靠性指数 (beta) 转换为其故障概率 (pf)
- c++ - 在 C++ 中读取 .dat 二进制文件(深度图)
- firebase - NullInjectorError:没有 InjectionToken angularfire2.app.options 的提供者!2021
- android - 可绘制矩形左边框上的垂直可绘制线?
- javascript - 如何使用 JavaScript 在嵌入式 CSS 中更改 CSS 样式规则
- c - 如果输入非整数(例如 1.2),程序会跳过连续的 scanf("%d", &int) 输入
- python - 如何传递变量并在 python 脚本中提取它们?