如何使用cmake在android studio中添加外部库? [英] How to add external library in android studio with cmake?

查看:352
本文介绍了如何使用cmake在android studio中添加外部库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to link live555.so and .h files with an Android project using cMake. If i don't use absolute path i'm getting error.

我的 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', 丢失,没有已知规则可以实现

Error:error: '../../../../src/main/jniLibs/live555/lib/armeabi-v7a/live555.so', needed by '../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so', missing and no known rule to make it

推荐答案

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

${PROJECT_SOURCE_DIR} is the directory where the main CMakeList.txt file resides. That's typically app/src/main/cpp. You can work out the correct relative path now.

这意味着您可以简单地编写

This means that you can simply write

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)

这篇关于如何使用cmake在android studio中添加外部库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆