当前位置:网站首页>CMake教程(一)
CMake教程(一)
2022-06-28 23:05:00 【开始沸腾了】
CMake官方文档
参考官方cmake3.24教程翻译。我这里使用cmake 3.16来演示例子。
https://cmake.org/cmake/help/v3.24/guide/tutorial/A%20Basic%20Starting%20Point.html
1、基础的起点
构建并运行一个最基础的项目(从源代码文件构建的可执行文件),这将是我们教程的起点。
1.1、创建项目文件夹
创建项目文件夹rd-project, 并进入
mkdir rd-project
cd rd-project
1.2、创建源代码文件
源代码文件tutorial.cxx, 文件内容同官方教程计算平方根的代码tutorial.cxx (在cmake源码Step1目录中提供)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char* argv[]){
if(argc<2){
fprintf(stdout, "Uage: %s number\n", argv[0]);
return 1;
}
double inputValue = atof(argv[1]);
double outputValue = sqrt(inputValue);
fprintf(stdout, "The square root of %g is %g\n",inputValue, outputValue);
return 0;
}
1.3、编写CMakeLists.txt文件
在Step1目录中创建一个CMakeLists.txt文件,最基础的项目只需要一个三行CMakeLists.txt文件。
vi CMakeLists.txt
# cmake版本 # 查看cmake版本cmake --version 这里用的3.16
cmake_minimum_required(VERSION 3.16)
# 工程名
project(Tutorial)
# 添加app的源文件
add_executable(Tutorial tutorial.cxx)
注意,这个例子在CMakeLists.txt文件中使用了小写命令。CMake支持大写、小写和混合大小写命令。
2、构建和运行
2.1、创建一个构建目录
cd ..
mkdir rd_build
2.2、生成本地构建系统
进入构建目录并运行CMake来配置项目并生成一个本地构建系统
cd rd_build
cmake ../rd-project
2.3、实际编译/链接项目
通过本地构建系统来实际编译/链接项目:
cmake --build .
至此,构建编译完成。
2.4、尝试运行可执行程序
./Tutorial 4294967296
./Tutorial 10
./Tutorial
运行结果如下
3、为项目添加版本号和可配置的头文件
我们将添加的第一个特性是为我们的可执行文件和项目提供一个版本号。虽然我们可以在源代码中单独完成此操作,但使用CMakeLists.txt提供了更大的灵活性。
3.1、使用project()命令设置项目名称和版本号
cmake_minimum_required(VERSION 3.16)
# set the project name and version
project(Tutorial VERSION 1.0)
3.2、配置一个头文件,将版本号传递给源代码
configure_file(TutorialConfig.h.in TutorialConfig.h)
3.3、由于配置文件将被写入到二叉树中,我们必须将该目录添加到路径列表中以搜索包含文件。
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
3.4、使用你喜欢的编辑器,在源目录下创建TutorialConfig.h.in,包含以下内容:
// the configured options and settings for [email protected]@引用的变量可以通过CMakeLists.txt来设置
#define Tutorial_VERSION_MAJOR @[email protected]
#define Tutorial_VERSION_MINOR @[email protected]
当CMake配置这个头文件时,@[email protected]和@[email protected]的值将被替换。
tutorial.cxx中包含配置头文件TutorialConfig.h
更新tutorial.cxx源码,打印出可执行文件的名称和版本号。cxx内容如下:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "TutorialConfig.h"
int main(int argc, char *argv[]) {
if (argc < 2) {
// report version
std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
<< Tutorial_VERSION_MINOR << std::endl;
std::cout << "Usage: " << argv[0] << " number" << std::endl;
return 1;
}
// double inputValue = atof(argv[1]);
const double inputValue = std::stod(argv[1]);
double outputValue = sqrt(inputValue);
fprintf(stdout, "The square root of %g is %g\n", inputValue, outputValue);
return 0;
}
4、指定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版本 # 查看cmake版本cmake --version 这里用的3.16
cmake_minimum_required(VERSION 3.16)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# 工程名
# project(Tutorial)
# set the project name and version
project(Tutorial VERSION 1.0)
# 添加app的源文件
add_executable(Tutorial tutorial.cxx)
# 配置文件
configure_file(TutorialConfig.h.in TutorialConfig.h)
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
5、重新构建并测试更改
参考上文步骤 2.2 到 2.4 进行重构,并尝试运行可执行程序,观察更改

边栏推荐
- [sword finger offer] 50 First character that appears only once
- C interview questions_ 20220627 record
- 深入虚拟内存(Virtual Memory,VM)
- 【kotlin】好看的弹出框、自定义弹出框(对话框)、扩展函数、菊花等待条、消息提示框
- 【Try to Hack】nmap
- 邂逅阿维塔 11:强产品力下久违的新鲜感
- 浅析搭建校园在线教学视频汇聚平台的必要性及解决方案
- Google Earth Engine(GEE)——利用sentinel-2数据进行农作物提取分析
- 老家出资,俞敏洪设立两支基金
- 一张能卖上千万,商家扩张比玩家还快:球星卡的江湖你不懂
猜你喜欢

在线文本过滤小于指定长度工具

Flowable boundary timer

Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way
![Leetcode 324 swing sort ii[sort double pointer] the leetcode path of heroding](/img/41/b8ba8d771b7224eac1cc8c54fe9d29.png)
Leetcode 324 swing sort ii[sort double pointer] the leetcode path of heroding

k线图基础知识图解——单根K线的含义

How powerful is the Zadig build? Practice together

在线SQL转HTMLTable工具

Fanuc robot_ Introduction to Karel programming (2)_ Usage of general IO signal

老家出资,俞敏洪设立两支基金

第五章 虚拟存储器 练习
随机推荐
LeetCode 324 摆动排序 II[排序 双指针] HERODING的LeetCode之路
Undefined symbol main (referred from entry9a.o).
Qtcreater5.15.0 source code compilation process record
论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
Prometeus 2.36.0 新特性
Production environment sonarqube installation
微搭低代码中实现二维码生成
云计算的迷路者
[chapter 71 of the flutter problem series] mutual conversion between uint8list and image in flutter
Leetcode 324 swing sort ii[sort double pointer] the leetcode path of heroding
Didn't find an internship. He summed it up
A password error occurred when docker downloaded the MySQL image to create a database link
Detailed steps for MySQL to recover data through IBD files
Undefined symbol main (referred from entry9a.o).
flowable 边界定时器
[flutter issues Series title 71] Mutual Conversion between uint8list and Image in flutter
CIN at QT (the clearest tutorial in the whole network)
Realization of 2D code generation in micro build low code
frameworks/base/core/res/res/values/symbols.xml:3915: error: no definition for declared symbol解决办法
Wave picking of WMS warehouse management system module