gpt4 book ai didi

c++ - vscode-cpptools 错误 : "cl.exe build and debug is only usable when VS Code is run from the Developer Command Prompt for VS."

转载 作者:行者123 更新时间:2023-12-03 08:03:01 70 4
gpt4 key购买 nike

我正在使用带有 VS Code 的 vscode-cpptools 扩展的 Visual Studio 工具链,但出现以下错误:

cl.exe build and debug is only usable when VS Code is run from the Developer Command Prompt for VS.

错误原因是什么以及如何解决?

最佳答案

阅读the docs for configuring VS Code for Microsoft C++ .

在显示以下错误的屏幕截图下找到以下摘录:

"cl.exe build and debug is only usable when VS Code is run from the Developer Command Prompt for VS."

If you get an error trying to build and debug with cl.exe, make sure you have started VS Code from the Developer Command Prompt for Visual Studio using the code . shortcut.

每个版本的 Visual Studio 构建工具都附带一个开发人员提示,用于设置使用该版本工具的环境。例如,它将目录放入其环境的 PATH 中,以便可以轻松访问它们。使用 Visual Studio 构建工具的配置中的一些其他工具(例如 VS Code)可能期望从该环境运行。

https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line#how-to-use-the-command-line-tools中有一些很好的相关信息:

To work correctly, the tools require several specific environment variables to be set. These variables are used to add the tools to the path, and to set the locations of include files, library files, and SDKs. To make it easy to set these environment variables, the installer creates customized command files, or batch files, during installation. You can run one of these command files to set a specific host and target build architecture, Windows SDK version, and platform toolset. For convenience, the installer also creates shortcuts in your Start menu. The shortcuts open developer command prompt windows by using these command files for specific combinations of host and target. These shortcuts ensure all the required environment variables are set and ready to use.

The required environment variables are specific to your installation and to the build architecture you choose. They also might be changed by product updates or upgrades. This variability is one reason why we recommend you use an installed command prompt shortcut or command file, instead of setting the environment variables yourself.

启动 VS Code 时执行此操作可能会很痛苦,但如果您安装了多个 Visual Studio 并且想要使用特定的构建工具在 VS Code 中进行开发,我不知道如何改进这一点。

如果您不介意让整个 VS Code 进程从 VS 开发人员提示符的环境开始,并且您只关心在tasks.json 中定义的构建任务是否有效,那么您可以使用为工作区硬编码特定的开发人员提示环境,您可以按照 https://code.visualstudio.com/docs/cpp/config-msvc#_run-vs-code-outside-the-developer-command-prompt 中的说明进行操作。 。阅读文档以获取完整信息和注意事项,但长话短说,请将类似以下内容(根据需要调整)放在tasks.json 的顶层:

"windows": {
"options": {
"shell": {
"executable": "cmd.exe",
"args": [
"/C",
// The path to VsDevCmd.bat depends on the version of Visual Studio you have installed.
"\"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools/VsDevCmd.bat\"",
"&&"
]
}
}
},

您还可以尝试其他技巧。请参阅https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line#developer_command_file_locations ,它提供了命令文件的位置,您可以使用这些文件来修改现有命令提示符的环境,而不是使用预构建的开发人员命令提示符之一。例如,编写一个脚本文件,仅运行这些命令文件之一来设置选定的开发人员环境,然后运行 ​​code,将其余命令行参数传递给 code.然后,您可以将该脚本文件固定到某个易于访问的位置,或通过命令行使用它。例如,参见Run VScode from Dev Console through Context Menu .

请注意,如果您使用的是 CMake,则 CMake 工具扩展具有 a "kits" feature (另见What are the differences between the scanned-for kits on my Windows machine using the VS Code cmake-tools extension?)和supports CMake presets ,两者都有处理此类内容的机制(有关预设,请参阅 configure presets 的“架构”和“工具集”属性)。

关于c++ - vscode-cpptools 错误 : "cl.exe build and debug is only usable when VS Code is run from the Developer Command Prompt for VS.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73347313/

70 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com