vs code 搭建c/c++开发环境(mingw64)

安装vs 插件

安装vs code ,安装c/c++插件

自行百度

安装MSYS32

https://www.msys2.org/

直接一路下一步

安装编译器

在mysys提示符下面执行

pacman -S --needed base-devel mingw-w64-x86_64-toolchain

安装好了,在电脑里面找到g++,gcc, dgb的目录,把这个目录加到windows的环境变量PATH里面去


编写代码

Vscode 应该是会自动扫描PATH路径,添加了g++之后,就可以直接编写代码运行了。

常见问题

如何编译多个文件

正常情况下应该是写makefile 等等。目前用vscode 也就是写点小函数,测试代码。直接可以用传递参数的形式编译多个c/cpp文件

这里比如有多个cpp,h文件,可以修改task.json文件的参数

"tasks": [

{

"type": "cppbuild",

"label": "C/C++: g++.exe build active file",

"command": "C:\\msys64\\mingw64\\bin\\g++.exe",

"args": [

"-fdiagnostics-color=always",

"-g",

// "${file}",

"${fileDirname}\\**.cpp",

"-o",

"${fileDirname}\\${fileBasenameNoExtension}.exe"

],

默认是传递当前文件名,这里选择当前文件所在目录的所有cpp文件,如果是c的话,就改成.c

附件

以下是Lunch和task文件,如果这些全部手写,说明环境变量肯定没配对,导致vscode 识别出了问题。否则的话,默认基本都是自动生成的。

Lunch.json文件

{

"configurations": [

{

"name": "C/C++: g++.exe build and debug active file",

"type": "cppdbg",

"request": "launch",

"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",

"args": [],

"stopAtEntry": false,

"cwd": "${fileDirname}",

"environment": [],

"externalConsole": false,

"MIMode": "gdb",

"miDebuggerPath": "C:\\msys64\\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": "C/C++: g++.exe build active file"

}

],

"version": "2.0.0"

}

Tasks.json文件

{

"tasks": [

{

"type": "cppbuild",

"label": "C/C++: g++.exe build active file",

"command": "C:\\msys64\\mingw64\\bin\\g++.exe",

"args": [

"-fdiagnostics-color=always",

"-g",

// "${file}",

"${fileDirname}\\**.cpp",

"-o",

"${fileDirname}\\${fileBasenameNoExtension}.exe"

],

"options": {

"cwd": "${fileDirname}"

},

"problemMatcher": [

"$gcc"

],

"group": {

"kind": "build",

"isDefault": true

},

"detail": "Task generated by Debugger."

}

],

"version": "2.0.0"

}

原文链接:,转发请注明来源!