首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法识别visual代码任务生成'C:\Program‘

无法识别visual代码任务生成'C:\Program‘
EN

Stack Overflow用户
提问于 2018-09-22 18:28:12
回答 2查看 5.7K关注 0票数 3

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

代码语言:javascript
运行
复制
{
    // 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"
            ]
        }
    ]
}

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

代码语言:javascript
运行
复制
 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.

我要指出,我确实以下列方式更改了设置:

代码语言:javascript
运行
复制
"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:\程序”就是从这里来的。我只想在Visual中构建和执行C++程序,所以希望得到一些帮助。

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

代码语言:javascript
运行
复制
"terminal.integrated.shellArgs.windows": [
        "/k",
        "vcvars64.bat",
    ],
 "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe"

这导致错误变成:

代码语言:javascript
运行
复制
> 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.
EN

回答 2

Stack Overflow用户

发布于 2019-03-02 15:59:56

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

代码语言:javascript
运行
复制
"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代码将从以/d/c为前缀的tasks.json中将"command""args"的值附加到< Code >D14的值中。这将导致vcvars64.bat将上述值作为参数,而不是cmd.exe。标记为[ERROR:vcvarsall.bat]的错误出现是因为对参数的拒绝附加到了错误的位置。

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

代码语言:javascript
运行
复制
{
    // 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

代码语言:javascript
运行
复制
{
    // 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的Developer命令提示符启动Visual代码,则可以省略在每次生成之前调用vcvars64.bat的必要性。为了方便起见,可以使用以下tartget创建快捷方式:

代码语言:javascript
运行
复制
cmd /d /c "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\VsDevCmd.bat" && start /b code

这将使Developer命令提示符处于打开状态,可以按需关闭。

票数 4
EN

Stack Overflow用户

发布于 2021-06-22 06:41:23

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

当我改变的时候

代码语言:javascript
运行
复制
"java.home": "C:\\Program Files\\JDK\\11" to
"java.home": "C:\\PROGRA~1\\JDK\\11"

它开始工作了

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52459661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档