/ vs. \到CMD和C系统功能中可执行文件的相对路径

cic

考虑Windows 10上的以下CMD会话,以#注释开头的行

# We have this simple program
D:\testdir>type prg.c
#include <stdio.h>

int main() {
  printf("Hello prg");
  return 0;
}
# This is "realgcc.exe (Rev2, Built by MSYS2 project) 6.2.0"
D:\testdir>gcc prg.c -o prg
D:\testdir>prg.exe
Hello prg
D:\testdir>md dir
D:\testdir>cd dir
D:\testdir\dir>..\prg.exe
Hello prg
# This does not work however
D:\testdir\dir>../prg.exe
'..' is not recognized as an internal or external command,
operable program or batch file.
# But this does
D:\testdir\dir>"../prg.exe"
Hello prg
# Now we want to call the same program, from the same directory,
# but from C instead, so we create this file
D:\testdir\dir>type call.c
#include <stdlib.h>

int main() {
  system("..\\prg.exe");
  return 0;
}
D:\testdir\dir>gcc call.c -o call
D:\testdir\dir>call.exe
Hello prg
# Now to the question: If we modify the system function call
# to the following -- why doesn't it work?
D:\testdir\dir>type call.c
#include <stdlib.h>

int main() {
  system("\"../prg.exe\"");
  return 0;
}
D:\testdir\dir>gcc call.c -o call
D:\testdir\dir>call.exe
'..' is not recognized as an internal or external command,
operable program or batch file.

因此,显然调用system与在CMD会话中交互式运行命令不同吗?报价是否由于某些原因而被删除?是否存在另一种语法,可以使用包含使用/而不是\作为分隔符的相对路径来调用其他目录中的可执行文件

(我在尝试使Cygwin程序和通常的Windows程序相互通信时注意到这一点。)

编辑:显然,这工作:

#include <stdlib.h>

int main() {
  system("\"\"../prg.exe\"\"");
  return 0;
}

在CMD中可能相关

cmd /c ""../prg.exe""

可行,但这不可行

cmd /c "../prg.exe"

因此,似乎出于某种原因在system通话中需要一些额外的报价吗?

cic

感谢您提供所有有用的评论,我现在了解发生了什么。system在询问此问题时,我没有考虑命令(即的参数)实际上是如何传递给“命令解释器”的,因此考虑理解外部引号为何消失至关重要。

在Windows上,调用与使用参数system("command")产生新cmd.exe进程本质上是相同的(在Linux和类似的操作系统上,我们将与等效。)/ccommandsh -c command

但是,这里的问题是,在大多数情况下,当命令通过/c标志传递时,Windows的CMD会删除第一个和最后一个引号字符这在以下文档给出的文档中进行了说明cmd /?

[...]

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

1.  If all of the following conditions are met, then quote characters
    on the command line are preserved:

    - no /S switch
    - exactly two quote characters
    - no special characters between the two quote characters,
      where special is one of: &<>()@^|
    - there are one or more whitespace characters between the
      two quote characters
    - the string between the two quote characters is the name
      of an executable file.

2.  Otherwise, old behavior is to see if the first character is
    a quote character and if so, strip the leading character and
    remove the last quote character on the command line, preserving
    any text after the last quote character.

[...]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++中的翻译单元vs编译单元vs目标文件vs可执行文件vs ...

如何在VS 2012中设置HTML和CSS中的相对路径和文件夹路径?

不管可执行文件的位置如何,如何使用相对路径访问项目目录中的文件?

在带有符号链接的可执行文件中使用相对路径

通过相对路径中的守护程序systemd启动一个dotnet核心可执行文件

Windows上的Ubuntu作为VS Code终端-可执行文件路径

Webpack 相对路径 vs 绝对路径

Xcode:如何将当前工作目录设置为可执行文件的相对路径?

为什么File.OpenRead()将相对路径附加到可执行文件?

Spring Boot classpath:vs相对路径

VS 代码中的相对路径不一致

使用VS2010构建的可执行文件在System32中找不到DLL

如何在 VS2015 中更改特定配置的可执行文件名称

cmake-是否可以在运行时将可执行文件链接到具有相对路径的共享库?

命令行长度限制:内置vs可执行文件

.NET便携式可执行文件VS .NET程序集

如何使用相对路径到执行文件而不是python命令路径导入其他文件?

找不到与命令dotnet-projectmodel-server,VS2015匹配的可执行文件

Visual Studio 2019 调试嵌入式目标。如何强制 VS 不将其视为 Windows 可执行文件

为什么我使用 Cmake 构建和 VS 2019 构建时相对路径不同?

告诉VS Code始终为TypeScript自动导入使用相对路径?

如何使用相对路径向VS项目添加图标

为什么我使用 Cmake 构建和 VS 2019 构建时相对路径不同?

相对路径的 VS Code 片段正则表达式

路径可执行文件的CMD完成

Popen 预置路径到可执行文件

Graphviz报告虽然在系统路径中找不到可执行文件

vim和sudo可执行文件的路径

“ bash可执行文件”和“ bash -c可执行文件”之间的区别