basic unix ./ syntax

2019-08-27 16:07发布

问题:

I'm compiling c++ programs from the OS X Terminal:

> g++ a.cpp

I then the run the program from the terminal:

> ./a.out

What does the ./ syntax mean? Is this somehow related to ./run?

回答1:

. means "the current directory", so ./a.out means "the file named a.out in the current directory". If you were to type simply a.out, without the ./, then the system would look through the directories on the path ($PATH) to find the program, instead of looking in the current directory.



回答2:

dot-slash is just a relative path; it's telling the shell to run the program from the "dot" directory, that is, the current working directory. it only works if the program has the execute bit set (chmod +x a.out; the compiler will ordinarily handle this for you).



回答3:

In all systems, when you type a command, the system needs to know where the command is. The places it should look for the program are listed in an environment variable named PATH. Windows searches the directory you are in as well as the PATH (I believe before, but I haven't used Windows in quite some time), and if it finds an executable with the proper name in the current directory, it executes it. Windows considers that a "feature," the rest of the computer community sees it as a "security flaw." Therefore, Unix-like environments do not search the current directory unless it is put in the path explicitly. You can run something not in the PATH by giving an explicit path to it. In Unix, . is the current directory, so ./a.out tells the system to execute the program a.out found in the current directory.