无法识别 Visual Studio 代码任务构建 'C:\Program' [英] visual studio code task build 'C:\Program' is not recognized

查看:53
本文介绍了无法识别 Visual Studio 代码任务构建 'C:\Program'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用 Visual Studio Code 来构建和运行一个简单的 C++ 程序.我使用tasks.json来处理编译:(基于此:如何用VS Code和cl编译C++代码

So I am using Visual Studio Code to build and run a simple C++ program. I used the tasks.json to handle compilation: (based off of this: How to compile C++ code with VS Code and cl

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build Test",
            "type": "shell",
            "command": "cl",
            "args": [
                "/MDd",
                "/W4",
                "/EHsc",
                "/ZI",
                "/std:c++11",
                "/Od",
                "/Fe:${workspaceFolder}/Debug/test.exe",
                "/Fd:${workspaceFolder}/Debug/",
                "/Fo:${workspaceFolder}/Debug/",
                "main.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Build Release",
            "type": "shell",
            "command": "cl",
            "args": [
                "/MD",
                "/W4",
                "/EHsc",
                "/std:c++11",
                "/O2",
                "/Fe:${workspaceFolder}/Release/test.exe",
                "/Fd:${workspaceFolder}/Release/",
                "/Fo:${workspaceFolder}/Release/",
                "main.cpp"
            ]
        }
    ]
}

但是当我尝试构建时,我得到以下输出:

But when I try to build I get the following output:

 Executing task: cl /MDd /W4 /EHsc /ZI /std:c++11 /Od "/Fe:G:\My Drive\Semester 3\CS 341\Project\Project 2/Debug/test.exe" "/Fd:G:\My Drive\Semester 3\CS 341\Project\Project 2/Debug/" "/Fo:G:\My Drive\Semester 3\CS 341\Project\Project 2/Debug/" main.cpp <

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

我会注意到,我确实通过以下方式更改了设置:

I will note, I did change the settings in the following way:

"terminal.integrated.shellArgs.windows": [
        "/k",
        "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat"
    ],
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe"

我相信这就是C:\Program"的来源.我只想在 Visual Studio 中构建和执行 C++ 程序,因此将不胜感激.

I believe that is where the "C:\Program" is coming from. I just want to build and execute C++ programs in Visual Studio, so some help would be appreciated.

所以我决定把C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build添加到PATH变量中,然后我改了设置为:

So I decided to add C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Buildto the PATH variable, and then I changed the settings to:

"terminal.integrated.shellArgs.windows": [
        "/k",
        "vcvars64.bat",
    ],
 "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe"

导致错误变为:

> Executing task: cl /MDd /W4 /EHsc /ZI /std:c++11 /Od "/Fe:G:\My Drive\Semester 3\CS 341\Project\Project 2/Debug/test.exe" "/Fd:G:\My Drive\Semester 3\CS 341\Project\Project 2/Debug/" "/Fo:G:\My Drive\Semester 3\CS 341\Project\Project 2/Debug/" main.cpp <

[ERROR:vcvarsall.bat] Invalid argument found : /d
[ERROR:vcvarsall.bat] Invalid argument found : /c
[ERROR:vcvarsall.bat] Invalid argument found : cl
[ERROR:vcvarsall.bat] Invalid argument found : /MDd
[ERROR:vcvarsall.bat] Invalid argument found : /W4
[ERROR:vcvarsall.bat] Invalid argument found : /EHsc
[ERROR:vcvarsall.bat] Invalid argument found : /ZI
[ERROR:vcvarsall.bat] Invalid argument found : /std:c++11
[ERROR:vcvarsall.bat] Invalid argument found : /Od
The syntax of the command is incorrect.

推荐答案

空格和括号的问题可以用转义符来解决,它是脱字符号 (^) 在这种情况下.使用上述字符,settings.json 中的相关行将如下所示:

The issue with spaces and parentheses could be solved with the escape character which is the caret (^) in this case. Using the mentioned character the relevant rows in your settings.json would look like:

"terminal.integrated.shellArgs.windows": [
    "/k",
    "C:\\Program^ Files^ ^(x86^)\\Microsoft^ Visual^ Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat"
],
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe"

这种方法的问题与您在将 vcvars64.bat 的路径添加到 PATH 变量时遇到的相同,即 Visual Studio Code 将 append "command""args" 的值来自 tasks.json 前缀为 /d/c"terminal.integrated.shellArgs.windows" 执行任务时的值.这导致 vcvars64.bat 将获得上述值作为参数而不是 cmd.exe.出现标记为 [ERROR:vcvarsall.bat] 的错误是因为拒绝附加到错误位置的参数.

The problem with this approach is the same you've encountered when you added the path of vcvars64.bat to the PATH variable, i.e. Visual Studio Code will append the values of "command" and "args" from your tasks.json prefixed with /d and /c to the value of "terminal.integrated.shellArgs.windows" when executing your tasks. This results in that the vcvars64.bat will get the mentioned values as arguments not the cmd.exe. The errors labeled with [ERROR:vcvarsall.bat] appears because the rejection of arguments appended to the wrong place.

您可以通过为您的任务指定一个带有适当参数的 shell 来解决这个问题,如下所示:

You can solve this issue by specifying a shell with appropriate arguments to your tasks as follows:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "options": {
        "shell": {
            "executable": "C:\\Windows\\System32\\cmd.exe",
            "args": [
                "/d", "/c",
                "C:\\Program^ Files^ ^(x86^)\\Microsoft^ Visual^ Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat",
                "&&"
            ]
        }
    },
    "tasks": [
        {
            "label": "Build Test",
            "type": "shell",
            "command": "cl",
            "args": [
                "/MDd",
                "/W4",
                "/EHsc",
                "/ZI",
                "/std:c++11",
                "/Od",
                "/Fe:${workspaceFolder}/Debug/test.exe",
                "/Fd:${workspaceFolder}/Debug/",
                "/Fo:${workspaceFolder}/Debug/",
                "main.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Build Release",
            "type": "shell",
            "command": "cl",
            "args": [
                "/MD",
                "/W4",
                "/EHsc",
                "/std:c++11",
                "/O2",
                "/Fe:${workspaceFolder}/Release/test.exe",
                "/Fd:${workspaceFolder}/Release/",
                "/Fo:${workspaceFolder}/Release/",
                "main.cpp"
            ]
        }
    ]
}

另一种方法是将 "terminal.integrated.shellArgs.windows" 留空,即不要将任何参数传递给 cmd.exe 中的 >settings.json,然后将您的 tasks.json 更改如下:

Another approach is to leave the "terminal.integrated.shellArgs.windows" empty, i.e. don't pass any arguments to the cmd.exe in the settings.json, then change your tasks.json as follows:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "options": {
        "env": {
            "build": "C:\\Program^ Files^ ^(x86^)\\Microsoft^ Visual^ Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat && cl"
        }
    },
    "tasks": [
        {
            "label": "Build Test",
            "type": "shell",
            "command": "%build%",
            "args": [
                "/MDd",
                "/W4",
                "/EHsc",
                "/ZI",
                "/std:c++11",
                "/Od",
                "/Fe:${workspaceFolder}/Debug/test.exe",
                "/Fd:${workspaceFolder}/Debug/",
                "/Fo:${workspaceFolder}/Debug/",
                "main.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Build Release",
            "type": "shell",
            "command": "%build%",
            "args": [
                "/MD",
                "/W4",
                "/EHsc",
                "/std:c++11",
                "/O2",
                "/Fe:${workspaceFolder}/Release/test.exe",
                "/Fd:${workspaceFolder}/Release/",
                "/Fo:${workspaceFolder}/Release/",
                "main.cpp"
            ]
        }
    ]
}

如果您从 Visual Studio 的开发人员命令提示符启动 Visual Studio Code,则可以省略在每次构建之前调用 vcvars64.bat 的必要性.为方便起见,您可以使用以下目标创建快捷方式:

The necessity of calling the vcvars64.bat before every build can be omitted if you start Visual Studio Code from the Developer Command Prompt of Visual Studio. For convenience, you can create a shortcut with the following tartget:

cmd /d /c "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat" && start /b code

这将使开发人员命令提示符保持打开状态,可以按需关闭.

This will leave the Developer Command Prompt open which can be closed on demand.

这篇关于无法识别 Visual Studio 代码任务构建 'C:\Program'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆