当前位置:网站首页>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 进行重构,并尝试运行可执行程序,观察更改
边栏推荐
- [729. My schedule I]
- Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
- Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
- Jarvis OJ shell traffic analysis
- Embedded-c language-6
- 【性能测试】全链路压测
- Iphone14 with pill screen may trigger a rush for Chinese consumers
- Timestamp strtotime the day before or after the date
- 【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!
- Yarn common commands
猜你喜欢
[Web attack and Defense] WAF detection technology map
Jarvis OJ Telnet Protocol
[brush questions] effective Sudoku
Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
Jarvis OJ Webshell分析
国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
飞桨EasyDL实操范例:工业零件划痕自动识别
机器学习编译第2讲:张量程序抽象
SQL injection of cisp-pte (Application of secondary injection)
Cs231n notes (bottom) - applicable to 0 Foundation
随机推荐
Copy mode DMA
国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
Deep learning plus
[61dctf]fm
Embedded UC (UNIX System Advanced Programming) -2
The first lesson of EasyX learning
Wechat official account web page authorization login is so simple
The survey shows that the failure rate of traditional data security tools in the face of blackmail software attacks is as high as 60%
Twig数组合并的写法
thinkphp3.2.3
[729. My schedule I]
Embedded-c Language-3
Wsl2.0 installation
Embedded -arm (bare board development) -2
叩富网开期货账户安全可靠吗?怎么分辨平台是否安全?
美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
Detailed explanation of use scenarios and functions of polar coordinate sector diagram
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale