CMake Tutorial(Step 1):A Basic Starting Point

根据CMake官(https://cmake.org/cmake/help/latest/guide/tutorial/)的教程编写,仅供参考。


前言

根据CMake官网(https://cmake.org/cmake/help/latest/guide/tutorial/)的教程编写,总共会有12个step:
Step 1: A Basic Starting Point
Step 2: Adding a Library
Step 3: Adding Usage Requirements for a Library
Step 4: Adding Generator Expressions
Step 5: Installing and Testing
Step 6: Adding Support for a Testing Dashboard
Step 7: Adding System Introspection
Step 8: Adding a Custom Command and Generated File
Step 9: Packaging an Installer
Step 10: Selecting Static or Shared Libraries
Step 11: Adding Export Configuration
Step 12: Packaging Debug and Release


一、CMake是什么?

CMake 是一个开源的跨平台工具,用来build,和测试软件包。可以使用简单的独立配置文件控制软件编译过程。与许多跨平台系统不同,CMake 被设计为与本地构建环境结合使用。

二、使用步骤

1. Building a Basic Project

最基本的 CMake 项目是由单个源代码文件构建的可执行文件。对于这样的简单项目,只需要一个包含三个命令的 CMakeLists.txt 文件。

任何项目中最大的 CMakeLists.txt 都必须使用 cmake_minimum_required()命令指定最小的 CMake 版本。这将建立策略设置,并确保以下 CMake 函数使用兼容的 CMake 版本运行。

要启动一个项目,我们使用 project ()命令来设置项目名称。这个调用是每个项目都需要的,并且应该在cmake_minimum_required()之后立即调用。正如我们将在后面看到的,此命令还可用于指定其他项目级信息,如语言或版本号。

最后,add_executable()命令告诉 CMake 使用指定的源代码文件创建一个可执行文件。

编写CMakelists.txt里的内容:

cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Tutorial VERSION 1.0)

# add the executable
add_executable(Tutorial tutorial.cxx)

2. Specifying the C++ Standard

CMake 有许多变量以 CMAKE _ 开始。其中两个特殊的可设置变量是 CMAKE_CXX_STANDARDCMAKE_CXX_STANDARD_REQUIRED。它们可以一起用来指定构建项目所需的 C + + 标准。

编写CMakelists.txt里的内容:

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

3. Adding a Version Number and Configured Header File

有时候,让在 CMakelists.txt 文件中定义的变量在源代码中也可用会很有用。实现这一点的一种方法是使用配置的头文件。我们创建一个包含一个或多个要替换的变量的输入文件。

通常,TutorialConfig.h.in是一个包含占位符的模板文件,用于生成实际的配置文件TutorialConfig.h。在模板文件中,可以使用类似@VAR_NAME@的占位符来表示需要在配置时替换的变量。configure_file()命令将根据项目的配置,将占位符替换为实际的值,并生成最终的配置文件。

通过这种方式,可以在构建过程中动态地生成配置文件,将项目的相关信息注入到代码中。这样,可以根据不同的构建选项或环境进行配置,而无需手动修改配置文件。
先编写TutorialConfig.h.in文件:

// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

然后修改CMakelists.txt里的内容:

# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)

最终的CMakelists.txt

cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Tutorial VERSION 1.0)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)

# TODO 2: Use add_subdirectory() to add MathFunctions to this project

# add the executable
add_executable(Tutorial tutorial.cxx)

# TODO 3: Use target_link_libraries to link the library to our executable

# TODO 4: Add MathFunctions to Tutorial's target_include_directories()
# Hint: ${PROJECT_SOURCE_DIR} is a path to the project source. AKA This folder!

# 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}"
                           )

总结

基础版本的CMakelists,txt文件的编写,其中包括了CMake最低版本的要求,设定项目的名称及版本号,设置C++标准,配置头文件,添加可执行文件,设定项目二进制路径。

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wendel_CN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值