Android Studio 下Cmake的CMakeLists.txt文件配置

1.生成多.so模式,如图目录结构,需要单独每个模块生成自己的.so

Android Studio 下Cmake的CMakeLists.txt文件配置_第1张图片

外层CMakeLists.txt

Android Studio中, 具体Module中的build.gradle中先指定这个文件的位置

Android Studio 下Cmake的CMakeLists.txt文件配置_第2张图片

cmake_minimum_required(VERSION 3.4.1) #指定编译器版本

add_subdirectory(lic)#指定子文件夹
add_subdirectory(serialport)

里层CMakeLists.txt

 

set(PROJECT_NAME xxx)

project (${PROJECT_NAME})

add_library( ${PROJECT_NAME} SHARED
             xxx.c
             )

find_library( log-lib log )

target_link_libraries( ${PROJECT_NAME}
                       ${log-lib} )

 

2.多目录,多层次目录,多文件的时候,批量定义源文件(此种方式适合编译第三方库),如图
Android Studio 下Cmake的CMakeLists.txt文件配置_第3张图片
CMakeLists.txt

 

set(PROJECT_NAME box2d)

project (${PROJECT_NAME})

set(SRC_DIR ../Box2d)

#方式1
file(GLOB_RECURSE cpp_src "${SRC_DIR}/*.cpp")#指定当前目录下的所有.cpp文件(包括子目录)
add_library( ${PROJECT_NAME} SHARED ${cpp_src})

#方式2 (要是乐意也可以这么写)
#file(GLOB cpp_src_1 "${SRC_DIR}/Collision/*.cpp")
#file(GLOB cpp_src_2 "${SRC_DIR}/Collision/Shapes/*.cpp")
#file(GLOB cpp_src_3 "${SRC_DIR}/Common/*.cpp")
#file(GLOB cpp_src_4 "${SRC_DIR}/Dynamics/*.cpp")
#file(GLOB cpp_src_5 "${SRC_DIR}/Dynamics/Contacts/*.cpp")
#file(GLOB cpp_src_6 "${SRC_DIR}/Dynamics/Joints/*.cpp")
#file(GLOB cpp_src_7 "${SRC_DIR}/Rope/*.cpp")

#set(cpp_src_dir ${cpp_src_1} ${cpp_src_2} ${cpp_src_3} ${cpp_src_4} ${cpp_src_5} ${cpp_src_6} ${cpp_src_7} )
#add_library( ${PROJECT_NAME} SHARED ${cpp_src_dir})

3.更多功能,添加和依赖外部库或依赖静态库。

目录结构:Android Studio 下Cmake的CMakeLists.txt文件配置_第4张图片

CMakeLists.txt

set(PROJECT_NAME xxxx)

set(SRC_DIR src/main/cpp/face)

project(${PROJECT_NAME})

set(OpenCV_DIR E:/OpenCV-android-sdk/sdk/native/jni)

include_directories(${OpenCV_DIR}/include)
include_directories(${SRC_DIR}/include)

add_library(${PROJECT_NAME} SHARED

             xxxx.cpp
             xxxx2.cpp
             )

find_library(log-lib log)

set(LINK_LIB_DIR ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})

add_library(ncnn STATIC IMPORTED)
set_target_properties(ncnn PROPERTIES IMPORTED_LOCATION ${LINK_LIB_DIR}/libncnn.a)

add_library(opencv SHARED IMPORTED)
set_target_properties(opencv PROPERTIES IMPORTED_LOCATION ${LINK_LIB_DIR}/libopencv_java.so)

target_link_libraries(${PROJECT_NAME}
                      ncnn
                      opencv
                      -lgomp
                      ${log-lib} )

 

你可能感兴趣的:(Android功能)