Adding sub directory in cmake not working

2019-06-03 05:11发布

问题:

I'm trying to add another cmake project which has CMakeLists.txt file as a compilation dependency which i can use in another .cpp file.

Location of project which i want to add: Users/brainfreak/Downloads/assimp-master/

Location of main project: /Users/brainfreak/AndroidStudioProjects/ModelShow/app/src/main/cpp/hellojni.cpp

This is used as a native code in a Android Studio project. I followed the tutorial in https://developer.android.com/studio/projects/add-native-code.html#create-cmake-script under "Include other CMake projects"

This is the main CMakeLists.txt that i came up with: /Users/brainfreak/AndroidStudioProjects/ModelShow/app/src/main/cpp/CMakeLists.txt

cmake_minimum_required( VERSION 2.6 )

add_library(model-lib SHARED hellojni.cpp)

set (src_dir Users/brainfreak/Downloads/assimp-master/)
set (output_dir Users/brainfreak/Downloads/assimp-master/output)
file(MAKE_DIRECTORY ${output_dir})

add_subdirectory(${src_dir} ${output_dir})

add_library(assimp STATIC IMPORTED)
set_target_properties( assimp PROPERTIES IMPORTED_LOCATION
                       ${output_dir}/${ANDROID_ABI}/assimp)
include_directories(${src_dir}/include)

target_link_libraries(model-lib assimp)

The error i always get:

Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
  Error while executing process /Users/brainfreak/Android/sdk/cmake/3.6.3155560/bin/cmake with arguments {--build /Users/brainfreak/AndroidStudioProjects/ModelShow/app/.externalNativeBuild/cmake/debug/x86 --target model-lib}
  ninja: error: 'Users/brainfreak/Downloads/assimp-master/output/x86/assimp', needed by '/Users/brainfreak/AndroidStudioProjects/ModelShow/app/build/intermediates/cmake/debug/obj/x86/libmodel-lib.so', missing and no known rule to make it

I don't know what file to place under "${output_dir}/${ANDROID_ABI}/" for the script to find. Can you tell where i'm going wrong?

回答1:

Assuming that you got one of the latest releases from https://github.com/assimp and followed the instructions (note that this was tested with NDK r14, available for download from https://developer.android.com/ndk/downloads/older_releases), you have produced file libassimp.so inside project "Code" folder. Make sure that you build the x86 version of the library.

Copy this file to /Users/brainfreak/Downloads/assimp-master/output/x86/, and prepare your CMakeLists.txt:

cmake_minimum_required( VERSION 2.6 )

add_library(model-lib SHARED hellojni.cpp)

set (src_dir /Users/brainfreak/Downloads/assimp-master/)
set (output_dir /Users/brainfreak/Downloads/assimp-master/output)
file(MAKE_DIRECTORY ${output_dir})

add_subdirectory(${src_dir} ${output_dir})

add_library(assimp STATIC IMPORTED)
set_target_properties( assimp PROPERTIES IMPORTED_LOCATION
                       ${output_dir}/${ANDROID_ABI}/libassimp.so)
include_directories(${src_dir}/include)

target_link_libraries(model-lib assimp)

Note that the file that your script was missing slash (/) before Users which could cause the confusion.

Don't forget to set abiFilters in your app/build.gradle:

ndk {
  // Specifies the ABI configurations of your native
  // libraries Gradle should build and package with your APK.
  abiFilters 'x86'
}