Running C programs in Linux/MacOSX
Here is another(probably) noob question. Let us assume that I have a simple 1 file program (called myProg.c) written in C. When I want to compile this program in Linux/MacOSX, I type "gcc -o haha myProg.c". The executable file that is generated is now named "haha". When I wish to run this program, I will need to type "./haha" in the console.
What do I need to do to stop typing "./" always? How can I make sure that by just typing "haha", my progr开发者_运维知识库am will be invoked? I have checked the permissions of the file "haha" and it is executable. Am I correct in thinking that the "./" indicates that the path of executable file i.e., the file is present in the current directory (".")??
The current directory is by default not a part of PATH in unix-derived OS'es. This is a security measure, which you can but should not change by modifying PATH in your .bash_profile
or .bashrc
The reason not to include the current directory in path: Assume that you are root, and you have a malicious user. This user creates e.g. a ls
executable in his home directory, which does something not nice. If you're looking at what this user is up to, and type ls
while in his home directory, his ls will be executed.
If you want to just change it, add PATH="${PATH}:."
to your .bashrc
and .bash_profile
精彩评论