我尝试通过 YouTube 视频为 VS Code 设置 C++,但它们已经很老了。因此,c_cpp_properties.json 和task.json 中存在配置错误。
最佳答案
- 下载并安装 MinGW-w64
- 安装“C/C++ for Visual Studio Code”扩展
这都是针对 Window 操作系统的设置。对于其他操作系统,您只需更改 c_cpp_properties.json configuration
c_cpp_properties.json 文件的示例(您必须添加自己的 MinGW-w64 的路径):
{
"configurations":
[
{
"name": "Win64",
"includePath": [
"${workspaceFolder}",
"C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0",
"C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\i686-w64-mingw32\\include"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"compilerPath": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin",
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"${workspaceFolder}",
"C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0",
"C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\i686-w64-mingw32\\include"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 3
}
task.json 文件示例:
{
"version": "2.0.0",
"tasks": [
{
"label": "debug",
"type": "shell",
"command": "",
"args": ["g++","-g", "${relativeFile}", "-o","a.exe"]
},
{
"label": "Compile and run",
"type": "shell",
"command": "",
"args": [
"g++","-g", "${relativeFile}", "-o","${fileBasenameNoExtension}", "&&" , "./${fileBasenameNoExtension}"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
您可以使用 Ctrl+Shift+b 构建 C++ 程序。
关于c++ - 如何在 VS Code 中为 C++ 开发进行初始设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53909659/