本篇文章主要介绍的是Visual Studio Code下如何配置.vscode文件夹下面的四个文件,如下。具体的MinGW可以参考其他博客下载以及配置环境变量。
整体图片为:
tasks.json文件如下
此文件中的command内容下的路径注意修改为自己的MinGW下的g++所在路径,示例中的是支持c++的g++.exe
{
"version": "2.0.0",
"command": "D:\\MinGW\\bin\\g++.exe",
"args": ["-g","-std=c++11","${file}","-o","${fileBasenameNoExtension}.exe"], "problemMatcher":
{"owner": "cpp", "fileLocation": ["relative", "${workspaceFolder}"], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } }, "tasks": [ { "type": "shell", "label": "gcc.exe build active file", "command": "D:/MinGW/bin/gcc.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "D:/MinGW/bin" }, "problemMatcher": [ "$gcc" ] }, { "type": "shell", "label": "cpp.exe build active file", "command": "D:\\MinGW\\bin\\cpp.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "D:\\MinGW\\bin" }, "problemMatcher": [ "$gcc" ] }, { "type": "shell", "label": "cpp.exe build active file", "command": "D:\\MinGW\\bin\\cpp.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "D:\\MinGW\\bin" }, "problemMatcher": [ "$gcc" ] } ]}
settings.json文件如下
{
"files.defaultLanguage": "cpp", // ctrl+N新建文件后默认的语言
"editor.formatOnType": true, // 输入时就进行格式化,默认触发字符较少,分号可以触发
"editor.snippetSuggestions": "top", // snippets代码优先显示补全
"code-runner.runInTerminal": true, // Set the executor of each language.
"code-runner.executorMap": { "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt", },
"code-runner.saveFileBeforeRun": true,
"code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
"code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息
"C_Cpp.clang_format_sortIncludes": true, // 格式化时调整include的顺序(按字母排序)
"C_Cpp.intelliSenseEngine": "Default", // 可以为Default或Tag Parser,后者较老,功能较简单。具体差别参考cpptools扩展文档
"C_Cpp.errorSquiggles": "Disabled", // 因为有clang的lint,所以关掉
"C_Cpp.autocomplete": "Disabled", // 因为有clang的补全,所以关掉
"clang.cflags": [ // 控制c语言静态检测的参数
"--target=x86_64-w64-mingw", "-std=c11",
"-Wall"
],
"clang.cxxflags": [ // 控制c++静态检测时的参数
"--target=x86_64-w64-mingw",
"-std=c++17",
"-Wall"
],
"clang.completion.enable":true // 效果效果比cpptools要好}
launch.json文件如下
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.cpp", "args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe", #这里最重要,注意修改为自己电脑下的gdb.exe绝对路径(我下载的是MinGW)
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
]}
c_cpp_properties.json文件如下
同理注意修改compilerPath的路径
{
"configurations": [
{
"name": "MinGW",
"intelliSenseMode": "clang-x64",
"compilerPath": "D:/MinGW/bin/gcc.exe",
"includePath": ["${workspaceFolder}"],
"defines": [],
"browse": {"path": ["${workspaceFolder}"],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": "" ,
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
在配置完四个文件后,安装一个扩展,如下:
在安装完在右上角则会出现一个三角符号,则可以直接运行程序,而不是调试,
接下来则可以在文件夹中创建一个test.cpp文件(类似的cpp文件),编写一段简单的代码,直接运行就可以了。