一、vscode(这你得有)
二、安装插件[C/C++]
三、 安装mingw-w64
- 在mingw-w64官网下载64位的mingw-w64离线包
https://sourceforge.net/projects/mingw-w64/files/?source=navbar
- 配置计算机环境变量如图(我的解压路径是C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin,因此环境变量中加入该路径)
- 安装完成后打开控制台,分别输入 g++ -v 和 gcc -v、gdb -v 查看环境是否安装成功(成功会显示当前版本号)
四、重启电脑(important)。
五、配置运行环境
打开vscode,选择或新建一个空文件夹目录打开作为项目目录。
点击“文件”按钮,再点击“新建文件夹”按钮,并重命名为”.vscode”。
在该文件夹内,在点击“新建文件”按钮,建launch.json,settings.json,tasks.json三个.json文件。如图所示。
3个json文件如下配置
launch.json:
ps:"miDebuggerPath"这个参数可能是你最需要修改的参数,需要改成你自己的gdb.exe路径。
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch C/C++调试",// 配置名称,将会在启动配置的下拉菜单中显示
"preLaunchTask": "build",
"type": "cppdbg",// 配置类型,这里只能为cppdbg
"request": "launch",// 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径
"args": [],// 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false,// 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}",// 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
"environment": [],
"externalConsole": true,// 调试时是否显示控制台窗口,一般设置为true显示控制台
"MIMode": "gdb",
"miDebuggerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gdb.exe", // 这里修改GDB路径为安装的mingw64的bin下的gdb.exe路径
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true,
}
]
}]
}
settings.json:
// Configuring tasks.json for C/C++ debugging
// author: huihut
// repo: https://gist.github.com/huihut/887d3c28db92617bd5148c20a5ff112a
// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process
{
"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\""
]
}
}
],
"files.autoSave": "afterDelay",
"[c]": {
},
"files.encoding": "utf8",
"files.autoGuessEncoding": true,
"explorer.confirmDragAndDrop": false,
"workbench.colorTheme": "Dracula",
"team.showWelcomeMessage": false,
// 工作区设置
"C_Cpp.errorSquiggles": "Disabled",
"files.associations": {
"stdlib.h": "c",
"time.h": "c"
}
}
tasks.json:
{
"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\"",
"-finput-charset=UTF-8",//输入编译器文本编码 默认为UTF-8
"-fexec-charset=GBK"//编译器输出文本编码 自行选择
]
}
}
]
}
至此,环境配置基本完成。
六、运行C代码
新建helloworld.c文件,键入或粘贴C语言的helloworld代码,按F5调试运行。(注意,以后所有的c文件都要放到与.vscode相同的目录下,如果想放到别的地方,需要把配置文件.vscode也移过去)