c++ - 指定cl.exe的命令行C++版本(Visual Studio代码)

标签 c++ visual-studio-code cl

我正在Visual Studio Code中测试C++ 17 fallthrough属性。 IDE已配置为使用Microsoft Visual Studio cl.exe编译器编译C / C++代码。我在DEBUG模式下构建简单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"
    ]
}
这已经在多个程序上进行了测试,并且效果很好。现在,我使用新的switch属性包括一个[[fallthrough]];语句,编译器将生成:
warning C5051: attribute 'fallthrough' requires at least '/std:c++17'; ignored
"/std:c++17",添加到cl.exe的“args”中并没有改变(生成相同的编译器警告)。这是新版本:
"args": [
    "/Zi",
    "/EHsc",
    "/Fe:",
    "/std:c++17",
    "${file}",
    "/link",
    "/OUT:${fileDirname}\\${fileBasenameNoExtension}.exe"
],
据我所知,根据用于指定语言标准的Microsoft documentation,我的语法是正确的。
我究竟做错了什么?

最佳答案

我在寻找其他东西时发现了这个问题,但这是为您解决的问题:)。
您的问题是提供参数的顺序。 /Fe:期望紧随其后的文件路径-https://docs.microsoft.com/en-us/cpp/build/reference/fe-name-exe-file?view=msvc-160
这是取自VSCode documentation的示例args部分,但我添加了/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}"
]
希望对您有所帮助,编码愉快!

关于c++ - 指定cl.exe的命令行C++版本(Visual Studio代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62915524/

相关文章:

c++ - 在 opencv for c 中,什么函数做与 Mat::convertTo 和 cvtColor() 完全相同的事情

windows - 如何让 Clangd 在 Windows 上与 VS Code 一起正常工作

c++ - 修改task.json以编译多个c++文件

visual-studio-2017 - 命令行警告D9035:不建议使用 'Gm'选项,并将在以后的版本中将其删除

c++ - 创建一个包含其父类的所有功能的类,除了一个

c++ - OpenGL 动画似乎偶尔会丢失骨骼数据

c++ - 在C++中实现Interface类的创建函数

python - VSCode Python 交互式窗口 - 如何停止 Jupyter 服务器?

c - 使用gdb调试exe

powershell - 使用 cl 编译器选项在不同目录中编译对象