Visual Studio Code配置C语言教程
【前言】网上的vscode配置C/C++的教程不是有坑就是不全,总之就多数无法正常运行,故而出现本教程。
1.安装Visual Studio Code
2.安装必要插件
-
安装C/C++
打开软件,在插件市场中安装插件C/C++(快捷键Ctrl+Shift+X)
3.环境配置
-
下载mingw64
下载链接:https://pan.baidu.com/s/1lXnBx6gTh-IO6XCplo8DcQ
提取码:8888 -
配置环境
【windows10以下版本系统(不含windows10)】
最后编辑的时候在最后面加上一个分号,然后将mingw64中的bin目录的路径(例如:F:\mingw64\bin)粘贴进去。
【windows10/11】右键我的电脑,属性,系统高级设置,环境变量,后续与上面一样。
4.配置文件(C)
-
在本地新建一个文件夹,并使用vscode打开,注意,不要创建在vscode的软件目录之下,也不要创建在C盘,电脑账户名称为中文的用户不要将文件夹建在桌面上(后面会有解释)
-
创建.vscode文件夹
`在已经创建好的文件夹内创建.vscode文件夹
-
创建c_cpp_properties.json文件
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "F:\\mingw64\\bin\\gcc.exe", //需改成自己的路径
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}",
"compilerArgs": [
"-fexec-charset=GBK"
]
}
],
"version": 4
}
- 创建launch.json文件
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "F:\\mingw64\\bin", //需改成自己的路径
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "F:\\mingw64\\bin\\gdb.exe", //需改成自己的路径
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 生成活动文件"
}
]
}
- 创建settings.json文件
{
"files.associations": {
"stdlib.h": "c",
"stdio.h": "c"
},
"docwriter.progress.trackMethods": false,
"docwriter.progress.trackFunctions": true,
"C_Cpp.errorSquiggles": "disabled"
}
- 创建tasks.json文件
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "task g++",
"command": "F:\\mingw64\\bin\\gcc.exe", //需改成自己的路径
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"F:\\mingw64\\vscode", //需改成自己的路径
"-std=c++17"
],
"options": {
"cwd": "F:\\mingw64\\bin" //需改成自己的路径
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "F:\\mingw64\\bin\\gcc.exe", //需改成自己的路径
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK"
],
"options": {
"cwd": "F:\\mingw64\\bin" //需改成自己的路径
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
]
}
5.配置文件(C++)
- 其他配置文件都相同,只是tasks.json文件需稍作改动
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "task g++",
"command": "F:\\mingw64\\bin\\gcc.exe", //需改成自己的路径
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"F:\\mingw64\\vscode", //需改成自己的路径
"-std=c++17"
],
"options": {
"cwd": "F:\\mingw64\\bin" //需改成自己的路径
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "F:\\mingw64\\bin\\g++.exe", //需改成自己的路径
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK"
],
"options": {
"cwd": "F:\\mingw64\\bin" //需改成自己的路径
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
]
}
6.常见报错
- GDB报错
【报错原因】此种报错的原因是文件路径中含有中文,上述示例中的情况就是因为该文件夹的目录之中含有中文,如C:\Users\晋成\Desktop\C++\01.exe,这其中的用户名为中文,用户名为英文不会有此报错。亦或者说文件夹名称或文件名称含有中文。
【解决方案】将文件夹路径之中的中文改成英文。
- Lunch报错
【报错原因】.vscode配置文件中的路径错误。
【解决方案】将路径改为正确路径
例:c_cpp_properties.json文件中的路径错误
其中的错误就在于,其在解压mingw64压缩包的时候将其全部解压至D盘内,导致文件被打散,然后c_cpp_properties.json文件中的路径为D:\mingw64\bin\gcc.exe,故无法找到此路径,因而报错。最终只需将mingw64的文件整理一下,路径改一下就成。
【声明】本教程写的比较匆忙,可能会有错误,还请指正。如需转载,还望声明出处,谢谢配合。