当前位置:网站首页>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 . . .
边栏推荐
- About core files
- Leetcode652 finding duplicate subtrees
- Literature reading: gopose 3D human pose estimation using WiFi
- pyhanlp安装教程
- On dynamic application of binary array
- 2022 Summer Games of Hangzhou electric power multiple schools 1012aalice and Bob (game theory)
- FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(上)
- Jedi survive and eat chicken F12 screenshot save path reference
- Equals() method of object class
- TCL programming style guide
猜你喜欢

Sequences, time series and prediction in tessorflow quizs on coursera (I)

MySQL1

FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 1)

Day 9 (this keyword and experiment)

LSTM and Gru of RNN_ Attention mechanism

【校招面经】8道指针面试真题,快来检测自己掌握了几道。

多线程与并发编程常见问题(未完待续)
![[question 39] special question for Niuke in-depth learning](/img/18/0e182f2c003ff5dd8ed3751c718d73.png)
[question 39] special question for Niuke in-depth learning
![[JVM learning 03] class loading and bytecode Technology](/img/b4/9482b02b58580171235fd4c36129e9.png)
[JVM learning 03] class loading and bytecode Technology

Machine learning_ Softmax function (multi classification problem)
随机推荐
Ensure the health and safety of front-line construction personnel, and implement wrong time construction at Shenzhen construction site
Cesium uses czml to implement dynamic routes
[understanding of opportunity-48]: Guiguzi - Chapter 7 - collect information in advance, make predictions and implementation plans in advance
知乎上的那些神回复
Taokeeper environment setup
[understanding of opportunity-49]: three seasons and cognitive dimension
Get module handle / base address
拦截器和过滤器
OpenGL learning (V) modern OpenGL triangle rendering
Detailed explanation of the relationship between MySQL tables
Principle and application of database
【JVM学习03】类加载与字节码技术
Analysis of the basic concept of digital warehouse
Leetcode402 remove K digits
[JVM learning 04] JMM memory model
湖仓一体释放全量数据价值,SequoiaDB v5.2线上发布会重磅来袭
PWN learning
2022杭电多校第一场Dragon slayer(dfs+状态压缩)
MySQL sort. Sort by field value
Installation and use of lsky Pro lancong drawing bed: a drawing bed program for online uploading and managing pictures