一、开发环境搭建
·安装mingw-w64编译器(GCC for Windows 64 & 32 bits)、Cmake工具(选装)
·VSCode插件安装
·C/C++
·cmake
·cmake tools
二、代码实践演练
·基于g++命令
·g++编译单文件,生成带调试信息的可执行文件、并调试
g++ -g main.cpp -o my_single_swap
·g++编译多文件,生成带调试信息的可执行文件、并调试
g++ -g main.cpp swap.cpp -o my_multi_swap
默认的tasks.json是编译单个文件的,不会去自动识别多个文件,我们必须手动去配置launch.json和tasks.json这两个文件。
·基于cmake
·编写最简单的CMakeLists.txt
project(MYSWAP)
add_ executable(my_cmake_swap,main.cpp swap.cpp)
·进行多文件编译,并调试
mkdir build
cd build
#如果电脑上已安装了VS,可能会调用微软MSVC编译器,使用(cmake -G "MinGW Makefiles" ..) 代替(cmake ..)即可
#仅第一次使用cmake时使用(cmake -G "MinGw Makefiles" ..) 后面可使用(cmake ..)
cmake ..
mingw32 -make. exe
·配置json
·launch.json --- for debug
{
"configurations": [
{
"name": "C/C++: g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/build/my_cmake_swap.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "E:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "Build"
}
],
"version": "2.0.0"
}
·tasks.json --- for build before debug
{
"version": "2.0.0",
"options": {
"cwd": "${fileDirname}/build"
},
"tasks": [
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"presentation": {
"panel": "new"
},
"args": [
".."
],
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"command": "mingw32-make.exe",
"args": [
],
},
{
"label": "Build",
"dependsOn":[
"cmake",
"make"
]
},
],
}
如若遇到以下问题:
在setting.json中加入上述框框中的代码即可。
{
"terminal.integrated.defaultProfile.windows": "Command Prompt",
"files.autoSave": "onFocusChange",
"workbench.iconTheme": "material-icon-theme",
"workbench.colorTheme": "Material Theme Ocean",
"workbench.editorAssociations": {
"*.exe": "default"
},
"cmake.configureOnOpen": true,
"cmake.options.advanced": {
"build": {
"statusBarVisibility": "inherit",
"inheritDefault": "visible"
},
"launch": {
"statusBarVisibility": "inherit",
"inheritDefault": "visible"
},
"debug": {
"statusBarVisibility": "inherit",
"inheritDefault": "visible"
}
},
"cmake.options.statusBarVisibility": "visible",
"cmake.showOptionsMovedNotification": false
}