Using Mingw-w64 in VS Code
Related LinksEdit
准备工作:Edit
1、安装Visual Studio Code。
The C/C++ extension 自身不包括 C++ compiler 或者 debugger,需要提前安装:
2、for Windows: 安装 Mingw-w64。在Windows平台上,需要Mingw-w64支持GCC 的 C++ compiler (g++) 和 GDB debugger 运行。注意,安装路径里面不可以有空格,所以,不用默认安装路径 C:/Program Files/,可以采用:C:/mingw-w64。安装完毕后,添加环境变量:Mingw-w64的bin文件夹,例如:C:\mingw-w64\mingw32\bin。 检查Mingw-w64是否正常安装:打开Command Prompt,输入:
g++ --version gdb --version
如果一切正常,会有相关版本信息提示。
下载安装文件地址:mingw-w64-install.exe,下载至本机后,点击安装,在安装过程中,需要调整默认安装路径。
也可以通过SourceForge网站链接地址:MinGW-w64 - for 32 and 64 bit Windows,下载 mingw-w64-v7.0.0.7z压缩文件,解压至C:\mingw-w64。但是找不到bin文件夹,所以,下一步不知道如何处理。
3、for Ubuntu: 测试:gcc --help,确定gcc已经安装。
4、安装 C/C++ extension for VS Code 插件。在VScode里面,按Ctrl+Shift+X,打开插件列表,搜索:C++,找到 C/C++extension 并安装。
HelloWorld 代码调试Edit
第一步,打开工作空间 WorkspaceEdit
在终端里面输入
mkdir ArduinoUNOProjects cd ArduinoUNOProjects mkdir myDelay cd myDelay code .
经过测试:
- “code .”命令,在当前目录 myDelay 里面,打开 VS ,进入 Workspace ,当前目录即为当前的“Workspace”。
- “code ..” 命令,在上一级目录 ArduinoUNOProjects 里面打开 VS ,进入 Workspace。
- “code” 在再上一级目录 mq 里面打开 VS ,进入 Workspace。
此时,左侧的文件列表里面,在 .vscode 文件夹里面会出现:settings.json文件。
第二步,新建文件:helloworld.cppEdit
粘贴以下代码,保存文件。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
会看到关键词已经高亮显示。在代码里面添加变量:msg. ,出现成员列表选项框,说明 IntelliSense 已经启用,C/C++ extension 添加成功。
第三步,build程序,设置参数Edit
在此过程中,系统会创建task.json文件,告知VScode如何编译代码。
在菜单栏里,点击:Terminal > Configure Default Build Task,在打开的选项列表里面,默认由三个选项:
- shell:cpp.exe build active file detected task
- shell:g++.exe build active file
- shell:gcc.exe build active file
选择:#shell:g++.exe build active file,调用g++编译器生成可执行代码。此时,在.vscode文件夹里,已经创建文件:task.json
{
// See https://code.visualstudio.com/docs/editor/variables-reference
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\mingw-w64\\mingw32\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
第四步、运行buildEdit
在菜单栏里,单击:Terminal > Run Build Task...,编译结果如下图。
第五步,执行helloworld.exe 文件Edit
在VScode内嵌的Terminal面板里面,选择 powershell方式,单击 “+”,新建一个Terminal窗口,文件夹helloworld作为当前工作目录,运行 dir 找到可执行文件:helloworld.exe,运行该文件,过程如下图所示。注意:需要在文件名前面添加:.\,即执行:.\helloworld.exe
第六步,Debug helloworld.cppEdit
在此过程中,会创建一个launch.json文件,在启动调试的时候,告知VS Code 启动GDB debugger。
在菜单栏里,单击:Debug > Add Configuration... ,在弹出的列表菜单里,选择:C++:(GDB/LLDB),在新出现的列表项里,选择:g++.exe build and debug active file。此时,在新弹出的DEBUG CONSOLE面板里面,出现 GNU gdb(GDB) 8.1的列表信息 而不敢在.vscode文件夹里自动生成文件: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": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw-w64\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
现在,可以单击:Debug > Start Debugging 启动调试过程,添加断点,单步执行,观察程序执行序列,各个变量值如何变化。
第七步,设置C/C++程序编译过程中的参数Edit
在此过程中,会创建一个c_cpp_properties.json文件,定义如下参数:compiler的路径, include paths, C++ standard的位置等。
按F1键(或者:ctrl+shift+p),打开搜索栏,输入:C/C++:Edit Configurations(UI)。这里输入的参数都会自动保存到c_cpp_properties.json文件里面。只需要添加Include 的路径参数,因为程序文件里面用到的头文件,可能不在当前的workspace 和标准库的路径里面。