首页 > 解决方案 > cmake - 创建一个可在 Android 中使用的共享 arm 库

问题描述

我有一个包含 armeabi-v7a 文件夹并使用 libssl.a 和 libcrypto.a 库的项目。现在我可以使用源代码 c++ 文件编译项目并将其用于项目。

现在我希望 cmake 创建一个可以在任何其他项目中使用的共享库。我对cmake了解不多

我试过什么?

cmake -DCMAKE_TOOLCHAIN_FILE=$(NDK_PATH)/build/cmake/android.toolchain.cmake -DANDROID_NDK=$(NDK_PATH) -DANDROID_ABI=armeabi-v7a -DANDROID_PLATFORM=android-21 -DANDROID_STL=c++_shared -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/some_dir/

运行这个之后

-- Check for working C compiler: $(NDK_PATH)/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang
-- Check for working C compiler: $(NDK_PATH)/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: $(NDK_PATH)/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++
-- Check for working CXX compiler: $(NDK_PATH)/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_C_COMPILER= /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
CMAKE_CXX_COMPILER= /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++

-- The C compiler identification is AppleClang 9.1.0.9020039
-- The CXX compiler identification is AppleClang 9.1.0.9020039
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done

-- Configuring done                                                                                                                       
-- Generating done
-- Build files have been written to: $(PATH)

现在当我尝试跑步时

make

它给了我

Scanning dependencies of target dcsdk
Building CXX object CMakeFiles/dcsdk.dir/src/main/cpp/DCSDK.cpp.o
Building CXX object CMakeFiles/dcsdk.dir/src/main/cpp/Constants.cpp.o
Linking CXX shared library libdcsdk.dylib

ld:警告:ld:警告:忽略文件 openssl-armeabi-v7a/lib/libssl.a,文件是为存档而构建的,它不是被链接的架构(x86_64):openssl-armeabi-v7a/lib/libssl.aignoring 文件openssl-armeabi-v7a/lib/libcrypto.a,文件是为存档而构建的,它不是被链接的架构(x86_64):openssl-armeabi-v7a/lib/libcrypto.a

架构 x86_64 的未定义符号:“_OPENSSL_init_ssl”,引用自:DCSDK.cpp.o 中的 initSSL()

现在我得到的问题是它为 x86_64 架构编译,但我已经在 cmake 中指定它使用 ANDROID_ABI 到 armeabi-v7a。

如果你需要我的 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)

# 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.

add_library( # Sets the name of the library.
         dcsdk

         # Sets the library as a shared library.
         SHARED

         # Provides a relative path to your source file(s).
         src/main/cpp/native-lib.cpp src/main/cpp/DCSDK.cpp src/main/cpp/Constants.cpp )

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

include_directories(openssl-armeabi-v7a/include/) 

include_directories(/System/Library/Frameworks/JavaVM.framework/Headers/)

# 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.
                   dcsdk
                   # Links the target library to the log library
                   # included in the NDK.
                   ${CMAKE_CURRENT_SOURCE_DIR}/openssl-armeabi-v7a/lib/libssl.a
                   ${CMAKE_CURRENT_SOURCE_DIR}/openssl-armeabi-v7a/lib/libcrypto.a )

标签: androidc++cmakeandroid-ndktoolchain

解决方案


推荐阅读