VS Code运行C语言的配置
记一下花费考研期间几个星期走过的坑:
- mingw64的安装时间长到令人窒息,绝逼是个坑,最后在网上找到了压缩包,可快速解压即可使用,但是网站不记得了。
- 网上的教程一大堆,行不通的一大堆,别人的配置是别人的配置,用合适自己的,而且每个人的文件路径也不一样。
- Clion的编译时间也是可以让你等到分分钟卸载,每次创建新文件还得cmakelist类似目录,添加可执行文件名称。886
- DevC++听说已经停止维护很久了,而且每次还得在编译选项中选择C语言的规则还是C++的。
配置过程正文
- 下载VS Code(Visual Studio Code),不是VS(Visual Studio2017,因为这个软件很庞大,使用更麻烦,不推荐使用)
- 下载C语言的编译环境,有MinGW64,TCC,Cygwin…等等
- 在VScode下载插件C/C++,code runner插件
- mingw64/bin的路径装到环境变量(其他编译环境也一样,文件路径的目录下要有gcc.exe,g++.exe之类的文件)
.
法一(针对Code Runner插件)
setting.json
{
"editor.fontSize": 16,
"code-runner.runInTerminal": true,
"terminal.integrated.fontSize": 15,
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
"code-runner.executorMap": {
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
},
}
法二(针对微软的C/C++插件)
在项目中创建.vscode文件,下面是需要包含的文件
- launch.json
- tasks.json
- c_cpp_properties.json
一般只需要改下面编译器的文件路径,我是使用了法一,法二虽然也成功了但是不了解为什么这样。
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"preLaunchTask": "Build",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x86_64",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"miDebuggerPath": "D:\\MinGW64\\bin\\gdb.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"windows": {
"command": "g++",
"args": [
"-ggdb",
"\"${file}\"",
"--std=c++11",
"-o",
"\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
]
},
"group":{
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/MinGW64/include/*"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/MinGW64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
也可参考https://blog.csdn.net/qq_28581077/article/details/81380341
看着还行