« August 20, 2006 | Main | August 30, 2006 »

August 29, 2006

How to compile a binary for Linux, Unix and even Mac OS X

I don't do too many "tech" entries on my blog, as there really isn't a great deal of people that hit this thing that would understand what I was talking about anyhow. However, a few days ago I ran across a blog entry where someone was talking about having the need for a binary ... but he didn't know how to compile a program from source.

Hence the reason for this entry.

For those of you that just have no clue, a program is written in a programming language, and often contains hundred, thousands or even million lines of code. It all depends on how complex the program is. It's these lines of code that tells a program how its supposed to work.

But, how do you get these lines of code to actually do something useful? Simple ... you compile them into a working executable binary. This is done by running the code against something called a compiler, such as gcc, in the terminal (or command line) of your system. This application will convert the code to a working binary for the system that it's compiled against (Linux, Unix or BSD). Please note that you must have the compiler installed on your *nix machine in order to compile a program's source code.

For *nix based systems, programmers will often include something called a makefile and possibly a configuration utility to make compiling a program source easier. The breakdown of using the makefile and configuration utility, if one is included, is as follows:

./configure (parameters)
make
make install
make clean

The ./configure option, if available, allows a user to pass specific parameters to the makefile, such as the binary's installation directory, location of supporting binaries and more. An example of this can be seen below:

./configure INSTALL_DIR=/usr/local

In the example above, we're passing the information "install directory" to the makefile. This will cause the binary, when it is compiled, to be installed in the /usr/local directory on the machine that it is being compiled on.

Please note that this is just an example. There can be *MANY* configuration options available to be used with the makefile. The best rule of thumb to determine these options is to pass a "help" command to the configure file, as in the example below:

./configure --help

If available, this will list all possible parameters that can be passed to the makefile, thus affecting how the source code is compiled. Be sure to read this information carefully, as a simple oversite can cause significant problems with your binary.

Once the configure script has been executed and all parameters have been passed, you can now compile the source code. This is done by running the make command. This command handles compiling and linking of the binary. Depending on the size of the source code and the speed of the machine that it is being compiled on, it may take a while for the make command to complete. The best bet is to sit back and be patient ... some programs can take hours to finish. Thankfully most binaries compile within a matter of seconds or minutes.

After make completes successfully, you can install the binary. This is done by running the make install command. This will put the binary in the proper default installation directory. If you passed a parameter to the makefile using the configure script, as shown in the example above, the binary will be placed in the user defined location instead.

To finish up the process, you can run the make clean command. This will clean up any temporary files that were created during the compiling and linking of the binary while running "make".

After this step is completed, you will now have a working binary, freshly compiled and installed from source code.

~out...

Posted by ed at 10:24 PM | Comments (0)