如何在 VS Code 中更改 c++ 版本?

问题描述 投票:0回答:2

我在 VS Code 中使用了 C++17。现在我想使用C++20或C++23,但我无法从C++17版本更改。

我尝试了很多方法,比如在Task.json中添加“-std-c++2b”或者将C++Standard设置为C++23,但我的VS Code仍然是C++17。也许我重新安装了 VS Code 并重新设置了?

visual-studio-code c++17 c++23
2个回答
1
投票

在 VSCode 项目中浏览

.vscode
文件夹,然后在
tasks.json
中键入类似以下内容(根据您的版本需求进行调整):

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-std=c++23"  // Add this line for C++23
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

还尝试配置

c_cpp_properties.json
:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "C:/MinGW/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++23"  // Change this line for C++23
        }
    ],
    "version": 4
}

最后重建你的项目。它应该对你有用。


0
投票

我用

__cplusplus
来检查c++版本,这是我使用C/C++和Code Runner扩展时的编译结果:

结果是

c++17

我尝试使用 MSYS2 中的 mingw64 运行编译:

结果是

c++20
。 另外,我使用C/C++运行器扩展来运行,结果仍然是
c++17
:

© www.soinside.com 2019 - 2024. All rights reserved.