指定 cl.exe 的命令行 C++ 版本 (Visual Studio Code)

Ala*_*inD 2 c++ cl visual-studio-code

我正在 Visual Studio Code 中测试 C++17 Fallthrough属性。IDE 已配置为使用 Microsoft Visual Studio cl.exe 编译器编译 C/C++ 代码。我在调试模式下tasks.json构建简单文件的任务定义(在 )是:.cpp

{
    "type": "shell",
    "label": "cl.exe: Build active file",
    "command": "cl.exe",
    "args": [
        "/Zi",
        "/EHsc",
        "/Fe:",
        "${file}",
        "/link",
        "/OUT:${fileDirname}\\${fileBasenameNoExtension}.exe"
    ],
    "options": {
        "cwd": "${workspaceFolder}"
    },
    "problemMatcher": [
        "$msCompile"
    ]
}
Run Code Online (Sandbox Code Playgroud)

这已经在多个程序上进行了测试并且效果良好。现在我包含一个switch使用新[[fallthrough]];属性的语句,编译器会生成:

warning C5051: attribute 'fallthrough' requires at least '/std:c++17'; ignored
Run Code Online (Sandbox Code Playgroud)

添加"/std:c++17",到 cl.exe 的“args”没有任何改变(生成相同的编译器警告)。这是新版本:

"args": [
    "/Zi",
    "/EHsc",
    "/Fe:",
    "/std:c++17",
    "${file}",
    "/link",
    "/OUT:${fileDirname}\\${fileBasenameNoExtension}.exe"
],
Run Code Online (Sandbox Code Playgroud)

据我所知,根据Microsoft指定语言标准的文档,我的语法是正确的。

我究竟做错了什么?

und*_*Hao 7

我在搜索其他内容时发现了这个问题,但这里有一个修复程序:)。

您的问题是您提供参数的顺序。/Fe:期望文件路径直接在 - https://learn.microsoft.com/en-us/cpp/build/reference/fe-name-exe-file?view=msvc-160之后

args这是取自 VSCode文档的示例部分,但我添加了/std:c++17编译器标志

"args": [
    "/Zi",
    "/EHsc",
    "/std:c++17", // <= put your compiler flag here
    "/Fe:", // <= /Fe: followed by the path + filename
    "${fileDirname}\\${fileBasenameNoExtension}.exe",
    "${file}"
]
Run Code Online (Sandbox Code Playgroud)

希望这有帮助,编码愉快!