Android使用C++库,头文件无法引用,以及引用静态so库

项目目录结构如下:
出现的问题:
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
        )

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值