How to create a makefile in linux

Programmer Blog

Before talking about anything, I would like to tell one term : Makefile

So the question is what is Makefile ?

Well to answer this question the first question that should be answered that is why you need to use something called Makefile ?

We have learned that by invoking

cc [filename].c

in the terminal we can easily compile the code & then ./a.out will successfully run this code. Then why somebody needs something called Makefile to do this. Isn’t this some other overhead? Well not really.

You see Makefile has lot of potential. I just don’t want to deviate form the actual topic but the most 2 important reason why we need Makefile are

a. To automate the compilation process & if at some point of compilation you want

to perform some task, Makefile handles that very easily Moreover when we are working in a big project which has 20 header files & their .c files & a main file, every time you need to type the whole command which is pretty big but if you used Makefile, then you write all those names once & invoke a command every time & it will perform all the task automatically.

b. The second reason is very serious. Now we all know that in a c program we should never write like this

main()

{

or

void main()

{

or

void main(void)

{

…………

………

…………

}

}

}

Now, if you compile a code without the Makefile, proper error reporting during runtime is not done by the OS. So firstly, in the program, the main should be like this below

int main()

{

or for some error handling

int main()

{

…………

return 0;

if (some error)

}

return 1;

if (some other error)

return 2;

if (no error)

return 0;

                                                               }

and after this, you write the Makefile and compile the code from the Makefile. If you don’t do either of them, you will face some bad runtime error handling problem plus it is not considered the best practise in the international developer platform.

Note: If you don’t know why we should use int main instead of main then visit http://users.aber.ac.uk/auj/voidmain.cgi and http://www.eskimo.com/~scs/readings/voidmain.960823.html

Note: To know more about Makefile visit http://www.tutorialspoint.com/makefile/ and http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/

Now that you what is makefile, here is the Makefile that I have developed for several years of my programming experience. This makefile works very well in all of my systems. I have 2 home PCs one has Ubuntu 14.04LTS & the other one has Debian 7 with the bash version of 3.2 & gnuMake latest version installed. I also have a Macbook Pro where it works also very well. If this does not work then try changing some components. I have also given some webpage address which I expect you to have a look. The main concept is that you learn how to create a Makefile.

MAIN = test #HEADER_DEFINITIONS = list.c CC = gcc

COMPILE = -cEXE = $(MAIN) OPTIMIZE = -Os

SHELL = /bin/bash NO_WARNINGS = -wVERBOSE = -v

# ARGS =

all: link

@echo “Executing………..

@echo ” > > > > > > OUTPUT < < < < < < “

@$(SHELL) -c ‘./$(EXE) $(ARGS)’

link: compile

@echo -n “Linking………….

@$(SHELL) -c ‘$(CC) -o $(EXE) *.o’

compile: $(MAIN).c $(HEADER_DEFINITIONS)

@echo -n “Compiling………

@$(SHELL) -c ‘$(CC) $(OPTIMIZE) $(COMPILE) $(NO_WARNINGS) $^’

run: $(EXE)

@echo “Executing………..

@echo ” > > > > > > OUTPUT < < < < < < “

@$(SHELL) -c ‘./$(EXE) $(ARGS)’

clean:

@echo “Cleaning…………

@$(SHELL) -c ‘rm -f *~ *.o $(EXE)’

Lets say that the file that you want to compile is p1.c, then edit the first line which says

MAIN = test to

MAIN = p1

and so on & so forth..You get the idea right?

Moreover, if you have multiple file compilation, then the file from where you are calling all the functions & where you have included all the headers, say main.c & the headers are defined in a.c, b.c, c.c, d.c & e.c, then the first two line of the Makefile be,

MAIN = main (without extension always)

and then uncomment the next line by deleting the # from the beginning of the line & write

HEADER_DEFINITIONS = a.c b.c c.c d.c e.c (with extension & space between them)

Then put this Makefile in the same directory where all of your .c files located & then open the directory in the terminal and then type

make or make all <———— To build & run the files

make run <————- Just to run if you have already built the files make clean <———— run once every time before you rebuilt the files

Note: If you have to give any command line arguments then uncomment the ARGS parameter & give the values after the ‘ = ‘

something like ARGS = 12

Personally I use even a more advanced version of this but this will establish a very good ground for Makefile..Lastly May the Google be with you & happy Coding 🙂

Read Also :  Socket Programming In C/C++

Please follow and like us:

Leave a Reply