c++ - 如何在 VS Code 中为 C++ 开发进行初始设置?

标签 c++ c visual-studio-code

我尝试通过 YouTube 视频为 VS Code 设置 C++,但它们已经很老了。因此,c_cpp_properties.jsontask.json 中存在配置错误。

最佳答案

  1. 下载并安装 MinGW-w64
  2. 安装“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/

相关文章:

c - 这段 Ragel 代码有什么作用?

c++ - 使用std和其他库的VS Code自动完成

c++ - 模板什么时候结束?

c++ - 如何确定一个整数需要多少字节?

c - 为什么在尝试打印空指针时出现错误

c - 将某些内容放入我的数组时出错。 C

typescript - 在 VSCode 中禁用 TSLint

visual-studio-code - 我可以查看并单独禁用扩展程序(例如 Sublime Text Keymap 扩展程序)的键盘快捷键吗?

c++ - snd_pcm_hw_params_set_period_size_near 没有返回好的值

c++ - Qt 与 Dlib 和 CUDA