Windows 下 vscode 上配置 MinGW-w64

一、环境

1、mingw64

由于在线安装版速度很慢,此处选择离线版安装

下载地址:https://sourceforge.net/projects/mingw-w64/files/

 

 具体版本的选择参考下图(引用自https://blog.csdn.net/jiqiren_dasheng/article/details/103775488)

 

 选择下载

 

这个版本,如果下载慢,可以在网页上选择不同的下载镜像 

下载后解压到某个目录。然后在环境变量中的PATH中增加解压后的bin目录即可

2、vscode

下载vscode,解压运行后,在应用市场中安装如下插件:

 

 

 

 

 

 

 插件的使用参看:https://zhuanlan.zhihu.com/p/92175757

 重启后生效

二、配置

首先在vscode中打开一个文件夹作为工作目录。

在工作目录下先建一个测试文件:

1 #include <stdio.h>
2 int main()
3 {
4     printf("你好,世界\n");
5     return 0;
6 }

 然后快捷键执行ctrl+shift+p,输入“c/c++”打开环境变量配置窗口:

 

 默认打开的内容如下:

 1 {
 2     "configurations": [
 3         {
 4             "name": "Win32",
 5             "includePath": [
 6                 "${workspaceFolder}/**"
 7             ],
 8             "defines": [
 9                 "_DEBUG",
10                 "UNICODE",
11                 "_UNICODE"
12             ],
13             "compilerPath": "D:\\green\\mingw64\\bin\\gcc.exe",
14             "cStandard": "gnu17",
15             "cppStandard": "gnu++14",
16             "intelliSenseMode": "windows-gcc-x64"
17         }
18     ],
19     "version": 4
20 }

13行是gcc的安装目录。保存后会在当前文件夹下生成一个目录,名称为.vscode,里面保存当前工作空间的配置信息。

下面生成launch.json:

 

 

继续选择:

 

 

 

 在弹出框中选择:

 

 会自动添加如下内容:

 1 {
 2     // 使用 IntelliSense 了解相关属性。 
 3     // 悬停以查看现有属性的描述。
 4     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
 5     "version": "0.2.0",
 6     "configurations": [
 7         {
 8             "name": "(gdb) 启动",
 9             "type": "cppdbg",
10             "request": "launch",
11             "program": "输入程序名称,例如 ${workspaceFolder}/a.exe",
12             "args": [],
13             "stopAtEntry": false,
14             "cwd": "${fileDirname}",
15             "environment": [],
16             "externalConsole": false,
17             "MIMode": "gdb",
18             "miDebuggerPath": "/path/to/gdb",
19             "setupCommands": [
20                 {
21                     "description": "为 gdb 启用整齐打印",
22                     "text": "-enable-pretty-printing",
23                     "ignoreFailures": true
24                 }
25             ]
26         }
27 
28 
29     ]
30 }

 

将以上内容修改如下:

 1 {
 2     "version": "0.2.0",
 3     "configurations": [
 4 
 5         {
 6             "name": "(gdb) Launch",    // 配置名称,将会在启动配置的下拉菜单中显示
 7             "type": "cppdbg",         // 配置类型,这里只能为cppdbg
 8             "request": "launch",    // 请求配置类型,可以为launch(启动)或attach(附加)
 9             "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径
10             "args": [],                // 程序调试时传递给程序的命令行参数,一般设为空即可
11             "stopAtEntry": false,     // 设为true时程序将暂停在程序入口处,一般设置为false
12             "cwd": "${workspaceRoot}",// 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
13             "environment": [],
14             "externalConsole": true,// 调试时是否显示控制台窗口,一般设置为true显示控制台
15             "MIMode": "gdb",
16             "miDebuggerPath": "D:\\green\\mingw64\\bin\\gdb64.exe",// miDebugger的路径,注意这里要与MinGw的路径对应
17             "preLaunchTask": "gcc",    // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
18             "setupCommands": [
19                 {
20                     "description": "Enable pretty-printing for gdb",
21                     "text": "-enable-pretty-printing",
22                     "ignoreFailures": true
23                 }
24             ]
25         }
26     ]
27 }

 

miDebuggerPath 这一条,要与你mingw64安装路径一致,注意在路径中 '\'要替换为'\\'

 保存launch.json后,尝试执行程序:

 

 会出现如下错误:

 

 说明需要建立task.json文件

 

 

 

 

 生成的内容如下:

 1 {
 2     // See https://go.microsoft.com/fwlink/?LinkId=733558
 3     // for the documentation about the tasks.json format
 4     "version": "2.0.0",
 5     "tasks": [
 6         {
 7             "label": "build",
 8             "type": "shell",
 9             "command": "msbuild",
10             "args": [
11                 // Ask msbuild to generate full paths for file names.
12                 "/property:GenerateFullPaths=true",
13                 "/t:build",
14                 // Do not generate summary otherwise it leads to duplicate errors in Problems panel
15                 "/consoleloggerparameters:NoSummary"
16             ],
17             "group": "build",
18             "presentation": {
19                 // Reveal the output only if unrecognized errors occur.
20                 "reveal": "silent"
21             },
22             // Use the standard MS compiler pattern to detect errors, warnings and infos
23             "problemMatcher": "$msCompile"
24         }
25     ]
26 }

 

posted @ 2021-06-11 20:57  疯狗强尼  阅读(2611)  评论(0编辑  收藏  举报