What is a makefile?

Can someone please help me understand ,in simple language, What is a makefile ? what are its uses?
I tied various sources from the net but the have been unable to cope with the rich technical grammar .

Hello @piyukr,

A makefile is just an easy way to compile several source files at once, simply using the command-line command make.

I will try to provide you with an example:

Suppose you decided to write a program to calculate factorials of a number, and, furthermore, assume you wrote two distinct versions:

An iterative one, which you placed on the file iter.c and a recursive one, you placed on the file recurs.c;

Also, due to modularity and portability restrictions assume that you had to declare the prototype of the factorial function on a separate header file, called, fact.h;

To end, assume that you have a fourth file, called main.c, that attempts to use one of the factorial functions defined on one of the files, recurs.c or iter.c;

So, we have:

  1. fact.h -> Header file that contains the factorial function declaration, int factorial(int n);
  2. iter.c -> C source code file that contains the code for the iterative definition of factorial function;
  3. recurs.c -> C source code file that contains the code for the recursive definition of factorial function;
  4. main.c -> C source code file that contains a simple usage of one of the above functions (that are defined on a separate file, this is usually how projects are deployed);

Now, to compile with command line the code for the iterative version, you could do:

gcc -o iterative iter.c main.c

which will produce you object files and an executable file called iterative (the .exe file goes with the name you pass to the -o flag), so, to run your program you can now do ./iterative and you’re done.

and the same for the recursive version…

This work because on this simple example, our function main.c is only “dependent” on one function definition and as a consequence on one header file…

However, assume you are working on a huge large scale project with say, some dozens of dependencies… Then, compiling everything by hand would be very time consuming… For such situations we can have a Makefile.

For the example I provided you, if I wanted to compile the iterative and recursive functions all at once, I could create a plain text file, name it Makefile, put it on the directory where all source folders/files are located and write something like this:

CFLAGS= -g -O2 -Wall -pedantic -ansi

OBJECTS1= main.o recurs.o
OBJECTS2= main.o iter.o

all:
	make factorial-recurs 
	make factorial-iter

factorial-recurs : $(OBJECTS1)
	gcc $(CFLAGS) -o factorial-recurs $(OBJECTS1) 

factorial-iter : $(OBJECTS2)
	gcc $(CFLAGS) -o factorial-iter $(OBJECTS2) 

main.o: main.c fact.h

iter.o: iter.c fact.h

recurs.o: recurs.c fact.h

clean :
	rm factorial-recurs
	rm factorial-iter
	rm *.o                                                                                                                                                                                                                                                                                                             
run :                                                                                                      make all
	./factorial-iter

Like this, on a terminal you can simple type the command make all and all files would be compiled :slight_smile:

If you attempt to read the above “code” carefully you will see that the vast majority of its content is simply flag definitions to make the compilation command cleaner and shorter :slight_smile:

I hope I could help,

Best regards,

Bruno

4 Likes

Thanks a lot @kuruma .One question - Does the concept of makefiles apply for all languages or just C ?

A Makefile applies to basically every compiled language, and I guess you could do the same to a language like C,C++, Java or other…

A major exception to this “Makefile standard” is Google’s programming language Go, that has some features which allow for a compilation of several files at once without any conflicts and without the need for a Makefile… :slight_smile:

1 Like

thank you :slight_smile:

//