VScode配置c/c++环境(无数试错版本)
在配置python时比较简单,c/c++有点复杂。
安装VScode
这个就不多说了,官网下载即可,或者应用市场下载。
下载mingw64并配置环境变量
建议采用离线下载,可参考以下文章,写的很详细。
下载安装MinGW-w64详细步骤(c/c++的编译器gcc的windows版,win10真实可用)
VScode和mingw64配置
首先新建一个文件夹作为c语言的项目文件,注意文件中不要含有汉字。
之后创建一个.vscode文件。包含以下三个文件。
- c_cpp_properties.json
将这两部分改成自己存放mingw64的路径
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"D:/LenovoSoftstore/Install/mingw64/include/**", //修改成自己的路径
"D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"D:/LenovoSoftstore/Install/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": "gcc-x64",
"browse": {
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "",
"path": [
"${workspaceRoot}",
"D:/LenovoSoftstore/Install/mingw64/include/**",
"D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"D:/LenovoSoftstore/Install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
]
},
"cppStandard": "c++17",
"cStandard": "c17"
}
],
"version": 4
}
- launch.json
将miDebuggerPath改成自己的路径
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "C:\\Windows\\System32\\cmd.exe", //解决闪退问题
"args": ["/c","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\LenovoSoftstore\\Install\\mingw64\\bin\\gdb.exe", /*修改成自己bin目录下的gdb.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++"
}
]
}
解决终端闪退问题:
- tasks.json
直接复制即可
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileBasenameNoExtension}.exe",
"-fexec-charset=GBK" //解决中文乱码问题
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
解决中文乱码问题:
测试
- c文件测试
- cpp文件测试