Monday, September 22, 2008

Makefiles in C

Typically in most c programs (especially those written for opensource purposes), there exist a file call Makefile. Makefile is pretty much like a macro (for those familiar with Microsoft Office), or a batch file (for those familiar with DOS). Well basically, in very simple terms, it automates repetitive stuffs so that it makes life easier to program.

In a huge program, we tend to have many classes and dependencies (i.e. header files). For example, if we have a single program4 file which is make up of program1.c, program1.h which in turn make up program2.c together with program3.c to make the final program4 file.

In this example, the modification of program1.c will result in a recompilation of program1.c and program3.c which is technically inefficient. Imagine having to do this each time you are trying to debug and found a bug. So to make life easier, Makefile comes to the rescue. It basically automate compilation so that we don't have to type a long string of dependencies from scratch each time we make modification.

For a proper tutorial on how to create your own Makefile, visit:
http://www.eng.hawaii.edu/Tutor/Make/3.html

In very short term, in order to execute the makefile, run the command "make" inside a folder.
Yes, correct, just type
"make" and hit ENTER. A chunk of output should normally appear, type "ls" and the executable program (normally in green) can be seen.

No comments: