Use VSCode, Mingw and Cmake to write engineering code, which is written from scratch. The whole process is shared
If you find something to be discussed in this post, please correct and give advice. Thank you in advance!
1 software download
1.1 vscode Download
Official website download address: https://code.visualstudio.com/
When downloading, select 64 bit or 32-bit according to your own machine configuration (most windows machines are 64 bit at present). After downloading to the local, it is shown in the following figure:
1.2 cmake Download
Official website download address: https://cmake.org/download/
1.3 mingw Download
Official website download address: https://osdn.net/projects/mingw/releases/
2 software installation
2.1 VSCode installation
Double click exe file to start installation, modify the installation directory according to your actual situation, and click "next" by default
Open vscode after installation
2.2 adding cmake environment variables
(1) Right click this computer and click properties
(2) Click Advanced system settings
(3) Click Advanced -- > environment variables
(4) Select Path and click Edit
(5) Paste the downloaded and unzipped cmake bin directory path, and then click OK all the way to close the pop-up window
2.3 add mingw environment variable
Find the bin directory of mingw and copy it. Refer to 2.2 to add cmake environment variable
2.4 after all software is installed, restart the computer (I don't know why. Before restarting, cmake configuration fails all the time, and it is solved after restarting)
In the cmd window, click the gcc --version and cmake --version command lines, and the version number will echo indicating the installation
3. Project under construction demo
3.1 create project
According to the following files, create an empty project demo without writing anything
------------------------------------------------------------------------------------------------------------------------------------------------
3.2 open with VSCode
Open as shown in the following figure
Complete c code writing
3.3 CmakeLists.txt encoding
Cmakelists for multi-level directories Txt. The code hierarchy is as follows:
(1) Top cmaklists txt
1 #Specifies the cmake minimum version number 2 cmake_minimum_required(VERSION 3.0) 3 4 #Specify project name 5 project(CmakeProjDemo) 6 7 #Specifies the output path bit bin directory of the executable file 8 set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin) 9 10 #Specify subdirectories 11 add_subdirectory(src) 12 #-----------------------These are the necessities of the top-level directory----------------- 13 14 15 #main.c in#include "Pub.h" & #include "Calculator.h",Need to specify include catalogue 16 include_directories(${CMAKE_SOURCE_DIR}/include) #Without this line, there must be an error. You can't find pub h & Calculator.h 17 18 #Specifies that the source file is compiled into CmakeProjDemo 19 add_executable(CmakeProjDemo main.c) 20 21 #When compiling the executable, main C needs to specify its dynamic link directory 22 target_link_libraries(CmakeProjDemo src) #Put in add_ Why not before executable (cmakeprojdemo main. C)?
(2)./src/CmakLists.txt
1 #The directory is marked as module_src 2 set(module_src src) 3 4 #Assign all source files in the current directory to source_src 5 aux_source_directory(. source_src) 6 7 #appoint src Source file in directory #include "Pub.h" & #include "Calculator.h" 8 include_directories(${CMAKE_SOURCE_DIR}/include) #Without this line, there must be an error. You can't find pub h & Calculator.h 9 10 #Compiling dynamic libraries 11 add_library(${module_src} SHARED ${source_src})
4 compile / run
4.1 configuring cmake
Press F1 in VSCode -- > enter cmake config -- > in the window and click CMake Configure
4.2 compilation
Press F1 in VSCode -- > enter CMake Build -- > in the window and click CMake Build
4.3 run
Menu bar -- > Terminal -- > New Terminal -- > used in terminal/ main.exe run the executable file main exe
Write multi-level directory cmakelists Txt is a good blog recommendation
https://www.cnblogs.com/svenzhang9527/p/10704777.html