美文网首页随笔-生活工作点滴
CMake构建脚本语法介绍

CMake构建脚本语法介绍

作者: 码上述Andy | 来源:发表于2019-07-23 22:45 被阅读0次

一.基本语法

1.cmake_minimum_required(VERSION 3.4.1)

指定最小cmake版本。

2.find_library( log-lib log )

搜索log库路径并赋值给log-lib。

3.target_link_libraries(native-lib ${log-lib} )

链接目标动态库,将log-lib库链接到native-lib上。

4.include_directories(include_path)

设置included头文件位置。

5.set(SOURCE_FILES xx.cpp yy.cpp zz.cpp)

设置cmake变量,SOURCE_FILES为变量。

6.add_library(native-lib SHARED|STATIC src/main/cpp/native-lib.cpp )

将指定的源文件编译成名为native-lib动态库,SHARED为动态库,STATIC为静态库。

7.aux_source_directory(source_path 变量A)

搜索source_path目录中文件保存到变量A,比如aux_source_directory(. SRC_LIST):搜索当前目录文件保存到变量SRC_LIST中。

8.project(project_name)

设置项目名称

9.message(${PROJECT_SOURCE_DIR})

打印信息

10.add_subdirectory(subdirectory_path)

添加子目录,subdirectory_path为指定包含CMakeLists.txt和源码文件的子目录位置

11.add_executable(demo ${SRC_LIST})

将源文件目录生成执行文件demo。其实就是生成demo.so文件。

二.简单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)

project(demo)

# 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).
             src/main/cpp/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 path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

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

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

相关文章

网友评论

    本文标题:CMake构建脚本语法介绍

    本文链接:https://www.haomeiwen.com/subject/kdqjhctx.html