CMakeList 详解

    技术2022-07-11  78

    CMake 构建脚本是一个纯文本文件,您必须将其命名为 CMakeLists.txt,并在其中包含 CMake 构建您的 C/C++ 库时需要使用的命令。如果您的原生源代码文件还没有 CMake 构建脚本,您需要自行创建一个,并在其中包含适当的 CMake 命令。

    添加so库

    可以用你写的cpp源文件生成一个so

    add_library( # Specifies 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 )

    也可以直接导入已有的库(import)

    add_library( imported-lib SHARED IMPORTED ) set_target_properties( # Specifies the target library. imported-lib # Specifies the parameter you want to define. PROPERTIES IMPORTED_LOCATION # Provides the path to the library you want to import. imported-lib/src/${ANDROID_ABI}/libimported-lib.so )

    使用 include_directories() 命令并包含相应头文件的路径,使CMake 能够在编译时定位你的头文件:

    include_directories( imported-lib/include/ )

    设置接口库对第三方库的链接,其中第一个参数为接口库,后面参数为第三方库

    target_link_libraries(libimported-lib)

    一个简单的CMakeList.txt 例子

    cmake_mini_required(VERSION 3.4.1) #定义cmake 支持的版本 project(my_proj) #项目名 include_directories(include) #头文件路径 aux_source_directory(src DIR_SOURCE) #源文件目录,DIR_SOURCE 为定义的变量 set(SRC_FILE_PATH ${DIR_SOURCE}) #设置环境变量,编译用到的源文件都要放到这里 add_executable(my_proj ${SRC_FILE_PATH}) #设置可执行源文件编译成的可执行文件名

     

    未完待续........

    Processed: 0.010, SQL: 9