vscode配置开发环境
简单来说,vscode类似一款文本编辑器,不是IDE,想要运行C/C++代码,得配置一下,配置文件有三个,
关于一些插件的安装,这里不在叙述,可以直接百度,本文主要说明配置文件。
1.在你写代码的地方新建一个文件夹,并命名为“.vscode”。然后再在“.vscode”文件夹下新建三个文件,分别为:(或者当你新建一个工作空间时,会自动生成.vscode文件)
1. tasks.json
2. launch.json
3. c_cpp_properties.json
2.编写 tasks.json 文件
该文件文件来告诉VS Code如何构建(编译)程序
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"${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
}
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "new", //这里shared表示共享,改成new之后每个进程创建新的端口
"showReuseMessage": true,
"clear": false
}
}
3.编辑 launch.json 文件
该文件负责配置VS Code。
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录 workspaceRoot已被弃用,现改为workspaceFolder
"environment": [],
"externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
"MIMode": "gdb",
"miDebuggerPath": "E:\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
]
}
]
}
编辑 c_cpp_properties.json 文件
该文件用于进一步控制C / C ++扩展。它将允许您更改设置,例如编译器的路径,包含路径,C ++标准(默认为C ++ 17)等。
需要说明:这个文件是非必须的,可以没有!
{
"configurations": [
{
"name": "Win32",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"compilerPath": "E:\\MinGW\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x86"
}
],
"version": 4
}
附注:配置好了记得重启!重启可以解决 99% 的问题!
另外需要注意的是:代码别写错!
附录
c_cpp_properties.json 文件中 includePath 路径的获取
打开CMD命令行工具,输入gcc -v -E -x c++ -。
需要说明:在 includePath 的第一行配置的是 MinGW根目录下的 include文件夹。