参考文章:WIN下vscode调试C/C++,从零开始生成helloworld项目【2018年6月29日】
Visual Studio Code (vscode) 配置 C / C++ 环境
参考了许多文章,但大多数文章要么代码存在错误,要么跳跃步骤,难以理解。我先是按照第二篇文章配置的,但是配制出总是存在“prelaunchtask g++已终止,退出代码为1”的错误。而第一篇文章堪称保姆级教程。
一、 安装vscode
vscode官网下载安装包直接安装即可
官网地址:VScode
二、 vscode内安装C/C++ 插件
vscode内按快捷组合键Ctrl+Shift+X(或如图第①步点击[拓展]按钮)打开拓展分页,在搜索栏输入" C ",查找到如图的第一个插件,安装并重新加载之。
三、 安装mingw-w64(具体安装与环境变量配置可以查看这里)
在mingw-w64官网下载64位的mingw-w64在线安装包(以在线包为例)或离线包(离线包直接解压出来就能用)
下载地址:https://sourceforge.net/projects/mingw-w64/files/
下载的文件:进入网站后不要点击 “Download Lasted Version”,往下滑,找到最新版的 “x86_64-posix-seh”。
在线包:根据系统选择合适的安装包进行下载,选择在线安装器
安装MinGW:下载后是一个7z的压缩包,解压后移动到你想安装的位置即可。
配置计算机环境变量如图(我的安装路径是D:\mingw64,因此环境变量这么加)
安装完成后打开控制台,分别输入 g++ --version 和 gcc --version 查看环境是否安装成功(是否有当前版本号)
四、重启电脑
(这点很重要,一开始我也没有重启,后来vscode代码写出来跑了很多次提示没有找到g++命令,最后重启解决)
五、运行C++代码
打开vscode,选择或新建一个空文件夹目录打开作为项目目录,新建一个test.cpp文件,键入如下helloworld代码
#include <stdio.h>
int main(int argc, char *args[])
{
int i, j;
printf("hello world!\n");
printf("argc:%d\nargv:\n", argc);
for (i = 0; i < argc; i++)
{
printf("%d:%s\n", i, args[i]);
}
getchar();
return 0;
}
按下F5,顶部或出现如下菜单,选择C++(GDB/LLDB)
系统自动在当前目录下创建.vscode文件夹,并在其中新建一个launch.json的模版文件如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
将该模版修改为如下(可以直接复制,并修改有注释的一段)
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"preLaunchTask": "build",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/mingw64/bin/gdb.exe", // 这里修改GDB路径为安装的mingw64的bin下的gdb.exe路径
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}]
}
vscode中按下组合键Shift+Ctrl+P,在唤出的任务栏中键入>task,下拉找到并点击 Tasks:Configure Task(任务:配置任务)项,并在接下来的返回项中选择使用模版创建tasks.json文件
系统会自动在.vscode文件夹下创建一个tasks.json文件,自动生成的代码如下
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "msbuild",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true",
"/t:build"
],
"group": "build",
"presentation": {
// Reveal the output only if unrecognized errors occur.
"reveal": "silent"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
]
}
同理,将之修改为如下代码(可直接覆盖)
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"windows": {
"command": "g++",
"args": [
"-ggdb",
"\"${file}\"",
"--std=c++11",
"-o",
"\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
]
}
}
]
}
至此,环境配置完成,转到C++代码页,按下F5,根目录下出现.cpp文件同名的.exe文件,代码自动执行,完成。
六、运行C代码
仿照第五步,新建helloworld.c文件,键入或粘贴C语言的helloworld代码
#include <stdio.h>
#include <windows.h>
int main() {
printf("hello world!\n");
system("pause");
return 0;
}
在.c页面内单击F5,稍候片刻出现同名.exe并自动执行,完成。
文章代码及图片大部分来源于第一篇文章,少部分来自于第二篇文章,本质上属于复制粘贴的低创性文章。仅用来记录解决问题的过程。