Make & CMake Tutorial and note

Environment

Download cmake source code from official website
https://cmake.org/download/

Introduction

CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.

Basic

add_executable

This is a basic CmakeLists.txt file

cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(Tutorial VERSION 1.0)
# just like Makefile Tutorial: tutorial.cxx
add_executable(Tutorial tutorial.cxx) 
``![在这里插入图片描述](https://img-blog.csdnimg.cn/c2c8f5f9660940a296285094397fc51e.png)
`

```shell
cmake dir # 在当前文件夹生成Makefile文件,CMakeCache等
make # 编译
--------
cmake --build # 也是编译

configure_file

configure_file(input_file output_file [options])
https://cmake.org/cmake/help/latest/command/configure_file.html?highlight=configure_file
把input_file 替换成 output_file,并初始化input_file里面的变量(该变量必须被cmake注册过或是默认的)
在这里插入图片描述

target_include_directories

add search dir for specific target

# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")

PUBLIC will pupulate the INTERFACE_INCLUDE_DIRECTORIES of target
如果去掉target的话,就是全局作用

include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")

target_link_libraries

需要与add_subdirectory配合起来

# add the MathFunctions library
add_subdirectory(MathFunctions)
# add the executable
add_executable(Tutorial tutorial.cxx)
# link library for specific target
target_link_libraries(Tutorial PUBLIC MathFunctions)

add_subdirectory

在这里插入图片描述
将一个也要build的文件夹(里面含有子CmakeLists.txt)加入到build过程中,可以成为一个library
比如:

#./lib/CMakeList.txt
add_library(MathFunctions mysqrt.cxx)
#./CmakeLists.txt
add_subdirectory(MathFunctions)

file

File manipulation command.
GLOB Generate a list of files that match the and store it into the .

# file(GLOB <variable>  [LIST_DIRECTORIES true|false] 
#    [CONFIGURE_DEPENDS] [RELATIVE <path>] [<globbing-expressions>...])
file(GLOB SOURCES "src/*.hpp") 

GLOB_RECURSE mode will traverse all the subdirectories of the matched directory and match the files. Subdirectories that are symlinks are only traversed if FOLLOW_SYMLINKS is given

# file(GLOB_RECURSE <variable> [FOLLOW_SYMLINKS]
#    [LIST_DIRECTORIES true|false] [RELATIVE <path>] [CONFIGURE_DEPENDS]
#    [<globbing-expressions>...])
file(GLOB_RECURSE FILES_NEED_FORMAT "src/*.cpp" "src/*.cc" "src/*.hpp" "src/*.h"
                                    "test/*.cc")

add_custom_target

在这里插入图片描述

  • COMMAND
    相当于在build time执行一些customized script,比如用yacc去parse

add_library

Add a library to the project using the specified source files.

add_library(<name> [STATIC | SHARED | MODULE]
            [EXCLUDE_FROM_ALL]
            [<source>...])
Generally:
add_library(sparql_lex 
  ${PROJECT_SOURCE_DIR}/src/core/sparql/lex.yy.c 
  ${PROJECT_SOURCE_DIR}/src/core/sparql/sparql.tab.c)

Scenarios

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CMake是一个用于管理项目构建过程的开源工具。它通过配置文件的方式来定义源代码、编译器选项、库依赖和构建规则等信息,并根据这些信息生成针对不同平台和编译器的构建脚本。 CMake的教程可以帮助我们更好地理解和使用这个工具。在教程中,我们可以学习到CMake的基本概念、语法和用法,以及如何编写CMakeLists.txt文件来定义项目的构建过程。 教程一般会从简单的示例开始,逐步介绍CMake的各个方面。首先,我们可以学习如何定义基本的项目信息,如项目名称、版本号和语言要求。然后,我们可以了解如何添加源代码文件、头文件目录和编译选项。接着,我们可以学习如何定义库依赖和链接选项,以及如何生成可执行文件或库文件。 教程还可以教我们如何使用CMake的命令来进行条件编译、循环遍历和文件查找等操作。此外,教程还会介绍如何使用CMake的变量、宏和函数来简化构建过程和提高代码的可读性。 通过学习CMake的教程,我们可以更加高效地管理和构建项目,提高代码的可维护性和可移植性。无论是小型项目还是大型项目,CMake都可以帮助我们更好地组织代码、管理依赖和发布软件。 总之,CMake的教程是学习和使用CMake的重要资源,它可以帮助我们掌握CMake的基本概念和用法,提高项目的构建效率和质量。希望通过学习CMake的教程,我们能够更加熟练地使用这个工具,为我们的项目开发和维护带来便利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值