无法使用MSVC在VS Code中编译C/C++:“cl.exe:无法将术语'cl.exe'识别为...的名称,”

btxsgosb  于 5个月前  发布在  C/C++
关注(0)|答案(5)|浏览(89)

我正在尝试使用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"
        }
    ]
}

的数据

ddrv8njm

ddrv8njm1#

cl.exe:术语“cl.exe”无法识别为小工具、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。
你的"msvc build"任务只为它的命令指定了"cl.exe",没有前导路径。问题是cl.exe不在你的PATH上,或者在VS Code运行你的构建任务时可以看到的任何地方。
一个解决方案是使用Visual Studio版本的“开发人员命令提示符”打开VS Code。此版本的命令提示符定义了Visual Studio构建工具的位置,以便从该cmd提示符运行的任何程序或命令都能够找到“cl.exe”等程序。
我更喜欢使用另一种解决方案,即为构建任务编写批处理脚本。
将此脚本放在VSCode工作区的根目录下,并将其命名为build.bat

:: set the path to your visual studio vcvars script, it is different for every version of Visual Studio.
set VS2017TOOLS="C:\Program Files(x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"

:: make sure we found them
if not exist %VS2017TOOLS% (
    echo VS 2017 Build Tools are missing!
    exit
)

:: call that script, which essentially sets up the VS Developer Command Prompt
call %VS2017TOOLS%

:: run the compiler with your arguments
cl.exe /EHsc /Zi /Fe: helloworld.exe test.c

exit

字符串
然后,您的任务必须更改为运行批处理脚本:

{
    "label": "msvc build",
    "type": "shell",
    "command": "${workspaceFolder}/build.bat",
    "group":  {
        "kind": "build",
        "isDefault": true
    },
    "presentation": {
        "reveal":"always"
    },
    "problemMatcher": "$msCompile"
}


以这种方式使用批处理脚本的优点是,您不需要从开发人员命令提示符运行VS Code,并且$msCompile问题匹配器仍然能够在VS Code中显示错误和警告。
另一个注意事项是,这个答案中的批处理脚本只针对你的项目。你可以 * 继续更新该脚本来构建你的项目,因为它变得更大,或者你可以利用像CMake这样的工具来处理从配置文件生成实际的构建脚本。
那么你的build.bat可能看起来更像这样:

:: set the path to your visual studio vcvars script, it is different for every version of Visual Studio.
set VS2017TOOLS="C:\Program Files(x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"

:: make sure we found them
if not exist %VS2017TOOLS% (
    echo VS 2017 Build Tools are missing!
    exit
)

:: call that script, which essentially sets up the VS Developer Command Prompt
call %VS2017TOOLS%

:: Set some variables for the source directory and the build directory
set SrcDir=%CD%
set BuildDir=%CD%\build

:: Make the build directory if it doesn't exist
if not exist "%BuildDir%" mkdir "%BuildDir%"

:: Make sure you configure with CMake from the build directory
cd "%BuildDir%"

:: Call CMake to configure the build (generates the build scripts)
cmake %SrcDir%^
 -D CMAKE_C_COMPILER=cl.exe^
 -D CMAKE_CXX_COMPILER=cl.exe^

:: Call CMake again to build the project
cmake --build %BuildDir%

exit

gojuced7

gojuced72#

我一直在按照Configure VS Code for Microsoft C++中的说明解决同样的问题。谢天谢地,我找到了这个问题和@Romen的伟大答案。
然而,他的解决方案中有一些令人发痒的地方:
1.他将可执行文件名和文件名硬编码,这样每次创建新项目时都必须更改。
1.他把.bat放在编译文件的文件夹中,而不是.vscode文件夹中,这样我们就不能在制作新项目时直接复制一个文件夹。
对于这两个fluxe,我修改了他的代码一点点,使它更方便地使新的项目调试与Microsoft C++。
1.在build.bat文件中,更改命令
cl.exe /EHsc /Zi /Fe: helloworld.exe helloworld.cpp

cl.exe /EHsc /Zi /Fe: %1.exe %1.cpp
1.将build.bat移动到.vscode文件夹,并将"command": "build.bat"更改为"command": ".\\.vscode\\build.bat ${fileBasenameNoExtension}"
这是微不足道的,但我希望它可以帮助像我这样的新手,并为更复杂的情况激发更好的解决方案:)

ifmq2ha2

ifmq2ha23#

对于任何来到这里的人来说,最好的解决方案是设置路径环境变量,这样cl.exe就可以在任何命令提示符或Power Shell终端中运行,这是不推荐的,根据文档:
MSVC命令行工具使用PATH、TMP、INCLUDE、LIB和LIBPATH环境变量,还使用特定于已安装工具、平台和SDK的其他环境变量。即使是简单的Visual Studio安装也可能设置20个或更多环境变量。由于这些环境变量的值特定于您的安装和您选择的生成配置,并且可以通过产品更新或升级进行更改,我们强烈建议您使用开发人员命令提示符快捷方式或自定义命令文件来设置它们,而不是在Windows环境中自行设置。
https://learn.microsoft.com/en-us/cpp/build/setting-the-path-and-environment-variables-for-command-line-builds?view=vs-2019

ggazkfy8

ggazkfy84#

什么对我起作用:
guide之后,我在task.json中做了以下更改:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "cl.exe build active file",
            "command": "build.bat", //calling build.bat instead
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

字符串
然后我的build.batsDevCmd.bat在开发者终端打开时调用):

call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"
echo Building...
cl.exe %*


然后按Ctrl + Shift + B。

bgtovc5b

bgtovc5b5#

  • VSCode* 的默认终端没有 cl.exe PATH,需要使用 Developer Command Prompt

1.打开 * 开发人员命令提示符 *
https://code.visualstudio.com/docs/cpp/config-msvc
要打开VS的开发人员命令提示符,请在Windows开始菜单中开始键入'developer',您应该会看到它出现在建议列表中。确切的名称取决于您安装的Visual Studio或Visual Studio Build Tools的版本。单击项目以打开提示符。
1.转到项目父文件夹假设它是D:\workspace,项目名称是myproject


的数据

  1. Ctrl+Shift+B构建

相关问题