【问题标题】:How to add external library in android studio with cmake?如何使用 cmake 在 android studio 中添加外部库?
【发布时间】:2018-10-05 12:18:43
【问题描述】:

我正在尝试使用 cMake 将 live555.so 和 .h 文件与 Android 项目链接。如果我不使用绝对路径,就会出错。

我的 cMake 文件:

cmake_minimum_required(VERSION 3.4.1)

include_directories( ${PROJECT_SOURCE_DIR}/src/main/jniLibs/live555/include )

add_library( live555 SHARED IMPORTED )

set_target_properties( live555 PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/live555/lib/${ANDROID_ABI}/live555.so)

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 )

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 )

target_link_libraries( # Specifies the target library.
                   native-lib
                   android
                   live555
                   ${log-lib} )

还有错误

错误:错误: '../../../../src/main/jniLibs/live555/lib/armeabi-v7a/live555.so', 需要 '../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so', 丢失并且没有已知的规则来制作它

【问题讨论】:

  • 您是否将.so 复制到src/main/jniLibs/live555/lib/armeabi-v7a 目录中?
  • 是的,我做到了。如果我将“ ${PROJECT_SOURCE_DIR}”更改为绝对路径,则它接受否则会给出该错误。所以我想知道我是否必须使用像 /usr/local... 这样的系统路径?
  • 在set_target_propeties之前使用message(STATUS ${PROJECT_SRC_DIR}),然后尝试获取set后的属性进行检查:stackoverflow.com/questions/24881408/…

标签: android cmake android-ndk


【解决方案1】:

${PROJECT_SOURCE_DIR} 是主 CMakeList.txt 文件所在的目录。这通常是 app/src/main/cpp。你现在可以算出正确的相对路径了。

这意味着你可以简单地写

include_directories( ${PROJECT_SOURCE_DIR}/../jniLibs/live555/include )

add_library( live555 SHARED IMPORTED )

set_target_properties( live555 PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../jniLibs/live555/lib/${ANDROID_ABI}/live555.so)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    相关资源
    最近更新 更多