安装VSCode
VSCode下载连接https://code.visualstudio.com/
安装过程中,需要根据自己的操作系统来选择安装的版本,这里我的是windows操作系统,所以选择装稳定版。
下载过程中,会遇到下图情况,可以根据自己需要来进行安装。
安装扩展包
安装完成后,我们需要打开vscode,在扩展中搜索C/C++扩展,并安装,在此之前你也可以把字体改成中文的,中文包如果不会设置可以参考下方博客。
下载编译器,MinGW
我们需要到下方官网中下载编译器
MinGW-W64https://sourceforge.net/projects/mingw-w64/files/mingw-w64/
官网中首页会有最新版本,在这里我下载的是x86_64-posix-seh(首页往下滑动就能找到),下载完之后需要解压,在这里给大家建议--在C盘建个文件夹,命名为MinGW-W64 GCC,解压到这个文件夹就可。因为稍后我们需要在vscode中添加json文件,路径如果不一样比较麻烦。
解压成功后,我们打开刚刚解压的文件
进到bin文件夹中,然后复制该路径,我们去配置环境变量。
配置环境变量
如何配置环境变量
打开win,搜索高级系统设置,点击环境变量,在系统变量中,我们找到path。
把刚刚复制的路径添加到里面。(最下面就是),然后点击三个确定即可。
检查环境变量的配置是否成功
win+R输入cmd打开命令窗口,输入以下命令
gcc -v -E -x c++ -
如果出现以下输出,则表示配置成功
VScode相关配置
在D盘新建一个文件夹,专门用来放置C/C++程序,然后在VSCode中打开自己刚刚的文件夹,然后在根目录下继续创建一个叫做.vscode的文件夹.
里面有三个文件:c_cpp_properties.json,launch.json,tasks.json
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"C:/MinGW-W64 GCC/mingw64/include/**",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"__GNUC__=6",
"__cdecl=__attribute__((__cdecl__))"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"${workspaceRoot}",
"C:/MinGW-W64 GCC/mingw64/include/**",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"C:/MinGW-W64 GCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
]
}
}
],
"version": 4
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "cmd",
"preLaunchTask": "echo",
"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": "C:\\MinGW-W64 GCC\\mingw64\\bin\\gdb.exe",// 自己电脑的gdb
"preLaunchTask": "echo",//这里和task.json的label相对应
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK",//解决中文乱码
"-lstdc++"//解决只能运行c不能运行c++
]
}
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
}
}
按照上面三段代码进行复制进去,注意在解压MinGW的时候,一定要自己新建MinGW64 GCC文件夹,不然会报错。然后就可以进行测试了~
测试
#include<stdio.h>
int main()
{
printf("hello world\n");
return 0;
}