下载 MinG-W64,选最新版本中的 x86_64-posix-seh,然后解压,设置环境变量。
https://sourceforge.net/projects/mingw-w64/files/
下载 VsCode 相关插件
只有 c/c++ 是必须的。
在工作区创建 .vscode 文件夹,用 VsCode 创建,系统自带文件管理无法创建以点开头的文件夹,然后创建下面两个配置文件
修改配置中的路径为自己的 Ming-W64 路径
launch.json 用于调试运行
https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
{ "version": "0.2.0", "configurations": [ { "name": "gdb debug", // 配置名称,将会在启动配置的下拉菜单中显示 "type": "cppdbg", // 配置类型,这里只能为 cppdbg "request": "launch", // 请求配置类型,可以为 launch(启动)或 attach(附加) "program": "${workspaceFolder}/.target/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径 "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可 "stopAtEntry": false, // 设为 true 时程序将暂停在程序入口处,一般设置为 false "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为 ${workspaceFolder} 即代码所在目录 "environment": [], "externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台 "MIMode": "gdb", "miDebuggerPath": "D:/PcAPP/mingw64/bin/gdb.exe", // miDebugger 的路径,注意这里要与 MinGw 的路径对应 "preLaunchTask": "gcc", // 调试会话开始前执行的任务,一般为编译程序,c++ 为 g++, c 为 gcc "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
tasks.json 用于调试运行前的编译
https://code.visualstudio.com/docs/editor/tasks#_custom-tasks
{ "version": "2.0.0", "command": "gcc", "args": [ "-g", "${file}", "-o", ".target/${fileBasenameNoExtension}.exe" ], // 编译命令参数 "problemMatcher": { "owner": "cpp", "fileLocation": [ "relative", "${workspaceFolder}" ], "pattern": { "regexp": "^(.*):(\d+):(\d+):\s+(warning|error):\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } }, "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "new", // 这里 shared 表示共享,改成 new 之后每个进程创建新的端口 "showReuseMessage": true, "clear": false } }
测试
#include <math.h> #include <stdio.h> void main() { double a = pow(2.0, 3.0); printf("res = %f.2 ", a); printf("hello "); double b = 1.234; int c = b; printf("%d", c); }
按 F5 即可调试运行
想直接运行,也可以安装 code-runner 插件
附上 VsCode 全部设置 settings.json
{ "editor.minimap.enabled": false, "editor.mouseWheelZoom": true, "files.autoSave": "afterDelay", "terminal.integrated.shell.windows": "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe", "liveServer.settings.donotShowInfoMsg": true, "workbench.startupEditor": "newUntitledFile", "code-runner.ignoreSelection": true, "code-runner.runInTerminal": true, "editor.fontSize": 18, "liveServer.settings.multiRootWorkspaceName": "", "git.path": "D:/PcAPP/PortableGit/bin/git.exe", "git.enabled": false, "[c]": { "editor.defaultFormatter": "ms-vscode.cpptools" }, "C_Cpp.clang_format_fallbackStyle": "LLVM", "C_Cpp.intelliSenseEngine": "Tag Parser", "workbench.colorTheme": "IDEA like light Theme", "code-runner.executorMap": { // windows 默认为 gbk 编码,这里让 VsCode 中默认 shell 为 PowerShell,编译时指定 gbk 编码,避免 code-runner 插件运行 c/c++ 输出中文时乱码 "c": "cd $dir && gcc -fexec-charset=gbk $fileName -o ..\.target\$fileNameWithoutExt.exe && ..\.target\$fileNameWithoutExt.exe", "cpp": "cd $dir && g++ -fexec-charset=gbk $fileName -o ..\.target\$fileNameWithoutExt.exe && ..\.target\$fileNameWithoutExt.exe" }, "workbench.iconTheme": "vsclassic-icon-theme", "update.showReleaseNotes": false, "telemetry.enableTelemetry": false, }
https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools