cmake - Creating a shared arm library which can be used in Android

拈花ヽ惹草 提交于 2020-02-06 08:13:06

问题


I have a project which has armeabi-v7a folder and uses libssl.a and libcrypto.a libraries. Now i am able to compile the project with the source code c++ files and use that into a project.

Now i want cmake to create a shared library which i can use in any other project. I don't have much knowledge of cmake

What i have tried?

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/

After running this

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

Now when i try running

make

It gives me

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: warning: ld: warning: ignoring file openssl-armeabi-v7a/lib/libssl.a, file was built for archive which is not the architecture being linked (x86_64): openssl-armeabi-v7a/lib/libssl.aignoring file openssl-armeabi-v7a/lib/libcrypto.a, file was built for archive which is not the architecture being linked (x86_64): openssl-armeabi-v7a/lib/libcrypto.a

Undefined symbols for architecture x86_64: "_OPENSSL_init_ssl", referenced from: initSSL() in DCSDK.cpp.o

Now i get the problem is that its compiling for x86_64 architecture but i have specified it in cmake to use ANDROID_ABI to armeabi-v7a.

if you need my CMakeLists.txt Here it is

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

来源:https://stackoverflow.com/questions/51783701/cmake-creating-a-shared-arm-library-which-can-be-used-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!