关于vscode下载以及环境变量的设置已经有许多的文章,这里不再赘述,这篇文章主要是解决同时配置C和C++两种环境中的问题。
首先配置C语言环境
创建一个.vscode文件夹,在里面创建三个文件c_cpp_properties.json、launch.json、tasks.json
c_cpp_properties.json文件中
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceRoot}",
"D:/vscode/mingw64/include/**", //以下7行需要修改为自己的地址
"D:/vscode/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/vscode/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/vscode/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/vscode/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/vscode/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"D:/vscode/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}",
"D:/vscode/mingw64/include/**", //以下7行需要修改为自己的地址
"D:/vscode/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/vscode/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/vscode/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
"D:/vscode/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/vscode/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"D:/vscode/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."
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal",
},
{//这个大括号里是我们的‘调试(Debug)’配置
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal",
"MIMode": "gdb",
"miDebuggerPath": "D:\\vscode\\mingw64\\bin\\gdb.exe",//更改为自己的地址
"preLaunchTask": "echo",
}
]
}
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": "new",
"showReuseMessage": true,
"clear": false
}
}
这样我们的C环境就配置成功
接下是C++环境
如果我们在上面配置的文件中直接编译运行C++会出现
解决这个问题我的方法是新建一个专门用来存C++代码的文件夹,然后新建一个.vscode文件夹,新建launch.json和tasks.json文件
launch.json文件中:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment":[],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\vscode\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "task g++",
}
]
}
tasks.json文件中:
{
"version":"2.0.0",
"tasks": [
{
"type": "shell",
"label": "task g++",
"command": "D:\\vscode\\mingw64\\bin\\g++.exe",
"args": [
// "-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\vscode\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
// "detail": "调试器生成的任务。"
}
]
}
但是这里有一个问题就是运行框会闪退,我是用system("pase");来解决这个问题,当然你也可以将launch.json中的"externalConsole"后面的ture改为false不出现小黑框(如果有大神能解决这个问题的话欢迎留言讨论)....