Visual Studio Code创建C语言编译环境
Visual Studio Code创建C语言编译环境
1.下载并安装Visual Studio Code
Visual Studio Code介绍
Visual Studio Code(简称“VS Code” )是Microsoft在2015年4月30日Build开发者大会上正式宣布一个运行于 Mac OS X、Windows和 Linux 之上的,针对于编写现代Web和云应用的跨平台源代码编辑器
, 可在桌面上运行,并且可用于Windows
,macOS
和Linux
。它具有对JavaScript,TypeScript和Node.js的内置支持,并具有丰富的其他语言(例如C++,C#,Java,Python,PHP,Go)和运行时(例如.NET和Unity)扩展的生态系统。
该编辑器支持多种语言和文件格式的编写,截止2019年9月,已经支持了如下37种语言或文件:F#、HandleBars、Markdown
、Python
、Java
、PHP
、Haxe、Ruby、Sass、Rust、PowerShell
、Groovy、R、Makefile、HTML
、JSON
、TypeScript、Batch、Visual Basic、Swift、Less、SQL、XML
、Lua、Go、C++
、Ini
、Razor、Clojure、C#、Objective-C、CSS、JavaScript、Perl、Coffee Script、Dockerfile、Dart
必要插件的安装
1.1 C/C++
1.2 Code/Runner
2.C/C++环境配置**
2.1 MinGW-w64
MinGW的全称是:Minimalist GNU on Windows
。它实际上是将经典的开源C语言 编译器 GCC 移植到了 Windows 平台下,并且包含了 Win32API ,因此可以将源代码编译为可在 Windows 中运行的可执行程序。而且还可以使用一些 Windows 不具备的,Linux平台下的开发工具。一句话来概括:MinGW 就是 GCC 的 Windows 版本 。
MinGW-w64 与 MinGW 的区别在于 MinGW 只能编译生成32位可执行程序,而 MinGW-w64 则可以编译生成 64位 或 32位 可执行程序。正因为如此,MinGW 现已被 MinGW-w64 所取代,且 MinGW 也早已停止了更新,内置的 GCC 停滞在了 4.8.1 版本,而 MinGW-w64 内置的 GCC 则更新到了 8.1.0 版本。(2020/12/15)
建议MinGW-w64安装路径跟Visual Studio Code在同一目录下
2.2 环境变量设置
在Windows系统中添加MinGW-w64的环境变量
验证环境变量是否添加成功
在cmd中输入gcc -v
,出现以下结果,即成功
3.测试C语言编译环境
3.1 创建项目目录,例如PROJECT
3.2 添加编译目录,.vscode
目录
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/App/Visual_Studio_Code/MinGW/include/*"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:/App/Visual_Studio_Code/MinGW/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
"D:/App/Visual_Studio_Code/MinGW/include/*"
根据实际安装位置修改
"compilerPath": "D:/App/Visual_Studio_Code/MinGW/bin/gcc.exe"
根据实际安装位置修改
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x86",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"miDebuggerPath": "D:\\App\\Visual_Studio_Code\\MinGW\\bin\\gdb.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"externalConsole": true,
"preLaunchTask": "g++"
}
]
}
"D:\\App\\Visual_Studio_Code\\MinGW\\bin\\gdb.exe"
根据实际位置修改
settings.json
{
"files.encoding": "gbk",
"files.associations": {
"stdio.h": "c",
"app_test.h": "c"
}
}
tasks.json
{
"version": "2.0.0",
"command": "g++",
"type": "shell",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
},
"args": ["-m32","-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe"],
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
3.3 创建src目录,在该目录下创建hello.c源文件
hello.c内容如下:
#include
int main ()
{
printf ("Hello World!\n");
return 0;
}
编译输出结果如下: