基本步骤:
下载并安装VSCode
vscode下载地址https://code.visualstudio.com/
不会安装vscode的看下方博客:
超详细VSCode安装教程(Windows)_索儿呀的博客-CSDN博客
安装C++插件
安装编译器(MinGW-W64 GCC)
C编译器(MinGW-W64 GCC)下载地址:https://sourceforge.net/projects/mingw-w64/files/mingw-w64/
在线下载的exe文件一般是会有网络的问题,导致下载不了,建议windows64位直接下载8.1.0版本的x86_64-win32-seh或者x86_64-posix-seh。两者在多线程方面有些许差异,一般情况不会用到该功能,所以两者皆可。下载后用解压软件解压即可。
配置环境变量
找到这个文件夹内的一个叫bin的文件夹,然后把它的地址复制一下:
打开之后将刚刚复制的地址添加进去:
然后点确定,之前弹出的所有页面都点击确定。然后测试环境配置是否成功:
Win+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:/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:复制粘贴,然后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": [],"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/" />
然后就可以在之前建的hello.c文件里面写程序啦,比如我们熟悉的hello world:
#includemain(){printf("hello world\n"); //system("pause");}
程序闪退问题
上面的三个文件只要复制正确,路径改成自己的闪退问题就已经解决
实在不行你就把输入
system("pause");
f5运行结果:
vscode配置c/c++环境就配置完成。
以上参考博客:
VSCode配置C语言环境(完整版)_vscode c语言_SchizophreniA6的博客-CSDN博客
vscode可以调试c但是无法调试c++程序的_vscode不能调试c++代码_Pher12的博客-CSDN博客
VS code “密钥externalConsole已弃用,请改用console“的解决方法_密钥“externalconsole”已弃用。请改用“console”。_snowayoung的博客-CSDN博客
超详细VSCode安装教程(Windows)_索儿呀的博客-CSDN博客