basic unix ./ syntax

可紊 提交于 2019-12-11 16:28:42

问题


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.



来源:https://stackoverflow.com/questions/9658364/basic-unix-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!