当前位置:网站首页>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 .
Then we need to choose one kit, It is the selection box below the above figure , In fact, the compiler is chosen :
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_
、_CMAKE
Or 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
# 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 .
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
边栏推荐
- Integer or int? How to select data types for entity classes in ORM
- Mysql:select ... for update
- Pycharm create a new file and add author information
- Using JWT to realize login function
- Information Security Experiment 4: implementation of IP packet monitoring program
- Confitest of fixture py
- stm32和电机开发(从单机版到网络化)
- Interview question: general layout and wiring principles of high-speed PCB
- MySql数据库-事务-学习笔记
- Self awakening from a 30-year-old female programmer
猜你喜欢
Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception
Netease Cloud Wechat applet
esp8266使用TF卡并读写数据(基于arduino)
Jenkins automated email
Pycharm create a new file and add author information
Yapi test plug-in -- cross request
Jenkins+ant+jmeter use
flex弹性布局
Locust performance test 5 (analysis)
MySql数据库-索引-学习笔记
随机推荐
Pick up the premise idea of programming
Some pit avoidance guidelines for using Huawei ECS
Difference between interface iterator and iteratable
Information Security Experiment 2: using x-scanner scanning tool
Netease Cloud Wechat applet
Confitest of fixture py
【云原生】DevOps(一):DevOps介绍及Code工具使用
MySql数据库-事务-学习笔记
2020 year end summary
超十万字_超详细SSM整合实践_手动实现权限管理
Common short chain design methods
嵌套(多级)childrn路由,query参数,命名路由,replace属性,路由的props配置,路由的params参数
Kubernetes cluster capacity expansion to add node nodes
Postman interface debugging method
Variable parameter of variable length function
Systick tick timer
Jenkins+ant+jmeter use
JWT certification used in DRF
Jenkins task grouping
Mysql database transaction learning notes