当前位置:网站首页>Cmake learning
Cmake learning
2022-07-23 21:18:00 【_ Soren】
List of articles
CMake brief introduction
Easy to use , Can cross platform , Build the project compilation environment . Especially better than writing directly Makefile Simple ( When building a large project, compile , Need to write a lot of File Dependencies ), It can be done by simple CMake Generate responsible Makefile file .
CMake install
ubuntu On the direct execution of sudo apt install cmake installation is complete , Can pass cmake -version View its version :

CMake Brief introduction
cmake_minimum_required (VERSION 3.0) # requirement cmake Minimum version number
project (demo) # Define the name of the current project
set(CMAKE_BUILD_TYPE "Debug") # Set up debug Pattern , Without this line, you cannot debug and set breakpoints
set(CMAKE_CXX_FLAGS ${
CMAKE_CXX_FLAGS} -g)
add_executable(main main.c)
# Enter the subdirectory to execute CMakeLists.txt file there lib and tests There are compiled code files
add_subdirectory(lib)
add_subdirectory(tests)
If there are multiple source files to compile , Can be added to add_executable(main main.cpp test.cpp) In the list , But if there are too many source files , Add one by one to add_executable In the source file list of , It's too much trouble , You can use
aux_source_directory(dir var) To define the source file list , Use as follows :
cmake_minimum_required (VERSION 3.0)
project (demo)
aux_source_directory(. SRC_LIST) # Defining variables , Store all source files in the current directory
add_executable(main ${
SRC_LIST})
aux_source_directory() There are also disadvantages , It will add all source files in the specified directory , We may add some files we don't need , Now we can use it set Command to create a new variable to store the required source file , as follows :
cmake_minimum_required (VERSION 3.0)
project (demo)
set( SRC_LIST
./main.cpp
./test.cpp)
add_executable(main ${
SRC_LIST})
Examples of use
muduo The server of the network library , Commands when compiling links :

stay CMakeLists.txt Writing on :

cmake_minimum_required(VERSION 3.0)
# The name of the project , Not an executable name
project(main)
# Configure compile options
set(CMAKE_CXX_FLAGS ${
CMAKE_CXX_FLAGS} -g)
# Configure header file search path
# include_directories()
# Configuration library file search path
# link_directories()
# Set the list of source files to compile
set(SRC_LIST ./muduo_server.cpp)
# Set the final storage path of the executable
set(EXECUTABLE_OUTPUT_PATH ${
PROJECT_SOURCE_DIR}/bin)
# Put all the source file names under the specified path into the variable name SRC_LIST Inside
# aux_source_directory(. SRC_LIST)
# The resulting file , By whom
add_executable(server ${
SRC_LIST})
# Express server This object program , Connection required muduo_net muduo_base pthread These three library files
target_link_libraries(server muduo_net muduo_base pthread)
stay build Execute under folder cmake …

Re execution make
CMakeLists.txt The final generated executable file written in bin Under the table of contents , So go to bin Inside .
Run the example :

Why CMake
Someone will ask. , I have only one cpp Source file , Use CMake After that, so many files were generated , That looks troublesome :
reason : In the actual development process , A project roughly includes the following modules :
The final generated executable 、 Intermediate library file 、 The header file 、 Source file 、CMake Generated files 、 The test file 、 Third party library files 、CMake file , Automatic compilation of script files , So many files , If you write makefile It will be more troublesome , While using CMake It's very convenient .

CMake Common predefined variables
PROJECT_NAME : adopt project() Specify project name
PROJECT_SOURCE_DIR : The root directory of the project
PROJECT_BINARY_DIR : perform cmake Directory of commands
CMAKE_CURRENT_SOURCE_DIR : At present CMakeList.txt Directory of files
CMAKE_CURRENT_BINARY_DIR : Compile Directory , You can use add subdirectory To modify the
EXECUTABLE_OUTPUT_PATH : Binary executable output location
LIBRARY_OUTPUT_PATH : Library file output location
BUILD_SHARED_LIBS : The default library compilation method ( shared or static ) , The default is static
CMAKE_C_FLAGS : Set up C Compilation options
CMAKE_CXX_FLAGS : Set up C++ Compilation options
CMAKE_CXX_FLAGS_DEBUG : Set compilation type Debug Compile options for
CMAKE_CXX_FLAGS_RELEASE : Set compilation type Release Compile options for
CMAKE_GENERATOR : Compiler name
CMAKE_COMMAND : CMake The full path of the executable itself
CMAKE_BUILD_TYPE : Version generated by project compilation , Debug / Release
边栏推荐
- Tell me the top ten securities companies? Is it safe to open an account online?
- One of QT desktop whiteboard tools (to solve the problem of unsmooth curve -- Bezier curve)
- 寻找消失的类名
- 高数下|二重积分的计算2|高数叔|手写笔记
- 1309_STM32F103上增加GPIO的翻转并用FreeRTOS调度测试
- Oom mechanism
- 分布式能源的不确定性——风速测试(Matlab代码实现)
- Chapter1 数据清洗
- 1062 Talent and Virtue
- Synchronized同步锁的基本原理
猜你喜欢
随机推荐
Unity解决动画不可用:The AnimationClip ‘XXX‘ used by the Animation component ‘XXX‘ must be marked as Legacy.
How to get the worker's hat? Where is the worker's helmet?
LeetCode_376_摆动序列
Boost Filesystem使用手册
【arxiv】第一次上传论文小记
确定括号序列中的一些位置
[cloud co creation] what magical features have you encountered when writing SQL every day?
High numbers | calculation of double integral 4 | high numbers | handwritten notes
深入浅出边缘云 | 1. 概述
Edge cloud | 1. overview
Jetson nano烧录踩坑记(一定可以解决你的问题)
h264编码参数
支付宝常用接口统一封装,可直接支付参数使用(适用于H5、PC、APP)
(note) learning rate setting of optimizer Adam
[shader realizes roundwave circular ripple effect _shader effect Chapter 6]
Scala programming (elementary)
OOM机制
Synchro esp32c3 Hardware Configuration Information serial port Print Output
Protocol buffers 的问题和滥用
First acquaintance with JS (programming suitable for beginners)









