项目目录结构如下: 出现的问题: 1、model.cpp需要里面引用include文件夹里面的头文件,报错无法引用 2、model.cpp里面需要使用其它so库的代码,报错无法引用 问题1解决方案:Cmake里面加入以下代码 set( INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include ${CMAKE_CURRENT_LIST_DIR}/include/flatbuffers/include) include_directories(${INCLUDE_DIRS}) 问题2解决办法:Cmake里面加入以下代码 必不可少的环节,在
target_link_libraries里面加入这个library
Cmake文件完整代码 # 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.18.1) # Declares and names the project. # 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(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/jniLibs/${ANDROID_ABI}) #include里面的头文件路径引用不到,就需要添加下面的代码 set( INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}/include ${CMAKE_CURRENT_LIST_DIR}/include/flatbuffers/include) include_directories(${INCLUDE_DIRS}) # 引入静态库 # ① 参数 1 ( add ) : 设置引入的静态库名称 # ② 参数 2 ( SHARED ) : 设置引入的函数库类型 : ① 静态库 STATIC ② 动态库 SHARED # ③ 参数 3 ( IMPORTED ) : 表示引入第三方静态库 , 导入静态库 , 相当于预编译静态库 # add_library( # 设置引入的静态库名称 tflite # 设置引入的函数库类型为静态库 SHARED # 表示引入第三方静态库 IMPORTED ) # 设置上述静态库的导入路径 # 设置目标属性参数 : # ① 参数 1 ( add ) : 要设置哪个函数库的属性 # ② 参数 2 ( PROPERTIES ) : 设置目标属性 # ③ 参数 3 ( IMPORTED_LOCATION ) : 设置导入路径 # ④ 参数 4 : 配置静态库的文件路径 set_target_properties( tflite PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_LIST_DIR}/jniLibs/${ANDROID_ABI}/libtensorflowlite.so ) # 设置函数库编译 add_library( # Sets the name of the library. # 参数 1 : 设置生成的动态库名称 model # 参数 2 : 设置生成的函数库类型 : ① 静态库 STATIC ② 动态库 SHARED SHARED # 参数 3 : 配置要编译的源文件 model.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. #add_library( # Sets the name of the library. # flatbuffers # # # Sets the library as a shared library. # STATIC # # # Provides a relative path to your source file(s). # IMPORTED) #set_target_properties( # flatbuffers # PROPERTIES # IMPORTED_LOCATION # ${CMAKE_CURRENT_LIST_DIR}/jniLibs/${ANDROID_ABI}/libflatbuffers.a) #target_link_libraries( # Specifies the target library. # model # flatbuffers # # Links the target library to the log library # # included in the NDK. # ${log-lib}) target_link_libraries( # Specifies the target library. model # Links the target library to the log library # included in the NDK. ${log-lib} ${android-lib} tflite )