CMakeList 详解

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})  #设置可执行源文件编译成的可执行文件名

 

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

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值