CMake教程Step1(基本起点)
CMake官方文档
参考官方cmake3.24教程翻译
https://cmake.org/cmake/help/v3.24/guide/tutorial/index.html
https://gitlab.kitware.com/cmake/cmake/-/tree/master/Help/guide/tutorial
step1
https://cmake.org/cmake/help/v3.24/guide/tutorial/A%20Basic%20Starting%20Point.html
我的仓库 :
https://github.com/FRBoiling/cmake-tutorial.git
基本起点
构建并运行一个最基础的项目(从源代码文件构建的可执行文件),这将是我们教程的起点。
创建项目文件夹
创建项目文件夹Step1, 并进入
mkdir Step1
cd Step1
创建源代码文件
源代码文件tutorial.cxx, 文件内容同官方教程计算平方根的代码tutorial.cxx (在cmake源码Step1目录中提供)
vi tutorial.cxx
vi tutorial.cxx
// A simple program that computes the square root of a number
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>int main(int argc, char* argv[])
{if (argc < 2) {std::cout << "Usage: " << argv[0] << " number" << std::endl;return 1;}// convert input to doubleconst double inputValue = atof(argv[1]);// calculate square rootconst double outputValue = sqrt(inputValue);std::cout << "The square root of " << inputValue << " is " << outputValue<< std::endl;return 0;
}
编写CMakeLists.txt文件
在rd-project目录中创建一个CMakeLists.txt文件,最基础的项目只需要一个三行CMakeLists.txt文件。
vi CMakeLists.txt
cmake_minimum_required(VERSION 3.10)# set the project name
project(Tutorial)# add the executable
add_executable(Tutorial tutorial.cxx)
注意,这个例子在CMakeLists.txt文件中使用了小写命令。CMake支持大写、小写和混合大小写命令。
注意,只有系统指令是不区分大小写的,但是变量和字符串是区分大小写的
构建和运行
2.1、创建一个构建目录
cd ..
mkdir Step1_build
2.2、生成本地构建系统
进入构建目录并运行CMake来配置项目并生成一个本地构建系统
cd Step1_build
cmake ../Step1
2.3、实际编译/链接项目
通过本地构建系统来实际编译/链接项目:
cmake --build .
至此,构建编译完成。
2.4、尝试运行可执行程序
./Tutorial 4294967296
./Tutorial 10
./Tutorial
运行结果如下
为项目添加版本号和可配置的头文件
我们将添加的第一个特性是为我们的可执行文件和项目提供一个版本号。虽然我们可以在源代码中单独完成此操作,但使用CMakeLists.txt提供了更大的灵活性。
3.1、使用project()命令设置项目名称和版本号
cmake_minimum_required(VERSION 3.10)# # set the project name
# project(Tutorial)
# set the project name and version
project(Tutorial VERSION 1.0)
3.2、配置一个头文件,将版本号传递给源代码
configure_file(TutorialConfig.h.in TutorialConfig.h)
3.3、由于配置文件将被写入到二叉树中,我们必须将该目录添加到路径列表中以搜索包含文件。
# 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}")
3.4、使用你喜欢的编辑器,在源目录下创建TutorialConfig.h.in,包含以下内容:
// the configured options and settings for Tutorial@@引用的变量可以通过CMakeLists.txt来设置
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
当CMake配置这个头文件时,@Tutorial_VERSION_MAJOR@和@Tutorial_VERSION_MINOR@的值将被替换。
tutorial.cxx中包含配置头文件TutorialConfig.h
更新tutorial.cxx源码,打印出可执行文件的名称和版本号。cxx内容如下:
// A simple program that computes the square root of a number
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include "TutorialConfig.h"int main(int argc, char* argv[])
{if (argc < 2) {// report versionstd::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."<< Tutorial_VERSION_MINOR << std::endl;std::cout << "Usage: " << argv[0] << " number" << std::endl;return 1;}// convert input to double// const double inputValue = atof(argv[1]);const double inputValue = std::stod(argv[1]);// calculate square rootconst double outputValue = sqrt(inputValue);std::cout << "The square root of " << inputValue << " is " << outputValue<< std::endl;return 0;
}
指定c++标准
接下来,让我们在tutorial.cxx中将atof替换为std::stod,为我们的项目添加一些c++ 11特性。同时,删除
#include 。
//文件tutorial.cxx// double inputValue = atof(argv[1]);const double inputValue = std::stod(argv[1]);
我们需要在CMake代码中显式地声明它应该使用的正确标准。
在CMake中启用对特定c++标准的支持最简单的方法是使用CMAKE_CXX_STANDARD变量。
在本教程中,将cmakellists .txt文件中的CMAKE_CXX_STANDARD变量设置为11,并将CMAKE_CXX_STANDARD_REQUIRED设置为True。
必须确保在add_executable调用的上方添加了CMAKE_CXX_STANDARD声明。
教程到此时,完整的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)# add the executable
add_executable(Tutorial tutorial.cxx)# 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}")
重新构建并测试更改
参考上文步骤 2.2 到 2.4 进行重构,并尝试运行可执行程序,观察更改
CMake教程Step1(基本起点)相关推荐
- CMake Tutorial Step1
CMake Tutorial Step1 参考资料:Step 1: A Basic Starting Point - CMake 3.26.3 Documentation Tutorial工程:官方T ...
- 【B站视频教程笔记】基于VSCode和CMake实现C/C++开发 | Linux篇(gcc/g++)(安装、配置、使用详细教程)(VSCode教程)(CMake教程)(精!)
基于VSCode和CMake实现C/C++开发 | Linux篇 文章目录 目录结构 文件编辑 vim(编辑器之神,linux里可以畅通无阻,必学,但不是现在!) 安装GCC和GDB g++编译过程 ...
- [转](转载+整理)超详细的cmake教程
cmake教程 参考 什么是cmake cmake 常见语法罗列 CMake可用变量 入门案例 单个源文件 多个源文件 同一目录,多个源文件 多个目录,多个源文件 进阶案例 自定义编译选项 指定安装和 ...
- CMake教程Step7(安装打包)
CMake官方文档 参考官方cmake3.24教程翻译.我这里使用cmake 3.16来演示例子. step7 https://cmake.org/cmake/help/v3.24/guide/tut ...
- (转载+整理)超详细的cmake教程
cmake教程 参考 什么是cmake cmake 常见语法罗列 CMake可用变量 入门案例 单个源文件 多个源文件 同一目录,多个源文件 多个目录,多个源文件 进阶案例 自定义编译选项 指定安装和 ...
- CMake教程(二)- 添加静态库文件和动态库文件
CMake教程(二)- 添加静态库文件和动态库文件 什么是库文件 静态链接库 动态链接库 静态库和动态库的区别 如何在CMake中添加库文件 CMake 中 target_link_libraries ...
- cmake教程-入门篇
从事linux的开发工作,不可避免的需要进行编译构建的工作,直接编辑Makefile,不仅需要熟悉Makefile的语法,还需要知道依赖和推导规则,比较麻烦.而cmake工具,只需要开发者提供头文件路 ...
- ubuntu18.04下--CMake教程
CMake 教程 文章目录 CMake 教程 0. CMake安装及卸载 1. 创建简单的实例 2. CMake 重要参数 3. 构建项目结构 4. CMakeList.txt 模板 CMake是开源 ...
- cmake教程(cmake教程pdf)
gcc和gcc-c已经装完了,centos安装cmake时不能找? 找不到makefile应该是当前目录下没有makefile文件吧,坚持下目录,还有,安装了gcc不一定带make的 求教程求教程谁有 ...
最新文章
- C语言开发单片机如何避免全局变量过多混乱
- 设计模式之一:单例模式
- 【Paper】2007_Consensus control for a class of networks of dynamic agents 二阶静态一致性
- 笨办法学Python——学习笔记1
- python教程简易版_简洁的十分钟Python入门教程
- python中timedelta_Python – 使用时间戳,timedelta的日期和时间比较
- 检验例题_高考必考|化学工艺流程之物质的分离提纯及检验鉴别,轻松拿分
- 《从零构建前后分离的web项目》准备 - 前端了解过关了吗?
- pil对图像加透明 python_使用Python图像处理库Pillow处理图像文件
- ubuntu 19.04 + lenovo-xiaoxin-I2000 触摸板右键单击无法使用
- 19.Java 数据库编程
- matlab做挖掘机仿真,基于Proe_Adams_Matlab挖掘机的机电液一体化仿真
- PV、UV、IV的概念
- linux宝塔下如何强制ssl,宝塔面板一键安装SSL证书强制HTTPS访问设置
- 获取窗口 history数量_带你走进JavaScript世界系列——history 对象
- python3类c语言LL1文法编译器设计
- 用python做公众号网页_使用python一步一步搭建微信公众平台(一)
- 计算机操作上机考试题目,计算机系统操作工上机操作考试题.pdf
- F. Equalize the Array(思维+前缀和)
- - **体感试衣镜等功能代码工程分享**
热门文章
- 西交自动控制研究所 教授团队 招聘科研助理
- python绘图点样式
- Excel导入数据,未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序
- 最大似然估计公式推导
- ios逆向- 01逆向原理Class-dump安装及获取头文件
- 苹果电脑录屏,掌握这两种方法就足够
- Python是什么 它有哪些优点
- 苹果手机关闭自动更新_极客修:苹果手机不关闭后台应用真的省电吗 内含秘诀...
- 网络爬虫学习(三)-scrapy框架
- 2023人力资源管理师报名时间是什么时候