Cygwin GCC C++ compiler - Why ./? [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this questionIf you look at point (6) here: http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/cygwin/part3/
Why should we type ./ before the .exe file in order for it to run?
Why cannot we type hello.exe immediately?
Thanks.
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.
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.
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.
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.
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
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.
精彩评论