CMake语法快速入门

本文是CMake的快速入门教程,详细介绍了CMake的基本语法,包括CMakeLists.txt的编写、变量、列表、流程控制、条件命令、循环命令、自定义函数和宏命令的使用,以及变量的作用域。通过实例解析,帮助读者掌握CMake在Android Studio中的应用。
摘要由CSDN通过智能技术生成

1.Cmake语法详解

什么是Cmake

  1. 在Android Studio 2.2及以上,构建原生库的默认工具是CMake
  2. CMake是一个跨平台的构建工具,可以用简单的语句来描述所有平台的安装(编译过程)。能够输出各种各样的makefile或者project文件。CMake并不直接构建最终的软件,而是生成其他的工具的脚本,比如生成makefile,然后在依据这个工具的构建方式使用。
  3. CMake是一个比make更高级的编译配置工具,他可以根据不同的平台,不同的编译器,生成相应的makefile或者vcproj项目,从而达到跨平台的目的。Android Studio利用CMake生成的是ninja。ninja是一个关注速度的构建系统。我们不需要关心ninja的脚本,知道怎么配置CMake就可以了
  4. CMake其实是一个跨平台的支持产出各种不同的构建脚本的一个工具

CMake源文件

  1. CMake的源码文件可以包含命令、注释、空格和换行。
  2. 以CMake编写的源文件以CMakeLists.txt命名或以cmake为拓展名。
  3. 可以通过add_subdirectory()命令吧子目录的CMake源文件添加进来。
  4. CMake源文件中所有的有效的语句都是命令,可以使内置命令或自定义的函数\宏命令

Android 默认的CMakeLists.txt

# 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.4.1)

# 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.

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.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 pa
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值