参考链接: link
前言
vscode可以当成一种强大的编辑器,只能够写代码(像VS这种IDE是集成编辑器、编译器,调试等等所有环境一起)。想要运行代码,还需要配置编译器,C++常用的编译器为MinGW,下载后将其配置到系统环境变量当中即可。最后在vscode中调用编译器即可运行。
总结步骤:获取编辑器 【vscode】-> 获取编译套装(编译器、调试器、头文件库等) 【MinGW】-> 做好两者之间的沟通工作(配置文件)
当然,还要安装C/C++的各种插件,这些丰富且强大的插件也是vscode受欢迎的原因之一。
一、配置文件
如果想在vscode软件界面上进行配置,大概步骤:按快捷键Ctrl+Shift+P调出命令面板,输入C/C++,选择“Edit Configurations(UI)”进入配置。主要配置两个选项:
(1)编译器路径:如C:\xxxxxx\mingw64\bin\g++.exe
(2)IntelliSense模式:如选gcc-x64。
这样就会生成在父目录下生成一个.vscode目录,并且里面有一个c_cpp_properties.json文件,里面就是刚才选择的配置参数。当然,再通过其他操作,可生成其他的json配置文件
1、.vscode
.vscode 文件夹中的配置文件是用于配置 Visual Studio Code(VS Code)编辑器的设置和扩展。以下是各个配置文件的作用:
- c_cpp_properties.json:配置编译器环境。
- tasks.json:调用g++编译器,基于源代码创建可执行文件(编译)。
- launch.json:用于配置调试器的启动选项。定义不同的调试配置,包括要调试的程序、运行参数、环境变量等。
- settings.json(不用创建也行): 这个文件允许自定义 VS Code 的编辑器设置。可以在其中指定首选项,例如缩进类型、字体大小、文件关联等。
更直接的方法是手动在父目录下创建一个.vscode文件夹,在里面复制各种json文件(但要注意修改相关内容),vscode会根据这些json文件自动完成配置。下面给出自己使用的json文件。
(1)c_cpp_properties.json
{
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64",
"compilerPath": "C:\\Users\\Administrator\\minGW\\mingw64\\bin\\g++.exe"
}
],
"version": 4
}
(2)tasks.json
{
"tasks": [
{
// "type": "cppbuild",
"type": "shell",
"label": "C/C++: g++.exe",
"command": "C:\\Users\\Administrator\\minGW\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g", // 添加gbd调试选项
"${file}",
//"${fileDirname}\\*.cpp",
"-o", // 指定生成可执行文件的路径
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Users\\Administrator\\minGW\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
(3) launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb)", // 配置名称
"type": "cppdbg", // 配置类型
"request": "launch", // 请求配置类型
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",//要调试的文件
"args": [], //程序调试时传递给程序的命令行参数,空即可
"stopAtEntry": false, // 为true时程序将暂停在入口处
"cwd": "${workspaceFolder}", // 当前工作路径:当前文件所在的工作空间
"environment": [], // 环境变量,空即可
"externalConsole": true, // 为true时使用单独的cmd窗口
"MIMode": "gdb",
"miDebuggerPath": "C:\\Users\\Administrator\\minGW\\mingw64\\bin\\gdb.exe",//调试器路径
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask":"C/C++: g++.exe"//与task.json的label要一致
}
]
}
(4) settings.json
{
"files.associations": {
"iostream": "cpp",
"iosfwd": "cpp",
"new": "cpp",
"*.tcc": "cpp",
"limits": "cpp",
"type_traits": "cpp",
"array": "cpp",
"atomic": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"cfenv": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cuchar": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"utility": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"istream": "cpp",
"mutex": "cpp",
"ostream": "cpp",
"scoped_allocator": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"valarray": "cpp"
},
"workbench.preferredHighContrastLightColorTheme": "Default Dark+"
}