Windows普遍被使用,一些人编程开始之路就是从Windows开始,笔者总结了Windows 10系统上搭建vscode C++的流程,验证通过供参考。visual studio code(简称vscode)是一个功能强大的开源IDE,相比Visual Studio更加灵活易用,笔者做工程项目早期用Clion\Visual Studio,现已投向vscode,插件定制化环境,是目前体验最好的IDE。
官网下载vscode windows版,如果下载速度不满意,参考https://zhuanlan.zhihu.com/p/112215618,将下载地址,如https://az764295.vo.msecnd.net/stable/2b9aebd5354a3629c3aba0a5f5df49f43d6689f8/VSCodeUserSetup-x64-1.54.3.exe中az764295.vo.msecnd.net替换为vscode.cdn.azure.cn
具体版本的下载链接,不必担心下错了版本, https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-win32/seh/x86_64-8.1.0-release-win32-seh-rt_v6-rev0.7z/download
推荐使用开源免费的解压工具 7zip 解压到C盘,或C盘的Programs文件夹下,下一步进行环境变量的设置需要其路径。
win键+s启动搜索 ,打开编辑账户的环境变量
,在xxx的用户变量点击Path
一行,点击编辑,在编辑环境变量窗口点击新建
,输入下载解压后的MinGW的bin文件夹的完整路径,如C:\mingw-w64\mingw64\bin,确定保存。
需要验证下gcc编译器是否已正确安装,win + r,输入cmd启动命令行窗口,在命令行窗口输入 g++ --version
,观察是否有如下类似输出,
g++ (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
如有则安装正确。
执行vscode安装程序,对话框勾选所有选项以创建桌面快捷方式,完成安装,启动vscode。
点击四个方块的图标,弹出插件市场,在搜索栏搜c++,选中C/C++
,点击安装。
C++(GDB/LLDB)
,接着选择g++.exe
,工程文件夹下增加.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\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
注意, “miDebuggerPath”: “C:\mingw-w64\mingw64\bin\gdb.exe”,路径要写本人PC上对应的。
{
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
"command": "C:\\mingw-w64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\mingw-w64\\mingw64\\bin"
}
}
],
"version": "2.0.0"
}
同样注意,“cwd”: "C:\mingw-w64\mingw64\bin"中路径要改成自己的。
.vscode
文件夹,在其中创建上述两个配置文件,注意上述两个注意点。搭建就完成了,打开主程序文件,在行前点击即可加入断点,按F5调试或shift F5开始测试了。
end