C++ Tutorial

What is Makefile?

A Makefile is a file (with no extension) containing a set of directives how to compile and link a program.

Using C/C++ as an example, when a C/C++ source file is changed, it must be recompiled. If a header file has changed, each C/C++ source file that includes the header file must be recompiled to be safe.

Each compilation produces an object file corresponding to the source file. Finally, if any source file has been recompiled, all the object files, whether newly made or saved from previous compilations, must be linked together to produce the new executable program(.exe extension).These instructions with their dependencies are specified in a Makefile. If none of the files that are previously compiled have been changed since the last time the program was compiled, no actions take place.

Why it’s needed?

For large software projects, using Makefiles can substantially reduce build times. Because it’s only compiled those files which have been changed since the last time the program was compiled. Also, it plays essential roles to manage build systems on large software projects. In  IT farm software engineers submit their files(containing code) as well as Makefiles to reduce the complexity of projects.

How to configure your Windows pc to use make command?

Makefiles originated on Unix-like systems and are still a primary software build mechanism in such environments. So, the make command run only Linux type operating system.

Windows supports a variation of makefiles with its nmake utility.

But the most surprising fact is that Standard Unix like makefiles can be executed in Windows in a Cygwin environment.

Now, I show you how to run make command in windows :

First, you have to download MinGW installer  Click here to download.

When download finish opens the download file. And you see like this

install menu of mingw installer
and click install. then you see a page like this popup.

file path of minGw

you have to remember the file path.because it will be needed later. click continue and finish the install.

then you see a page like this.

install menu of minGw installer
you have to click basic setup (mark on red color ). and  Mark list showing (red circle).

then click installation and click update Catalogue. you will see a page like this.

review changes

Now, you have to click Review changes and you will see a page like this.

apply
click Apply. wait until it finishes…

you will see a page like this.

finish

Then, click Close and update the list. Now, you have to make Environment variables of this package.

To do so, Go to  This pc  and

environment variables

Right-click and then, click properties. you will see this page and click Advanced system settings  (red marked)

properties

and you see a page like this.

properties

Click Environment Variables (red marked)  you see a page like this. Find the path (specified with blue  )option and click Edit (red marked ) and

A page like this popup and click New option(red marked ).

Now, you find the option where you put the Environment variables value. If you have a new computer your Environment variables list will be very small. Don’t worry it’s normal.

When you install  MinGW you specified the install path. Now, you have to use that path to reach following page. I marked two important folders which paths are used to form two Environment Variables.

Now, you have to copy those two folder paths including bin folder.

Screenshots show the paths  (red marked)  which will be copied.

 

Now, you have put those copied path into Environment variables list and press ok.

You have done most of the boring part.  Now, you have to use make command.

Open the command prompt and write make if you see like following screenshots this means you have successfully done your job. If you don’t then read it again see you missed something.

Makefile Command prompt

I know that you won’t feel happy.Because, the result you find that’s not we want.This shows because of we don’t provide any targets as well as  Makefile to execute. So, let’s provide target files with it’s following Makefile.

I just simply write a very small c file and also write a Makefile for it.

Here:

#include <stdio.h>

int main()
{
    printf("this is Make file tutorial");
    return 0;
}

Be sure that you save c file as “main.c“.

To write Makefile you have to open notepad++ or any text editor and write as you see in below or you just copy and paste the below code and save as Makefile(exactly as showed) with no extension. Remember that Makefile has no extension.

Here:

target:main.o
	g++ main.o -o app
main.o:main.c
	g++ -c main.c
clean:
	rm *.o *.exe

probably, you think that why I write clean section in Makefile. This is because when we give “make clean” command it will remove all .o (object file) and .exe (executable) extension file.

To run Makefile. Go to the Command prompt and write down the directory of the folder where your target file exists.I use cd to shift the command prompt directory to target’s directory also where our Makefile exist.

as:

Then simply write make.You will see like this. This point must be clear that if we write “make clean” command instead of “make” command it will remove all .o (object)  file as well as .exe (executable) file and It will only happen if in that directory there exist any .o  extension file.

runing make Command

If you see this output this means you successfully execute the Makefile.Congratulation, you have done very well.You learn the basics of Makefile.

If you want to know about the Rules to Write a Makefile and the Basic process of Compiling a C/C++ file.Check This Article What are the Rules for writing Makefiles.

In future, you will use Makefile to manage your projects. Best Of Luck.

21 COMMENTS

  1. Heya i am for the primary time here. I found this board and
    I in finding It really helpful & it helped me
    out a lot. I am hoping to offer something again and aid others such as you aided me.

  2. I am extremely impressed with your writing skills and also with the layout
    on your blog. Is this a paid theme or did you modify
    it yourself? Either way keep up the nice quality writing, it is rare to see a nice blog like this one these days.

  3. We are a bunch of volunteers and opening a new scheme
    in our community. Your site offered us with helpful info to
    work on. You have done a formidable activity and our
    whole community might be thankful to you.

  4. I’ve read several just right stuff here. Certainly value
    bookmarking for revisiting. I surprise how a lot attempt you set to make any such fantastic informative web site.

  5. First off I want to say excellent blog! I had a quick question which I’d like to
    ask if you do not mind. I was curious to know
    how you center yourself and clear your head before writing.
    I have had a difficult time clearing my thoughts in getting my ideas out.
    I truly do take pleasure in writing but it just seems like
    the first 10 to 15 minutes are generally wasted just trying to figure out how to begin. Any suggestions or tips?
    Kudos!

    • I also have same problem you do.But we all know we have to work hard to make things as we want.
      My tips for you that you have to make positive constraint to focus only what you have to do.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.