Visual Studio代码任务构建'C:\ Program'无法识别

问题描述 投票:1回答:1

所以我使用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\\Buildto 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.
c++ windows visual-studio-code
1个回答
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"留空,即不将任何参数传递给cmd.exe中的settings.json,然后按如下方式更改你的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的Developer Command Prompt启动Visual Studio Code,则可以省略在每次构建之前调用vcvars64.bat的必要性。为方便起见,您可以使用以下tartget创建快捷方式:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.