下载和安装好了 VSCode
VSCode只是一个编辑器(editor),不是IDE(集成开发环境),不含编译器(compiler)和许多其它功能,所以编译器要自己装好。
知识回顾
gcc如何执行C语言
这 4 步大致的工作内容如下:
(1) 预处理, C 编译器对各种预处理命令进行处理,包括头文件包含、宏定义的扩
展、条件编译的选择等;
(2) 编译,将预处理得到的源代码文件,进行“翻译转换”,产生出机器语言的目标
程序,得到机器语言的汇编文件;
(3) 汇编,将汇编代码翻译成了机器码,但是还不可以运行;
(4) 链接,处理可重定位文件,把各种符号引用和符号定义转换成为可执行文件中
gcc 编译器通过编译形成目标文件,后缀名是.o ,类似微软编译器(VS的目标文件后缀名是.obj)
1、linux下
$ gcc -c main.c // 会得到 main.o 文件
$ gcc main.o // 链接得到 a.out 文件 a.out 是 gcc 默认输出文件名称,可以通过-o 参数指定新的文件名。
$ ./a.out // 执行a.out 输出内容 .out 是linux的文件格式
2、windows下
gcc -g test.c -o test.exe // 把test.c 编译成test.exe 文件 -o 自定文件格式
./test.exe // 当前目录下直接执行 test.exe文件
MinGW-w64是c语言文件的编译器
MinGW-w64 - for 32 and 64 bit Windows 官网在此页面下载 MinGW-w64,往下稍微翻一下,选最新版本中的x86_64-posix-seh。最好不要用 Download Latest Version,这个是在线安装包,有可能因为国内的“网络环境”下载失败。解压文件到自己的目录
例如:我解压到 E:\RuanJian_2\VSCode
1、系统环境变量,path,在path中加上解压后的mingw64/bin的路径
如:E:\RuanJian_2\VSCode\mingw64\bin
2、要验证一下是否配置成功,快捷键win+R,cmd,输入 gcc -v
3、如果如下图所示,即成功了
在vscode里面下载c/c++官方插件(必须)
Code Runner 插件,快速运行C语言(可选)
1、在windows 下创建测试文件夹。
2、打开VSCode,打开c-test 文件夹,找到创建c-test的目录
3、打开成功后的页面
4、创建 .vscode 文件夹
在.vscode 下创建launch.json,tasks.json,settings.json,c_cpp_properties.json 四个文件
1、launch.json:将下面代码复制进去
注意: 需要修改的地方miDebuggerPat换成自己安装mingw64的目录
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "cmd",
"preLaunchTask": "compile",
"args": [
"/C",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"&",
"echo.",
"&",
"pause"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal"
},
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "E:\\RuanJian_2\\VSCode\\mingw64\\bin\\gdb.exe",// 自己电脑的gdb.exe
"preLaunchTask": "compile",//这里和task.json的label相对应
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
2、tasks.json:将下面代码复制进去
{
"version": "2.0.0",
"tasks": [
{
"label": "compile",
"type": "shell",
"command": "gcc", // C语言是用gcc的,如果编译的是C++文件,这需要改为g++
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK"//Console窗体输出字符编码 解决控制台中文乱码问题
]
}
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
}
3、settings.json:将下面代码复制进去
{
"files.associations": {
"cmath": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"memory": "cpp",
"new": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xmemory0": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp",
"stdio.h": "c"
}
}
4、c_cpp_properties.json:将下面代码复制进去
注意: 需要修改的地方includePath和path换成自己安装mingw64的目录
{
"configurations": [
{
"name": "Win32",
"includePath": [ // 如果使用别人的库,路径就要加到includePath和browse里
"${workspaceRoot}",
"E:/RuanJian_2/VSCode/mingw64/include/**", // 需要改成自己的mingw64目录
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"${workspaceRoot}",
"E:/RuanJian_2/VSCode/mingw64/include/**", // 需要改成自己的mingw64目录
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"E:/RuanJian_2/VSCode/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include"
]
}
}
],
"version": 4
}
#include <stdio.h>
// 输出你好
int main(){
printf("Hello,你好");
return 0;
}
Run build Task
C/C++:gcc.exe 生成活动文件
成功截图
如果你想debug,打断点,Run —> start debugging 即可!