我正在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/