NetworkingFiles
SecurityProNews
ITmanagement




Make And Makefiles

By: Bryan Young
Expert Author
2010-08-18

When I first learned to program, like many people, I learned in a graphical IDE that did basically everything for me. This is great for beginners who need to focus on learning programming, and not worry with compiling and linking their code manually. In my case, I was learning C so there was a lot going on in the background when I clicked on 'compile' that I was not aware of, until I began coding on a remote Unix machine.

For those who don't know, when creating a C program with more than one file, you have to first compile the code into machine language, then link that code together so that they can be used together.

I spent many nights working on programs, having to continuously type the same three lines of gcc over and over after making small changes, that I hoped would fix whatever bug I was having. It got to the point that I was typing more to compile my code than changing my code, so I decided to find a better way. That was when I foundmake.

Themake command is a way to compile and link all your code using just one command on the command line. It is controlled by aMakefile, written by you, which tracks dependencies in your code so that only the code that has been changed since the last build is compiled. A simpleMakefile looks something like this:


#Makefile

myprogram.exe : main.o class.o
gcc -o myprogram.exe main.o class.o

main.o : main.c
gcc -c main.c

class.o : class.h class.c
gcc -c class.c


The dependency lines (with the colons) work by checking to see if the files on the right of the colon are newer than the file on the left, and if so, will run the code beneath. This process is recursive, so in our example above,make will look at main.o and class.o, but before comparing them against myprogram.exe, it will first check their dependencies to see if they need to be updated.


About the Author:
Bryan Young is a staff writer for WebProNews.
Newsletter Archive | Submit Article | Advertising Information | About Us| Contact

LinuxDeveloperNews is an iEntry, Inc.® publication © All Rights Reserved Privacy Policy and Legal
iEntry Contact Advertise iEntry Jayde WebProWorld Forums Downloads News Article Archive About iEntry LinuxDeveloperNews Home Page LinuxDeveloperNews News