1、下载编译器MinGW,https://osdn.net/projects/mingw/。下载完成安装;
2、下载Cmake编译,https://cmake.org/download/ ,下载完成安装;
3、Vscode EXTENSIONS下载CMake和CMake Tools,C/C++;
4、测试程序,新建文件夹,新建文件写一个测试程序
5、CMakeLists编写;
cmake_minimum_required(VERSION 3.0.0)
project(test)
SET(CMAKE_BUILD_TYPE “Debug”) #设置debug版本编译
include_directories(./)
aux_source_directory(./ DIR_SRC)
#aux_source_directory(./ DIR_DEMO)
#add_subdirectory(./src)
add_executable(main ${DIR_SRC} )
6、编译器配置:Ctrl+Shift+p:输入C/Cpp:Edit Configurations生成配置文件 c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/programfile/mingw/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64",
"configurationProvider": "vector-of-bool.cmake-tools"
}
],
"version": 4
}
7、仿真配置:Ctrl+Shift+p:输入Open launch.json生成配置文件launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/test/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:/programfile/mingw/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
8、Ctrl+Shift+p:输入Tasks: Configure task生成配置文件 tasks.json
{
"version": "2.0.0",
"command": "g++",
"args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"], // 编译命令参数
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
9、Ctrl+Shift+p:打开配置文件 settings.json
添加:“cmake.cmakePath”: “D:/programfile/cmake-3.20.2/bin/cmake.exe”
10、编译如果出现make时错误,“make : 无法将“make”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。”
解决方法:将安装的minGW/bin文件下,找到mingw32-make.exe文件,重命名为make.exe即可。
11、Vscode下打开终端,进入工程,先使用Cmake编译生成Makefile,在make编译生成可执行文件。
12、仿真调试,或者在终端上直接运行