如何在Windows Powershell中运行带有空格的可执行文件(.exe)?

时间:2018-05-06 13:47:43

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

最近我想试用Visual Studio Code for C ++编程。由于我是一个相当新的C ++和编程,所以我不想做运行程序的繁琐的设置过程。所以我安装了扩展程序" Code Runner"它在终端中运行cpp文件。在我想要运行带有空格的cpp文件之前,这一切都很顺利。经过一番搜索,我发现扩展使用某种批处理文件来更改终端的目录,编译它并运行输出文件。当名称中有空格时,名称的2个部分被解释为单独的命令。这是VS代码设置中的code-runner.executorMap设置:

"code-runner.executorMap": {
    "javascript": "node",
    "php": "C:\\php\\php.exe",
    "python": "python",
    "perl": "perl",
    "ruby": "C:\\Ruby23-x64\\bin\\ruby.exe",
    "go": "go run",
    "html": "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"",
    "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
    "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
    "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",

这个输出是:

PS D:\> cd "d:\" ; if ($?) { g++ Test  1.cpp -o Test  1 } ; if ($?) { .\Test  1 }
g++.exe: error: Test: No such file or directory
g++.exe: error: 1.cpp: No such file or directory
g++.exe: error: 1: No such file or directory

我设法通过用双引号括起名称和目录来解决一些问题。

        "c": "cd $dir && gcc \"$fileName\" -o \"$fileNameWithoutExt\" && \"./$fileNameWithoutExt\"",
        "cpp": "cd $dir && g++ \"$fileName\" -o \"$fileNameWithoutExt\" && \"./$fileNameWithoutExt\"",
        "objective-c": "cd $dir && gcc -framework Cocoa \"$fileName\" -o \"$fileNameWithoutExt\" && \"./$fileNameWithoutExt\"",}

输出结果为:

PS D:\> cd "d:\" ; if ($?) { g++ "Test  1.cpp" -o "Test  1" } ; if ($?) { "./Test  1" }
./Test

" cd"命令和" g ++"命令似乎有效,但实际执行程序的部分出现故障。它将双引号文件名解释为字符串,只显示它而不是运行它。我在互联网上搜索了运行.exe文件的解决方案,其中包含名称中的空格但无法找到任何文件。如果主题不相关或由于我的无能而感到抱歉,因为我对编程很陌生。

0 个答案:

没有答案
相关问题