JNI

Android NDK(JNI)详解教程一 CMakeLists

2023-02-28  本文已影响0人  Kael_Zhang的安卓笔记

什么是NDK、JNI

CMakeLists.txt 是什么?

CMakeLists.txt 详解

cmake_minimum_required(VERSION 3.10.2)
project("projectname")
add_executable(projectname native-lib.cpp) //生成可执行文件
add_library(projectname STATIC native-lib.cpp) //生成静态库(默认)
add_library(projectname SHARED native-lib.cpp) //生成动态库
add_library(projectname SHARED native-lib.cpp)

2.搜索指定目录下所有源文件(不会递归遍历子目录)

//搜索一个目录下所有的源代码文件,并将列表存储再一个变量中
aux_source_directory(. SRC_LIST)
add_library(projectname SHARED ${SRC_LIST})

3.自定义搜索源文件规则(推荐使用 aux_source_directory,不推荐使用 file)

//搜索一个目录下所有的源代码文件,并将列表存储再一个变量中
file(GLOB SRC_LIST "*.cpp")
file(GLOB SRC_LIST "*.c")
file(GLOB SRC_LIST "src/*.cpp")
aux_source_directory(src SRC_PROTOCOL_LIST)
add_library(projectname SHARED ${SRC_LIST})
// find_library(VAR name path)查找到指定的预编译库,并将它的路径存储在变量中。
// 默认的搜索路径为cmake 包含的系统库,因此如果是NDK的公共库只需要指定库的name即可。
# 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)
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)
link_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/libs
)
# 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.
        projectname

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})
set(SRC_LIST main.cpp test1.cpp) //设置源代码变量
set(SRC_LIST test2.cpp) //追加
set(EXECUTABLE_OUTPUT_PATH [path]) //设置可执行文件的输出路径
set(LIBRARY_OUTPUT_PATH [output_path]) //设置库文件的输出路径
set(CMAKE_CXX_FLAGS "-Wall std=c++11") //设置C++编译参数
message(${PROJECT_SOURCE_DIR})
message("build with debug mode")
message(WARNING "this is warnning message")
...
#添加child目录
add_subdirectory(child)
...
target_link_libraries( # Specifies the target library.
        projectname
        child
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

子CMakeLists.txt

#child目录下的CMakeLists.txt
...
aux_source_directory(. DIR_SRCS)
...
add_library(
        child
        SHARED
        ${DIR_SRCS}
)
上一篇下一篇

猜你喜欢

热点阅读