如何在vscode中优雅的编写C语言
各位好,我认为vscode编辑器在windows环境下除了Pycharm外是最方便的IDE了,但在初学C语言时很少有人的第一个C语言软件使用的是vscode来编译与运行,使用10年前的IDE,在学习的时候看上去就不那么优雅与美观,因此本文旨在手把手的描述如何在vscode中实现C语言的编译与运行。(其实也挺无聊的,无论什么软件,请重点在意代码本身的实现)
1. 写在开头
大概搜了下vscode运行C语言的文章,其实本文很多也是拷贝了他们的方法,并稍加改善,更贴合萌新入门,避免不必要的时间浪费。最最必要的前提条件是下载一个vscode的ide,注意这个ide的安装是不需要管理员权限的,对于大公司打工人(用户权限,不能随意安装软件)是一个福音。
1.1 下载vscode
1.2 安装GCC编译器
本文选用了MinGW,推荐使用离线安装包安装,其实就是解压到方便找到的目录,我选的是i686-posix-dwarf,据说它有着最佳的兼容性。版本都可以。 请注意并记录下解压的具体目录位置,后面更改.json文件时需要
1.3 安装vscode的两个插件
Code Runner C/C++
1.4 环境变量设置
进入windows的环境变量界面,选择系统变量中的Path变量,将mingw64(或者mingw32)\bin的路径添加到Path中去。 设置完毕后可以在终端通过查看gcc版本进行验证是否成功:
2. 最终效果
为了明确这些操作之后需要实现的程度,这里直接对最终效果进行描述,首先需要新建一个文件夹,这个文件夹自己定义名称,我的叫 C TEST PROJECTS,在这个文件夹中新建如下几个文件,右击该文件夹选择用vscode打开后就是下图效果: 这里的C TEST PROJECTS就是接下来需要新建C文件的地方,可以将相关的例程放在这个文件路径中,当然如果需要新建其他的文件夹,只要保证整体的文件结构不变即可。 注意这里.vscode是一个C TEST PROJECTS下的子文件夹,2个.json文件属于改子文件夹内,测试用的hello.c文件不包含在.vscode文件夹内,但包含在C TEST PROJECTS文件夹内。
3. 配置文件内容变更
文件建立完毕后,就要在各个文件内挨个放内容了: hello.c 这里的代码内容很随意,能编译运行就可以了。
#include <stdio.h>
int main(void)
{
printf("Hello world\n");
return 0;
}
c_cpp_properties.json 这里的代码内容如下,带Comments的需要改为自己的MinGW目录。
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"C:/Program Files/mingw32/include/**",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/include/c++",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/include/c++/i686-w64-mingw32",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/include/c++/backward",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/include",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/include-fixed",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/../../../../i686-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"${workspaceRoot}",
"C:/Program Files/mingw32/include/**",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/include/c++",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/include/c++/i686-w64-mingw32",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/include/c++/backward",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/include",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/include-fixed",
"C:/Program Files/mingw32/lib/gcc/i686-w64-mingw32/6.4.0/../../../../i686-w64-mingw32/include"
]
}
}
],
"version": 4
}
launch.json 这里的代码内容如下,带Comments的需要改为自己的MinGW目录,仅一处。
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Windows 上的 Bash 启动",
"type": "cppdbg",
"request": "launch",
"program": "输入程序名称,例如 ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"pipeTransport": {
"debuggerPath": "/usr/bin/gdb",
"pipeProgram": "${env:windir}\\system32\\bash.exe",
"pipeArgs": ["-c"],
"pipeCwd": ""
},
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "cmd",
"preLaunchTask": "echo",
"args": [
"/C",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"&",
"echo.",
"&",
"pause"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole":true
},
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw32\\bin\\gdb.exe",
"preLaunchTask": "echo",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json 这里的代码内容如下,直接复制,无需更改。
{
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK"
]
}
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
}
全部修改完毕后记得保存。
4. 验证与总结
上述步骤看似很长,其实不是很麻烦,新建三个文件,修改一下路径就可以实现优雅的编程了,上文中提到的hello.c 就是我们的测试文件,运行方法有两种,参考Code runner,按下F1,然后输入Run Code后回车确认; 或者按下此时vscode界面左上角的运行Button,编译与运行后效果如下:
|