当前位置:网站首页>VSCode+mingw64+cmake
VSCode+mingw64+cmake
2022-07-07 09:26:00 【heater404】
cmake brief introduction
cmake install :Download | CMake.
before this , Let's introduce cmake. although Make and Makefile Simplifies the manual build process , But write Makefile Documents are still a troublesome job , So there it is CMake Tools .CMake Tools for building Makefile file , And how to generate Makefile file , By CMakeLists.txt The file specified . Their direct relationship is shown in the figure below :

Use cmake Generate Makefile And the process of compiling is as follows :
- To write CMake The configuration file CMakeLists.txt.
- Carry out orders cmake path Generate CMakefile. among ,path by CMakeLists.txt directory .
- Use make Command to compile .
Concrete cmake Grammar is not introduced here .
Use VSCode Create projects quickly
Use command CMake:Quick Start It creates a CMakeLists.txt file . If you want to create it manually .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-EgqMYw9P-1643097905142)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20220125095622030.png)]](/img/0f/17a857dca2715240176accf0457cd0.jpg)
Then we need to choose one kit, It is the selection box below the above figure , In fact, the compiler is chosen :
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-HgzMd5NB-1643097905143)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20220125095910421.png)]](/img/eb/07743925078649d03b681fa2a17b50.jpg)
Be careful , here VSCode Will automatically find the existing compiler in the computer , But I have also encountered GCC The compiler directory is not present , At this time, you can manually modify the configuration file :C:\Users\DELL\AppData\Local\CMakeTools\cmake-tools-kits.json.
It's done , We can do it build and debug 了 , The shortcut is also in the bottom menu bar . For the first time build Print the log as follows :
[main] Building folder: Demo
[main] The folder containing the CMake cache is missing. The cache will be regenerated.
[main] Configuring folder: Demo
[proc] Executing command: "D:\\Program Files\\CMake\\bin\\cmake.exe" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DCMAKE_C_COMPILER:FILEPATH=D:\msys64\mingw64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=D:\msys64\mingw64\bin\g++.exe -Hd:/02Personal/Project/Demo -Bd:/02Personal/Project/Demo/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 11.2.0
[cmake] -- The CXX compiler identification is GNU 11.2.0
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: D:/msys64/mingw64/bin/gcc.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: D:/msys64/mingw64/bin/g++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] project_source_dir:D:/02Personal/Project/Demo
[cmake] project_binary_dir:D:/02Personal/Project/Demo/build
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: D:/02Personal/Project/Demo/build
[build] Starting build
[proc] Executing command: "D:\\Program Files\\CMake\\bin\\cmake.exe" --build d:/02Personal/Project/Demo/build --config RelWithDebInfo --target all -j 10 --
[build] [ 50%] Building CXX object CMakeFiles/DemoOne.dir/main.cpp.obj
[build] [100%] Linking CXX executable DemoOne.exe
[build] [100%] Built target DemoOne
[build] Build finished with exit code 0
The key is two of them cmake command . But in the whole process , We still have a lot of doubts , Then let's answer them one by one .
CMakeLists.txt What is the content in
The default generated content is as follows :
# Minimum version required
cmake_minimum_required(VERSION 3.0.0)
# Configure project name and version
project(DemoOne VERSION 0.1.0)
# Start the test function
include(CTest)
enable_testing()
# Compile executable ( Executable name Source file )
set(EXECUTABLE_OUTPUT_PATH ${
PROJECT_BINARY_DIR}/bin) # Appoint exe route
add_executable(DemoOne main.cpp)
# Set the packaging function : Package name and version
set(CPACK_PROJECT_NAME ${
PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${
PROJECT_VERSION})
include(CPack)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
PROJECT_BINARY_DIR Is the binary program path by default , Here for build/; This statement will specify that the path of the program to finally generate the executable program is build/bin.
message Output
#print message
message(project_source_dir:${
PROJECT_SOURCE_DIR})
message(project_binary_dir:${
PROJECT_BINARY_DIR})
Set up project after , Two defined variables will be introduced . have access to message Print to see .
Specify the programming language version
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
The variables set here are CMAKE_ start ( Include project Command automatically sets the variables ), These variables are CMake Built in variables for , It is by modifying the values of these variables that you configure CMake Build behavior .
CMAKE_、_CMAKEOr add any after the beginning of the underline CMake The variable names of commands are CMake Reserved .
Configure compile options
Through the command add_compile_options The command configures compilation options for all compilers ( Works for multiple compilers at the same time ); By setting variables CMAKE_C_FLAGS You can configure the c Compiler options ; And set variables CMAKE_CXX_FLAGS Configurable for c++ Compiler options . such as :
add_compile_options(-Wall -Wextra -pedantic -Werror)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pipe -std=c99")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pipe -std=c++11")
Configure compilation type
By setting variables CMAKE_BUILD_TYPE To configure the compilation type , Can be set to :Debug、Release、RelWithDebInfo、MinSizeRel etc. , such as :
set(CMAKE_BUILD_TYPE Debug)
Of course , A better way is to execute cmake Command through parameters -D Appoint :
cmake -B build -DCMAKE_BUILD_TYPE=Debug
If the compilation type is set to Debug, So for c compiler ,CMake It will check whether there are compilation options for this compilation type CMAKE_C_FLAGS_DEBUG, If there is , Add its configuration content to CMAKE_C_FLAGS in .
You can set different compilation options for different compilation types , For example, for Debug edition , Start debugging information , No code optimization :
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
about Release edition , No debugging information , The optimization level is set to 2:
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
It can also be in VScode Set the menu bar at the bottom of the interface .
Multiple source files in the same directory
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-xZDlwGqU-1643097905145)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20220125120700535.png)]](/img/f2/8f15fbf5fbe76f5ef5f157eb096d54.jpg)
# Find all source files in the specified directory , Then save the result in the specified variable name
aux_source_directory(. src_dir)
# Compile executable ( Executable name Source file )
set(EXECUTABLE_OUTPUT_PATH ${
PROJECT_BINARY_DIR}/bin) # Appoint exe route
add_executable(${
PROJECT_NAME} ${
src_dir})
Multiple directories and multiple source files
Subdirectories are compiled into linked libraries
In this case , We need to write one in each directory CMakeLists.txt file , Then compile them into static link libraries , Then add the link library to the executable .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-kUxWqBhX-1643097905145)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20220125144817725.png)]](/img/a5/f043b273e8acff4528e9a0d94d619b.jpg)
about math In folder CMakeLists.txt:
# Find all source files in the specified directory , Then save the result in the specified variable name
aux_source_directory(. src_math)
# Compile static link library ( Static link library name Source file )
SET(LIBRARY_OUTPUT_PATH ${
PROJECT_BINARY_DIR}/lib)# Specify the path to the library
add_library(math ${
src_math})
Then there is the executable CMakeLists.txt:
# Add subdirectories
add_subdirectory(math)
# Find all source files in the specified directory , Then save the result in the specified variable name
aux_source_directory(. src_main)
# Compile executable ( Executable name Source file )
set(EXECUTABLE_OUTPUT_PATH ${
PROJECT_BINARY_DIR}/bin) # Appoint exe route
add_executable(${
PROJECT_NAME} ${
src_main})
# Add link library math Is a library compiled in a subdirectory
target_link_libraries(${
PROJECT_NAME} math)
use LIBRARY_OUTPUT_PATH Specify the generation path of the final static library or dynamic library , So here's the final libmath.a The static library will be build/lib Generate under directory
ADD_EXECUTABLE() Used to build executable programs , Here we use ADD_LIBRARY() To build static libraries or dynamic libraries
ADD_LIBRARY(libname [SHARED|STATIC|MODULE] [EXCLUDE_FROM_ALL]
- libname Represents the name of the static library or dynamic library to be generated , Don't put your name in front of it lib The system will automatically add . For example, to generate libmath Library name , Write math Just fine
- [SHARED|STATIC|MODULE] Respectively, it means to build a dynamic library ( commonly .so ending ,mac os System is .dyld)、(.a) Static library 、 Dynamic library (mac os System is .so, If not As SHARED To view ); The default parameter is STATIC
- EXCLUDE_FROM_ALL This means that this library will not be built by default , Unless there are other component dependencies or manual construction .
Of course , You can also generate both dynamic and static libraries .
# Find all source files in the specified directory , Then save the result in the specified variable name
aux_source_directory(. src_math)
# Compile executable ( Executable name Source file )
SET(LIBRARY_OUTPUT_PATH ${
PROJECT_BINARY_DIR}/lib)# Specify the path to the library
add_library(math SHARED ${
src_math})# Dynamic library
# Add a version number to the dynamic library
SET_TARGET_PROPERTIES(math PROPERTIES VERSION 1.2 SOVERSION 1) #VERSION Refers to the dynamic library version ,SOVERSION Refer to API edition .
add_library(static_math STATIC ${
src_math})# Static library
# Change the name of the finally generated static library
SET_TARGET_PROPERTIES(static_math PROPERTIES OUTPUT_NAME math)
# Get the value of the specified attribute of the specified build target ; Here for math_static Properties of OUTPUT_NAME The value assigned to OUTPUT_VALUE Variable
GET_TARGET_PROPERTY(OUTPUT_VALUE static_math OUTPUT_NAME)
Add subdirectory source files to the compilation list
The above method is to compile the subdirectory into a library , Then there will be more library files in the compilation result . But in fact , We can add all subdirectories to the compilation list , In this way, subdirectories do not need CMakeLists.txt The file .
# Minimum version required
cmake_minimum_required(VERSION 3.0.0)
# Configure project name and version
project(DemoOne VERSION 0.1.0)
#print message
message(STATUS "root This is BINARY dir " ${
PROJECT_BINARY_DIR})
message(STATUS "root This is SOURCE dir " ${
PROJECT_SOURCE_DIR})
# Start the test function
include(CTest)
enable_testing()
# Find all source files in the specified directory , Then save the result in the specified variable name
aux_source_directory(. src_main)
aux_source_directory(./math src_main)
# Compile executable ( Executable name Source file )
set(EXECUTABLE_OUTPUT_PATH ${
PROJECT_BINARY_DIR}/bin) # Appoint exe route
add_executable(${
PROJECT_NAME} ${
src_main})
# Set the packaging function : Package name and version
set(CPACK_PROJECT_NAME ${
PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${
PROJECT_VERSION})
include(CPack)
With people think , If there is no need to compile into a library , It is better to use the second method .
Reference external static libraries and dynamic libraries
边栏推荐
- PMP Exam Preparation experience systematically improve project management knowledge through learning
- Data association between two interfaces of postman
- Skill review of test engineer before interview
- DRF authentication, permissions, and flow restrictions (only for views in DRF)
- 5A summary: seven stages of PMP learning
- Where is the answer? action config/Interceptor/class/servlet
- Two schemes of unit test
- stm32和电机开发(从单机版到网络化)
- Windows starts redis service
- Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
猜你喜欢

JVM 内存结构 详细学习笔记(一)

STM32 and motor development (from stand-alone version to Networking)

MySQL common statements

Integer or int? How to select data types for entity classes in ORM

Variable parameter of variable length function
![Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]](/img/33/9fde4bce4866b988dd2393a665a48c.jpg)
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]

MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查

Using JWT to realize login function

Locust performance test 5 (analysis)

4、 Fundamentals of machine learning
随机推荐
How can I apply for a PMP certificate?
Huawei hcip datacom core_ 03day
正则匹配以XXX开头的,XXX结束的
Register address name mapping
Interview question: general layout and wiring principles of high-speed PCB
What is the value of getting a PMP certificate?
sqlplus乱码问题,求解答
Record of structured interview
Install pyqt5 and Matplotlib module
Information Security Experiment 3: the use of PGP email encryption software
Final keyword
C language pointer (special article)
PMP Exam details after the release of the new exam outline
What is MD5
Unittest simple project
Jenkins modifies the system time
Connecting mobile phone with ADB
Kubernetes cluster capacity expansion to add node nodes
Leetcode question brushing record (array) combination sum, combination sum II
How does the project manager write the weekly summary and weekly plan?