Search code examples
c++gcccygwin

Cygwin GCC C++ compiler - Why ./?


If 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.


Solution

  • 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.