android - 如何使用 Cmake 将 Android 项目与 libandroid 链接起来?

标签 android c++ android-ndk

所以,我有一个支持 C++ 的 android 项目。在尝试构建 C++ 代码时,我的项目中有一些 undefined reference ,在回答了我的第一个问题后,这里有什么问题 link ,我找不到办法。

最佳答案

如果你有看起来像这样的 Cmake:

cmake_minimum_required(VERSION 3.4.1)

# OpenCV stuff
include_directories(D:\\opencv-3.2.0-android-sdk\\OpenCV-android-sdk\\sdk\\native\\jni\\include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
add_compile_options(-std=c++11)

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

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.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 )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib
                       -ljnigraphics
                       lib_opencv

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

只需将部分改成这样:

target_link_libraries( # Specifies the target library.
                       native-lib
                       -ljnigraphics
                       -landroid # Add this.
                       lib_opencv

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

关于android - 如何使用 Cmake 将 Android 项目与 libandroid 链接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43963992/

相关文章:

java.lang.RuntimeException : Unable to resume activity Attempted to access a cursor after it has been closed

c++ - 除了使用 str.substr( ) 在给定位置提取子字符串之外,还有其他方法吗?

c++ - 将二叉搜索树导出到 .csv 文件

c++ - 如何使用子类上重载的相同方法使工作可变参数方法继承

android - onStart() 到底是做什么的? - 安卓

java - 没有拍照和录像的相机许可?

javascript - 在移动浏览器上关闭屏幕时无法暂停音频

android - 如何将Android ARGB图像格式转换为opencv中使用的RGB图像格式?

java - android-arm/libjnidispatch.so 找不到错误

NDK 从 20.1 升级到 21.0 后 Android 应用崩溃