无法使用CMake变量PROJECT_SOURCE_DIR定义静态库的绝对路径

时间:2019-01-13 19:11:25

标签: android c++ cmake android-ndk

我正在尝试使用CMake构建一个Android NDK项目。我的项目依赖于某些预构建的静态库,例如 libcurl 。我努力将这些库与自己的本机库链接起来,然后遵循了answer。它说要为静态库放置绝对路径,这就是我现在面临的问题。

我找到了一个名为PROJECT_SOURCE_DIR的CMake variable,我假设我的项目根目录按照文档说明。但是,当我尝试在我的 CMakeList.txt 文件中使用该变量时,出现以下错误:

Build command failed.
Error while executing process /Users/sdsl/Library/Android/sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/sdsl/Documents/AfriGIS-SB/GitLab/AGMapKit3dDroid/agmapkit3d_droid/mapkit3dapp/.externalNativeBuild/cmake/debug/x86_64 --target mapkit-engine}
ninja: error: '../../../../mapkit3dapp/src/main/jni/CoreClasses/Libraries/droid/libcurl/x86_64/libcurl.a', needed by '../../../../build/intermediates/cmake/debug/obj/x86_64/libmapkit-engine.so', missing and no known rule to make it

这是我的 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.4.1)

add_library( # Sets the name of the library.
             mapkit-engine

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/jni/MKMapJNI.cpp
             src/main/jni/CoreClasses/IMKCPlatform.cpp)

# Specifies a path to native header files.
include_directories(src/main/jni/CoreClasses/)
include_directories(src/main/jni/CoreClasses/Libraries/droid/libcurl/include/)

find_library(log-lib log)
find_library(android-lib android)
target_link_libraries(mapkit-engine ${log-lib} ${android-lib})

target_link_libraries(mapkit-engine ${PROJECT_SOURCE_DIR}/mapkit3dapp/src/main/jni/CoreClasses/Libraries/droid/libcurl/${ANDROID_ABI}/libcurl.a)

我可以确保将静态库正确存储在项目目录中。而且,如果我尝试通过单击该PROJECT_SOURCE_DIR变量导航到项目根目录,它将带我进入IDE的根目录。

2 个答案:

答案 0 :(得分:0)

看起来您像一个人。试试

${PROJECT_SOURCE_DIR}/src/main/jni/CoreClasses/Libraries/droid/libcurl/${ANDROID_ABI}/libcurl.a

答案 1 :(得分:0)

我发现我对cmake变量PROJECT_SOURCE_DIR的理解是不正确的,官方文档也不太清楚其实际结果。

  

当前项目的顶级源目录。

     

这是最新的project()命令的源目录。

但是在大​​多数使用cmake构建的Android NDK项目中,通常不手动调用project()命令是很正常的。就是这种情况,PROJECT_SOURCE_DIR变量指向 CMakeLists.txt 所在的src目录。

就我而言,我的根src目录是:/Users/sdsl/Documents/AfriGIS-SB/GitLab/AGMapKit3dDroid/agmapkit3d_droid/PROJECT_SOURCE_DIR指向:/Users/sdsl/Documents/AfriGIS-SB/GitLab/AGMapKit3dDroid/agmapkit3d_droid/mapkit3dapp/

这也是我放置 CMakeLists.txt 文件的目录。感谢Tsyvarev帮助我弄清楚PROJECT_SOURCE_DIR实际执行的结果。