【问题标题】:visual studio code task build 'C:\Program' is not recognized无法识别 Visual Studio 代码任务构建“C:\Program”
【发布时间】:2018-09-22 18:28:12
【问题描述】:

所以我正在使用 Visual Studio Code 来构建和运行一个简单的 C++ 程序。我使用 tasks.json 来处理编译:(基于此: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"
            ]
        }
    ]
}

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

 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.

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

"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++ 程序,因此不胜感激。

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

"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.

【问题讨论】:

  • 听起来你需要弄清楚如何在文件路径中传递空格。或者只是使用旧 8.3 天中的“短”路径名。
  • 从来没有看到需要这样做,因为设置的更改有效。你会建议我做什么?
  • "\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat\""
  • 对不起,我不知道。除非必须,否则我不会使用 Windows——我是 Unix 人。我只是在插话问题似乎是什么。但同样,如果不亲自探索它,我真的不知道。也许尝试在整个路径周围加上转义引号?类似“terminal.integrated.....”...“\”C:\\Program Files (x86)\\Mic.....vcvars64.bat\""。或者也许逃避路径中的空间会起作用?
  • "\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat\"" 给出以下错误:'\"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat\"' 不是内部或外部命令、可运行程序或批处理文件.

标签: c++ windows visual-studio-code


【解决方案1】:

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

"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 将追加 "command" 的值和"args" 从你的tasks.json 前缀/d/c"terminal.integrated.shellArgs.windows" 的值在执行你的任务时。这导致vcvars64.bat 将获得提到的值作为参数而不是cmd.exe。出现标有[ERROR:vcvarsall.bat] 的错误是因为拒绝附加到错误位置的参数。

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

{
    // 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"留空,即不要将任何参数传递给settings.json中的cmd.exe,然后按如下方式更改tasks.json

{
    // 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 的必要性。为方便起见,您可以使用以下目标创建快捷方式:

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

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

【讨论】:

    【解决方案2】:

    文件夹名称中的空格会破坏提示,因此请确保在您的 settings.json 文件中将路径“Program Files”更新为简单的“PROGRA~1”

    例如当我改变时

    "java.home": "C:\\Program Files\\JDK\\11" to
    "java.home": "C:\\PROGRA~1\\JDK\\11"
    

    它开始工作了

    【讨论】:

    • 不错,这个效果很好
    猜你喜欢
    • 2022-08-12
    • 2019-04-11
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多