当前位置:网站首页>Cmake series tutorial 2 HelloWorld
Cmake series tutorial 2 HelloWorld
2022-07-24 19:21:00 【senseshield】
This article takes Windows System Visual Studio Based on the development environment , demonstration CMake structure HelloWorld course .
HelloWorld.cpp
New folder HelloWorld, newly build C++ Source code file HelloWorld.cpp, Write the code as follows :HelloWorld.cpp
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello World!\n");
return 0;
}CMakeLists.txt
With the HelloWorld.cpp Create a new folder CMake Script files CMakeLists.txt, Write the code as follows :CMakeLists.txt
# project name
project(HelloWorld)
# Output message at build time
message(STATUS "This is HelloWorld project.")
# Declare variables , Contains the source code files that need to be included in the current project
set(SRC_FILES HelloWorld.cpp)
# Build executable programs , You need to specify the source file of the executable program
add_executable(HelloWorld ${SRC_FILES})CMakeLists.txt File is CMake Project organization script , There can only be one file per folder , The file name cannot be changed ,CMake When building, it will find the... In the specified directory by default CMakeLists.txt File build .
Command specification
project( Optional )
Specify the current CMake Project name , This name is not Visual Studio The name of the project , This name does not affect the source code project name . If you use Visual Studio structure ,project The name is the solution (*.sln) name .
This parameter can be operated in some very convenient ways in complex projects , Later articles will explain in detail .
If you're just building an ordinary simple project , This sentence can be omitted .
message( Optional )
message seeing the name of a thing one thinks of its function , It is the output message used by the project at ordinary times , stay CMake When building the project, output the message content specified by the parameter .
CMake Didn't like IDE Same debugging method , When building a complex project, you need to judge whether the current construction project meets the expected parameter settings by outputting messages , Or output debugging information , Check that the configuration is correct .
The function prototype
message([<mode>] "message to display" ...)Common parameters
- FATAL_ERROR Fatal error , Terminate build . When this parameter is specified, the message is output , Description encountered an unhandled error , Stop building , Tell the developer where the error occurred .
- WARNING Warning . Output warning message , Continue building .
- STATUS state . One of the common types , Output status information .
- (none) or NOTICE news . One of the common types , The output message .
set( Optional )
Declare variables , Replace a set of values with a variable .
The function prototype
set(<variable> <value>... [PARENT_SCOPE]) Usage method
CMake Use by convention ${} How to reference variables , Such as declaring variables in code SRC_FILES Record the source file of the current project , Use when building executables ${SRC_FILES} How to reference variables .
have access to message(STATUS “${SRC_FILES}”) View the content defined in the variable .
add_executable( Mandatory )
Add an executable program according to the specified source code ,CMake The source code project will be built according to the current instructions , Parameters 1 <name> It's the name of the project , Is the key parameter of project construction , In complex projects, you need to modify the compilation options , Specify the link library , Set build events through <name> Association .
The function prototype
add_executable(<name> [WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [source1] [source2 ...]) As noted above Optional The order is to make CMake The script is readable 、 Extensibility 、 Maintainability , But it's not necessary .
If in a simple example code , You can put the above CMakeLists.txt Script reduction add_executable(HelloWorld HelloWorld.cpp) , Can achieve the same effect , But this method is not recommended .
Official documents
This article only introduces the basic meaning and common methods of commands from a practical point of view , If you want to fully understand other uses of commands , Please see the CMake Official documents of .
- Local documents .CMake The installation directory \doc\cmake\html\index.html
- Online document .CMake Document center (https://cmake.org/cmake/help/v3.15/)
structure (Windows)
structure Visual Studio engineering
Create a folder for storing project files build, The current file structure is as follows :
│ CMakeLists.txt
│ HelloWorld.cpp
│
└─build
Use the command line prompt cmd Switch to build Catalog , perform cmake .., The output build information is as follows :
D:\cmake\HelloWorld\build>cmake ..
-- Building for: Visual Studio 14 2015
-- Selecting Windows SDK version to target Windows 10.0.17134.
-- The C compiler identification is MSVC 19.0.24210.0
-- The CXX compiler identification is MSVC 19.0.24210.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- This is HelloWorld project.
-- Configuring done
-- Generating done
-- Build files have been written to: D:/cmake/HelloWorld/build
From the output information, you can see the following information :
- Compiler Version :
Building for: Visual Studio 14 2015, When no compiler version is specified ,CMake Automatically select an available compiler from the current environment to build ,Windows Development environment deploymentVisual StudioThe default choice is , When there are multiple versions ofVisual StudioThe latest version is selected by default . remarks : Because this article builds the environment installationVisual Studio 2015, The actual build is based on the current development environmentVisual StudioThe version shall prevail . messageCommand output messageThis is HelloWorld project.- Storage directory of engineering documents :
D:/cmake/HelloWorld/build
In build command .. Indicates that the path of the specified source code is the upper level directory , It's also CMakeLists.txt File directory .
After building , stay build Directory generation Visual Studio 2015 project ,

Specify parameters to build
Specify the build directory
When you need to specify the source code or build the build directory , have access to -S Specify the source directory ,-B Specify the project build directory .
CMake Examples of command line tool parameters :cmake [options] -S <path-to-source> -B <path-to-build>
Specifies the compiler build
When a build environment deploys multiple development environments ,cmake Components can be through -G"generator-name" Specify compiler .
for example :
- structure NMake engineering
cmake -G"NMake Makefiles" .. - structure VS2012 engineering ,
cmake -G"Visual Studio 11 2012" ..
This article only lists two common construction parameters ,CMake Support build parameters. For details, use the command line prompt (CMD) Input cmake --help View details .
compile
This article USES the Visual Studio development environment , Therefore, using VS open HelloWorld.sln, Compile the source code to generate executable files .

Run the compiled program , Execution results :
Hello World!
Please press any key to continue . . .
边栏推荐
- JVM method call
- Mysql database, subquery, union, limit
- Introduction to VIM
- Nacos introduction and console service installation
- Common problems of multithreading and concurrent programming (to be continued)
- PCI express physical layer - electrical part
- Nezha monitoring - server status monitoring, SSL certificate change expiration, Ping monitoring and scheduled task reminder
- Meshlab&PCL ISS关键点
- OPENGL学习(二)OPENGL渲染管线
- FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(上)
猜你喜欢

【JVM学习03】类加载与字节码技术

OpenGL learning (III) glut two-dimensional image rendering

Common problems of multithreading and concurrent programming (to be continued)

High speed ASIC packaging trends: integration, SKU and 25g+

文献阅读:GoPose 3D Human Pose Estimation Using WiFi

Why is gradient the fastest changing direction of function

Hucang integrated release of full data value, sequoiadb V5.2 online conference heavy attack

Nacos简介和控制台服务安装

OPENGL学习(三)GLUT二维图像绘制

Decision tree_ ID3_ C4.5_ CART
随机推荐
Emergency lighting design of large stadiums and gymnasiums
Implement a proxy pool from 0
Cadence OrCAD capture tcl/tk script example
第4章 复合类型
Solve the problem of disconnection due to long idle time after SSH login
Literature reading: gopose 3D human pose estimation using WiFi
Tcl/tk file operation
Nezha monitoring - server status monitoring, SSL certificate change expiration, Ping monitoring and scheduled task reminder
What are the benefits of knowledge management in enterprises?
Machine learning_ Data processing and model evaluation
拿捏C指针
2022杭电多校第二场1009 ShuanQ(数学)
Cyberpanel free open source panel - high speed lscache free SSL Certificate - self built DNS and enterprise post office
OPENGL学习(五)Modern OpenGL 三角形绘制
Colon sorting code implementation
Taokeeper environment setup
MySQL final chapter
Unity框架之ConfigManager【Json配置文件读写】
MySQL8.0学习记录20 - Trigger
Arrays