当前位置:网站首页>Cmake tutorial step1 (basic starting point)
Cmake tutorial step1 (basic starting point)
2022-07-05 17:51:00 【It's beginning to boil】
CMake Official documents
Refer to the official cmake3.24 Course translation
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
My warehouse :
https://github.com/FRBoiling/cmake-tutorial.git
Basic starting point
Build and run a basic project ( Executable files built from source code files ), This will be the starting point of our tutorial .
Create project folder
Create project folder Step1, And enter
mkdir Step1
cd Step1
Create source code file
Source code file tutorial.cxx, The content of the file is the same as the code for calculating the square root of the official tutorial tutorial.cxx ( stay cmake Source code Step1 The catalog provides )
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;
}
To write CMakeLists.txt file
stay rd-project Create a CMakeLists.txt file , The most basic project only needs one three lines CMakeLists.txt file .
vi CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
# set the project name
project(Tutorial)
# add the executable
add_executable(Tutorial tutorial.cxx)
Be careful , This example is in the CMakeLists.txt Lower case commands are used in the file .CMake Capital letters are supported 、 Lower case and mixed case commands .
Be careful , Only system instructions are case insensitive , But variables and strings are case sensitive
Build and run
2.1、 Create a build directory
cd ..
mkdir Step1_build
2.2、 Generate a local build system
Go to the build directory and run CMake To configure the project and generate a local build system
cd Step1_build
cmake ../Step1
2.3、 Actual compilation / Link items
Build the system locally to actually compile / Link items :
cmake --build .
thus , Build compilation complete .
2.4、 Try running an executable
./Tutorial 4294967296
./Tutorial 10
./Tutorial
The operation results are as follows
Add the version number and configurable header file to the project
The first feature we will add is to provide a version number for our executables and projects . Although we can do this alone in the source code , But use CMakeLists.txt Provides greater flexibility .
3.1、 Use project() Command to set the project name and version number
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 a header file , Pass the version number to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
3.3、 Because the configuration file will be written to the binary tree , We must add this directory to the path list to search for included files .
# 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、 Use your favorite editor , Create in the source directory TutorialConfig.h.in, Contains the following :
// the configured options and settings for [email protected]@ The referenced variables can be accessed through CMakeLists.txt To set up
#define Tutorial_VERSION_MAJOR @[email protected]
#define Tutorial_VERSION_MINOR @[email protected]
When CMake When configuring this header file ,@[email protected] and @[email protected] The value of will be replaced .
tutorial.cxx Contains the configuration header file TutorialConfig.h
to update tutorial.cxx Source code , Print out the name and version number of the executable .cxx The contents are as follows :
// 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;
}
Appoint c++ standard
Next , Let us in tutorial.cxx Lieutenant general atof Replace with std::stod, Add some... To our project c++ 11 characteristic . meanwhile , Delete
#include .
// file tutorial.cxx
// double inputValue = atof(argv[1]);
const double inputValue = std::stod(argv[1]);
We need to be in CMake The code explicitly declares the correct standards it should use .
stay CMake Enable access to specific c++ The simplest way to support standards is to use CMAKE_CXX_STANDARD Variable .
In this tutorial , take cmakellists .txt In the document CMAKE_CXX_STANDARD The variable is set to 11, And will CMAKE_CXX_STANDARD_REQUIRED Set to True.
You have to make sure you're in add_executable Added... Above the call CMAKE_CXX_STANDARD Statement .
Tutorial to this point , complete CMakeLists.txt The contents are as follows :
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}"
)
Rebuild and test the changes
Refer to the above steps 2.2 To 2.4 refactoring , And try to run the executable , Observe changes
边栏推荐
- Teamcenter 消息注册前操作或后操作
- 力扣解法汇总1200-最小绝对差
- 2022年信息系统管理工程师考试大纲
- Please tell me why some tables can find data by writing SQL, but they can't be found in the data map, and the table structure can't be found
- 读libco保存恢复现场汇编代码
- Clickhouse (03) how to install and deploy Clickhouse
- Cartoon: a bloody case caused by a math problem
- Troubleshooting - about clip not found Visual Studio
- Tita 绩效宝:如何为年中考核做准备?
- Thesis reading_ Chinese NLP_ LTP
猜你喜欢
提高應用程序性能的7個DevOps實踐
Thesis reading_ Chinese NLP_ LTP
Zabbix
IDEA 项目启动报错 Shorten the command line via JAR manifest or via a classpath file and rerun.
Unicode processing in response of flash interface
服务器配置 jupyter环境
Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions
Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
7 pratiques devops pour améliorer la performance des applications
「运维有小邓」用于云应用程序的单点登录解决方案
随机推荐
Zabbix
基于YOLOv3的口罩佩戴检测
解读:如何应对物联网目前面临的安全问题?
提高应用程序性能的7个DevOps实践
企业数字化发展中的六个安全陋习,每一个都很危险!
十个顶级自动化和编排工具
Knowledge points of MySQL (7)
Kafaka technology lesson 1
网络威胁分析师应该具备的十种能力
PMP认证需具备哪些条件啊?费用多少啊?
Knowledge points of MySQL (6)
Size_ T is unsigned
「运维有小邓」用于云应用程序的单点登录解决方案
Customize the theme of matrix (I) night mode
Data access - entityframework integration
Elk log analysis system
How awesome is the architecture of "12306"?
Leetcode daily question: the first unique character in the string
Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
Webapp development - Google official tutorial