ADVERTISEMENT
ADVERTISEMENT

Home » Linux

Make Utility (MakeFile) in Linux

In this article, we shall learn about Make Utility and MakeFile in Linux. We understand each of them with proper example.
Submitted by Himanshu Bhatt, on December 17, 2018

Hey folks, have you ever used IDEs? Most probably, yes. So what's your favorite one? Geany, CodeBlocks, DevC++, Eclipse, NetBeans or something else?

We use IDEs for sake of ease and quick coding. What if I tell you it is making you dumber at programming because it is as it hides a lot of background details from us so this article is about it only.

You might say ‘Hey I used the terminal on Linux and I know it's compilation done' trust me I don't mean that. You might be aware with g++ compiler or GCC (GNU C Compiler) perhaps CC (Clang's Compiler) or any other compilers (for other languages too) but it is only for one single source code and what about if you are working on a project with multiple source code and having dependencies, and need to update every time any file updates other files also need to update here make comes handy.

So what is make? It is a GNU utility to maintain groups of programs. Yes, you can use it on the terminal with the syntax:

    make  [option]... [target]...

What make does?

This utility actually determines (automatically) which portion of the program need to recompile and issues the command to recompile them.

In this article, will discuss make's implementation using C language. Although make can be used for any other language whose compiler is compatible to run with a shell command. In fact, make can use to describe any task where some files need to update automatically from others whenever the others change.

Let's have a small demonstration of make:

Step 1: Create a C program [We created a simple program with name program1]

Step 2: We follow command for make which is
make program1 (we used the name of the program without extension)

makefile 1

makefile 2

makefile 3

makefile 4

ADVERTISEMENT

Now what will happen in this example is, make will look for program1 and when it doesn't found anywhere it will look source code with the same name. In our case, we had program1.c then make will check whether it can build from that source code that is whether necessary compilers are present or not. Now, we had C compiler and it runs the code and creates our object.

To prepare to make we need to create a file called makefile which maps the relationships among files in our program and also states the commands for updating each file. Usually, executables files are updated from object files and those object files are build by compiling the source code. After creating a makefile, each time we change some source files is sufficient for all required recompilations. The make program takes the makefile content and last modification times of the file and then decide which file For all those files which it will rebuild, it will issue the commands which are mentioned in the makefile. make execute commands present in the makefile (or sometimes can be named as Makefile). Targets are updated using make if it depends on a prerequisite files where some modification have been done since the target was last modified, or the target does not exist.

Examples:

    make

Now we will understand makefile, but before that, we need to make utility in our machine, usually make come pre-installed but in case we don't have we can download it by:

    sudo apt install build-essential
makefile 5

After installing make utility, to make sure that properly installed it in our machine we check the version of its version installed:

    make --version
makefile 6

ADVERTISEMENT

Time to create our makefile, so what do is we create a file with any text editor (vi, vim or nano) to create a file name makefile (or Makefile, both will work).

We created our makefile:

makefile 7

Now we will try all three options we made.

First, we run make with clean and then demo.

You can notice the pattern that first it prints the command it is going to run then execute it.

Now we see the make all:

makefile 8

Did you notice something?

Yes, it produced an error as program1 doesn't exist ( we cleaned it in above using clean option). Once a command executiongives fault it stop make execution. So we create the program1 and call make all command and this time we get success.

Tips:

  1. command: make
  2. is equivalent to : make all

While creating makefile use tabs for spacing don't mix tabs and spaces.

Recommended Articles:

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.