Cygwin GCC C++编译器 - 为什么./?

发布于 2024-10-12 13:13:11 字数 1459 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

二货你真萌 2024-10-19 13:13:11

通常是因为聪明的人在路径上没有当前目录 . :-) 路径

是一个环境变量,例如 /bin:/usr/bin:/usr/sbin,它是一个目录列表,用于查找可执行文件,例如当您键入 hello 时。

与 Windows 不同,许多 UNIX shell 不会自动在当前目录中搜索可执行文件。它们必须列在路径中,否则它们不会运行。

这是因为不这样做实际上是一种攻击媒介。例如,如果您在主目录中创建一个 ls 程序,并告诉其中一位管理员其中有一个有趣的文件,他们可能会转到您的目录并输入 ls看看里面有什么。

对于那些在 ls 的“真实”位置之前拥有当前目录的愚蠢管理员来说,他们现在受到了威胁,因为您的代码正在以他们的全部权限运行。

这就是为什么他们倾向于不这样做。

有些人(不是我)会将 . 放在他们的路径上以使他们的生活更轻松,但即使这样,他们也会将其放在最后,以便搜索其他位置第一的。

管理员没有那么值得信任的奢侈。

Usually because intelligent people don't have their current directory . on the path :-)

The path is an environment variable like /bin:/usr/bin:/usr/sbin, and it's a list of directories to look in for finding executables, such as when you type in hello.

Unlike Windows, many UNIX shells don't automatically search the current directory for an executable. They must be listed in the path otherwise they are not run.

That's because to do otherwise is actually an attack vector. For example, if you create an ls program in your home directory and tell one of the administrators that there's a funny file in there, they may go to your directory and enter ls to see what's in there.

For a silly administrator that has the current directory before the "real" location of ls, they are now compromised, because your code is running with their full privileges.

That's why they tend not to do that.

Some people (not I) will put . on their path to make their lives easier but, even then, they'll put it at the end so that other locations are searched first.

Administrators don't have the luxury of being that trusting.

请远离我 2024-10-19 13:13:11

因为当前工作目录不在PATH中?

或者至少,这就是 Unix 风格系统上的设置方式,我认为 CYGWIN 也是如此。

Because the current working directory is not in the PATH?

Or at least, that's how things are setup on Unix-style systems, I assume CYGWIN does the same.

荭秂 2024-10-19 13:13:11

在 Windows 上,当前目录始终位于可执行文件的搜索路径中。搜索顺序是“在当前目录中查找,如果没有找到,则在PATH环境变量列出的目录中查找”。

来自 MS 网站:

操作系统总是搜索
首先在当前目录中之前
它搜索目录
命令路径。

(这使得这里所有关于不将 . 放入 PATH 的警告变得无关紧要,恕我直言)

在 Linux 上情况并非如此(对于当前目录)。因此,要运行当前目录中的可执行文件,您需要编写 ./exe_name

众所周知,Cygwin 适用于 Windows,因此不需要 ./,并且似乎只是复制/粘贴或保留编写者习惯的 unix 风格。

编辑:这是命令处理器(shell)的问题,正如评论中指出的以及我在下面解释的那样,所以如果您在 Windows 上使用类 Unix 的 shell,您仍然可能需要这个风格。

编辑:详细阐述 .\

. (确切地说不是 ./)是 的别名>当前目录。在 Unix 上,每个新创建的目录并非“天生”为空,而是包含 2 个子目录:.(自引用)和 ..(对目录的引用)。父目录。与其他目录一样,两者都只是常规目录。当您运行 ls 命令时(与 Windows 上的 dir 相同),您看不到它们,因为以 . 开头的名称在某种意义上是特殊的默认情况下不显示它们。但是,您可以通过 ls -a 来查看它们。

当您在提示符下运行命令时,如果该命令只是一个(文件)名称,系统(实际上是 shell)会在 PATH 中搜索具有该名称的文件。

如果命令包含路径(不一定是绝对路径,例如subdir1/exe),系统将在您指定的位置查找可执行文件。因此,编写./exe表示当前目录中的文件exe

On Windows, the current directory is always in the search path for an executable. The search order is "look in the current dir, if not found, look in the directories listed in the PATH environment variable".

From MS site:

The operating system always searches
in the current directory first, before
it searches the directories in the
command path.

(which makes all the warning here of not putting the . in your PATH irrelevant, IMHO)

On Linux this is not the case (for current dir). So, to run an executable which is in your current dir you need to write ./exe_name.

As Cygwin, again AFAIK, is for Windows, the ./ is not needed and seems to be just a copy/paste or preserving the unix-style the writer is used to.

EDIT: this is the issue of the command processor (the shell) as pointed out in comments and as I explain below, so if you are using a Unix-like shell on Windows, you still may need this style.

EDIT: elaborating on .\

. (not ./ to be exact) is an alias to the current directory. On Unix, every newly created directory is not "born" empty but contains 2 children: ., which is a self-reference, and .. which is a reference to the parent directory. Both are just regular directories, as any other. You don't see them when you run the ls command (same as dir on Windows) because names starting with . are special in the sense that they are not displayed by default. However, you can see them by ls -a.

When you run a command at the prompt, if the command is only a (file) name, the system (actually, the shell) searches the PATH for the file with this name.

If the command contains a path (not necessarily an absolute path, e.g. subdir1/exe) the system looks for the executable where you specified. Hence, writing ./exe means file exe in the current dir.

倾听心声的旋律 2024-10-19 13:13:11

Cygwin 是一个类似 Unix 的运行时环境,因此遵循在此类环境中搜索可执行文件的路径的方式。 Unices 的默认可执行文件搜索路径包含当前目录。因此,如果要运行不在 PATH 中设置的目录之一中的可执行文件,则必须给出完整路径。 ./ 是当前目录的简写,也称为进程工作目录(pwd)。请注意,将 pwd 包含在可执行搜索路径中是一个非常糟糕的主意。

Cygwin is a Unix-like runtime environment and as such follows the way paths are searched for executables in such environments. The default executable search path of Unices does not contain the current directory. Thus if one wants to run an executable not located in one of the directories set in PATH a full path must be given. ./ is a shorthand for the current directory, also called process working directory (pwd). Be advised that it's a very bad idea to have the pwd being included in the executable search path.

ゞ花落谁相伴 2024-10-19 13:13:11

Cygwin 遵循 Unix 对当前工作目录中执行文件的限制。在 Unix 风格的终端环境中,如果要从当前目录执行可执行文件,则必须在前面添加 ./。这是因为当前目录“.”不是 PATH 环境的一部分,以限制恶意软件造成的损害。 Cygwin 只是遵循这个约定,它与 C++ 程序没有任何关系

Cygwin follows the Unix limitations on executing files in the current working directory. In Unix style terminal environments an executable must have ./ prepended if it is to be executed from the current directory. This is because the current directory "." is not part of the PATH environmment in order to limit the damage done by malware. Cygwin is simply following this convention, it has nothing per say to do with C++ programs

妳是的陽光 2024-10-19 13:13:11

这只是 shell 中“path”或“PATH”变量的问题。 (可能你的 shell 是 bash,所以它是 PATH。)

echo $PATH

执行你想要的操作的典型“用户”路径将以“.”开头。作为路径元素。当然,这是一个较小的安全风险。

That's just an issue with your 'path' or 'PATH' variable in your shell. (probably your shell is bash, so it'd be PATH.)

echo $PATH

A typical 'user' path to do what you want would start with "." as a path element. This is a minor security risk of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文