CMake使用遇坑记(一)
Android工程在加入了一系列 .so 库之后,点击运行之后,报了如下错误
Error:error:'../../../../../jniLibs/armeabi/libavcodec57.so',needed by '../../../../build/intermediates/cmake/debug/obj/armeabi/libnative-lib.so', missing and no known rule to make it
没错,一定是路径错了,但是由于是第一次,这个配置型错误,真让人一顿好找
set(lib_src_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}) include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include)
原先可不是这样的,原先如下
set(lib_src_DIR ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}) include_directories(${CMAKE_SOURCE_DIR}/../include)
好吧,对CMake的规则理解还真是不够,好在问题勉强算是解决了
附上工程目录
以及CMakelist.txt
# For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html # Sets the minimum version of CMake required to build the native library. cmake_minimum_required(VERSION 3.4.1) # 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. set(lib_src_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}) include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/include) add_library(avcodec-57_lib SHARED IMPORTED) set_target_properties(avcodec-57_lib PROPERTIES IMPORTED_LOCATION ${lib_src_DIR}/libavcodec-57.so) add_library(avformat-57_lib SHARED IMPORTED) set_target_properties(avformat-57_lib PROPERTIES IMPORTED_LOCATION ${lib_src_DIR}/libavformat-57.so) add_library(avutil-55_lib SHARED IMPORTED) set_target_properties(avutil-55_lib PROPERTIES IMPORTED_LOCATION ${lib_src_DIR}/libavutil-55.so) add_library(swresample-2_lib SHARED IMPORTED) set_target_properties(swresample-2_lib PROPERTIES IMPORTED_LOCATION ${lib_src_DIR}/libswresample-2.so) add_library(swscale-4_lib SHARED IMPORTED) set_target_properties(swscale-4_lib PROPERTIES IMPORTED_LOCATION ${lib_src_DIR}/libswscale-4.so) #native-lib文件 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 this # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib android avcodec-57_lib avformat-57_lib avutil-55_lib swresample-2_lib swscale-4_lib # Links the target library to the log library # included in the NDK. ${log-lib} )