« How to compile a binary for Linux, Unix and even Mac OS X | Main | My new car »
August 30, 2006
Executing a binary in *nix
So last night I wrote about how you can compile a program's source code on your machine. After reading the entry, I realized that I left out two important pieces of information that may be necessary to properly compile. First, you need to make sure that a file is executable and second, you need to be able to execute the file.
First, I'll talk about verifying if a file is executable or not. Back in March, I wrote a blog entry on the chmod command and how it is used to set file permissions. Go ahead and read that entry first, and come back here once you're done.
...
Now that you've read up on that entry, you should have a general understanding of how file permissions work in *nix operating systems. In order to execute a file, it has to have the proper execution permissions set by chmod. Also, depending on who you want to be able to execute the file, the file needs to have the proper user permissions. It's generally a good rule of thumb to set an executable file completely read/write/executable by the file owner, but only read/execute for groups and users. This can be done by running the following command:
chmod 755 filename
This will set the proper permission flags for the file you wish to execute. At this point, we can now run the binary with no problems. However, you can't just type in the name of the binary in the terminal interface and expect it to run. In order to just type in the name of the file to get it to execute, the binary must be in the executable search path, aka PATH, of the user account. I won't go into detail on what a PATH is, but you can click on this link to read up on what it is and does and come back here when you're finished.
So, if you're binary doesn't exist in the PATH, you need some way to run it. Thankfully, there is a fairly simple method to running an executable that's outside of the PATH. By placing a period (or dot) and a forward slash (/) in front of the binary, you are telling your *nix based OS "I want to run this program", as seen in the example below:
./configure
In this example, the file configure is an executable binary. It is located outside of the executable search path, so we put a ./ in front of it to make it run.
Now, if you have a binary that you want to be able to run without using the ./ method, you can simply edit your PATH or copy the file to an existing PATH, such as /bin, /usr/bin or /usr/local/bin. If the file requires root permissions to operate properly, you can put it in /sbin, /usr/sbin or /usr/local/sbin to get the same effect.
~out...
Posted by ed at August 30, 2006 08:56 AM
Comments
Post a comment
Thanks for signing in, . Now you can comment. (sign out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)