我正在尝试使用 cl(通过 Visual Studio 2019 安装)在 VS Code 中编译 C/C++ 代码。我已经按照 MS 网站的建议设置了 json 文件,
https://code.visualstudio.com/docs/cpp/config-msvc ,
但我仍然收到错误:
cl.exe : The term 'cl.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
这是我的json文件:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.21.27702/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
{
"version": "2.0.0",
"tasks": [
{
"label": "msvc build",
"type": "shell",
"command": "cl.exe",
"args": [
"/EHsc",
"/Zi",
"/Fe:",
"helloworld.exe",
"test.c"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal":"always"
},
"problemMatcher": "$msCompile"
}
]
}
原文由 port80 发布,翻译遵循 CC BY-SA 4.0 许可协议
您的
"msvc build"
任务仅为其命令指定"cl.exe"
,没有前导路径。问题是cl.exe
不在您的 PATH 上,或者 VS Code 在运行构建任务时可以看到的任何地方。一种解决方案是使用“开发人员命令提示符”为您拥有的任何 Visual Studio 版本打开 VS Code。此版本的命令提示符定义了 Visual Studio 构建工具的位置,以便从该 cmd 提示符运行的任何程序或命令都能够找到像“cl.exe”这样的程序。
我更喜欢使用另一种解决方案,即为您的构建任务编写批处理脚本。
将此脚本放在 VSCode 工作区的根目录中,并将其命名为
build.bat
:然后必须更改您的任务以运行批处理脚本:
以这种方式使用批处理脚本的好处是您不需要从开发人员命令提示符运行 VS Code,而且
$msCompile
问题匹配器仍然能够在 VS Code 中显示错误和警告。另一个注意事项是此答案中的批处理脚本仅针对您的项目。随着项目变大,您 可以 继续更新该脚本以构建您的项目,或者您可以利用 CMake 之类的工具来处理从配置文件生成实际构建脚本。
那么你的
build.bat
可能看起来更像这样: