【问题标题】:How to correctly specify CMakeLists file如何正确指定 CMakeLists 文件
【发布时间】: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,你能看看吗?

标签: android cmake


【解决方案1】:

您得到的错误是因为缺少zlib dependency。 添加以下内容:

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})

【讨论】:

  • CMake 错误:此项目中使用了以下变量,但它们设置为 NOTFOUND。请设置它们或确保它们在 CMake 文件中正确设置和测试:zlib-lib 由目录 /openssl/app/src/main/cpp 中的目标“openssl”链接
  • 我该如何尝试您的第二种方法? add zlib-lib to the ssl target's INTERFACE_LINK_LIBRARIES property. 是什么意思
  • 修正错字并添加第二种方法
  • ninja:错误:'openssl/app/src/main/cpp/libs/armeabi-v7a/libssl.a',需要'openssl/app/build/intermediates/cmake/debug/obj /armeabi-v7a/libopenssl.so',缺少并且没有已知的规则来制作它
  • 请检查生成的忍者文件。您问题中的输出使用了libssl.a 的正确路径,因此在此期间一定发生了一些变化。
猜你喜欢
  • 2023-03-13
  • 1970-01-01
  • 2015-06-23
  • 1970-01-01
  • 2021-12-21
  • 2020-04-15
  • 1970-01-01
相关资源
最近更新 更多