当前位置:网站首页>CMake教程Step1(基本起点)
CMake教程Step1(基本起点)
2022-07-05 16:36:00 【开始沸腾了】
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 double
const double inputValue = atof(argv[1]);
// calculate square root
const 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 [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内容如下:
// 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 version
std::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 root
const 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 进行重构,并尝试运行可执行程序,观察更改
边栏推荐
猜你喜欢
Browser rendering principle and rearrangement and redrawing
Jarvis OJ simple network management protocol
腾讯音乐上线新产品“曲易买”,提供音乐商用版权授权
干货!半监督预训练对话模型 SPACE
Get ready for the pre-season card game MotoGP ignition champions!
WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
Etcd build a highly available etcd cluster
飞桨EasyDL实操范例:工业零件划痕自动识别
随机推荐
Embedded UC (UNIX System Advanced Programming) -3
C# TCP如何设置心跳数据包,才显得优雅呢?
PHP talent recruitment system development source code recruitment website source code secondary development
Get ready for the pre-season card game MotoGP ignition champions!
[brush questions] effective Sudoku
ECU简介
Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
[wechat applet] read the life cycle and route jump of the applet
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
Embedded-c Language-2
通过proc接口调试内核代码
浏览器渲染原理以及重排与重绘
Etcd build a highly available etcd cluster
Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
SQL injection of cisp-pte (Application of secondary injection)
How to uninstall MySQL cleanly
Learnopongl notes (II) - Lighting
国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」