Home »
Linux
Make Utility (MakeFile) in Linux
By IncludeHelp Last updated : October 20, 2024
Hey folks, have you ever used IDEs? Most probably, yes. So what's your favorite one? Geany, CodeBlocks, DevC++, Eclipse, NetBeans or something else?
Overview
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.
Steps to Create a Make File
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)
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.
How to Create a Make File
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.
Example
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
After installing make utility, to make sure that properly installed it in our machine we check the version of its version installed:
make --version
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:
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:
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:
- command: make
- is equivalent to : make all
While creating makefile use tabs for spacing don't mix tabs and spaces.
Recommended Articles: