安装相关插件
打卡后进入如下界面,选择这个c/c++的,然后点击install进行安装,大概几秒钟就好了,安装完成后install按钮会变成uninstall(卸载):
安装编译器(mingw-w64 gcc)下载完成后解压:
然后配置环境变量
找到这个文件夹内的一个叫bin的文件夹:
然后把它的地址复制一下,找到此电脑(或者我的电脑)——>右键——>属性
然后进入到下面这个页面,打开高级系统设置:
在弹出的页面中选择“高级”分页,找到环境变量,单击打开:
然后在环境变量中的系统变量中,找到path变量:
打开之后将刚刚复制的地址添加进去:
然后点确定,之前弹出的所有页面都点击确定。然后测试环境配置是否成功:
crtl+r快捷键打开运行窗口,在里面输入cmd,回车打开cmd.exe
在cmd.exe中输入如下命令:
gcc -v -e -x c++ -
如果运行结果像下方图片中这样,就配置成功了。
配置最后在vscode中进行相关配置:
先新建一个文件夹作为c语言项目文件,然后点击菜单栏中的file——>open folder,找到刚才新建的文件夹,然后点击选择文件夹打开这个项目文件。
然后在里面新建一个hello.c文件(名字随便起,以.c结尾就行了)
然后再建一个
.vscode文件夹(注意前面有个点),在里面建三个文件,c_cpp_properties.json、launch.json、tasks.json
c_cpp_properties.json:将这段代码复制进去{ "configurations": [ { "name": "win32", "includepath": [ "${workspaceroot}", "c:/program files/mingw64/include/**", "c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++", "c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32", "c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward", "c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include", "c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed", "c:/program files/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:/program files/mingw64/include/**", "c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++", "c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32", "c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward", "c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include", "c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed", "c:/program files/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include" ] } } ], "version": 4}
然后,下方红框里的内容需要修改,将所有的 改为自己的安装路径,就是我们之前下载的编译器的地址:
把你的mingw-w64 gcc解压后的文件中的mingw64的地址复制下来,替换代码里所有的 d:/program files (x86)/softwarefactory/x86_64-8.1.0-release-win32-sjlj-rt_v6-rev0/mingw64/ :
launch.json:复制粘贴,然后midebuggerpath属性里的内容也要改成自己的路径{ "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": [], "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\\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"//解决中文乱码 ] } ], "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared", "showreusemessage": true, "clear": false }}
然后就可以在之前建的hello.c文件里面写程序啦,比如我们熟悉的hello world:
#include<stdio.h>main(){ printf("hello world\n"); //system("pause");}
f5运行结果:
vscode配置c环境就配置完成。
推荐学习:《vscode教程》
以上就是如何使用vscode配置c语言环境的详细内容。
