Marka GNU

GNU Make Tutorial

GNU Make Tutorial

GNU Make is a tool that helps to generate executable programs from source code and also process other non-source files from the project. Make gets the logic of building the executables and processing other non-source files from a file called a makefile or a Makefile.

Why Make?

Installing Make

Make alone is not enough to build programs. To build programs from source you need to have compilers and other tools installed on your system. So, we need the full suite of development tools for our purpose.

To compile sources on Linux, there is a package called “build-essential” on Debian-based systems (e.g. Ubuntu, Linux Mint, etc) and “Development Tools” on Red Hat and CentOS.

To install this on Debian based systems:

apt-get install build-essential

To install this on CentOS and Red Hat run:

yum groupinstall "Development Tools"

Getting Started With Makefile

Let's get started by writing a hello world program with the C programming language.

The main function of our C program will reside inside hellomain.c. The content of the file should look like the following:

#include  int main()  my_fun(); return 0;  

This code includes the header file hellofun.h that contains the declaration of a function called hello_fun(). The content of hellofun.h is:

void my_fun();

The definition of my_fun() lives inside hellofun.c:

#include  #include  void my_fun()  printf("Hello World!\n"); return; 

This is a very simple program and we could compile it with gcc with just one line of command. But, real life programs are not as simple and as small as this. Things get complex really soon.  Below, I am going to write the necessary makefile script for compiling this hello world program. I will explain different parts of it in subsequent sections.

hellomain: hellomain.c hellofun.c gcc -o hello hellomain.c hellomain.c -I.

Keep this code in a file called makefile (without any file extension). Put the file in the directory where the C files are. Point your command line in this directory. On the command line write make and press enter. An executable file called hello will be generated in the current directory. You can verify the result by running the executable with the following command.

./hello

Outputs:

Hello World!

Rules, Targets And Dependencies

A makefile script is a collection of rules. The rules instruct Make how to build a target or output from source or other files. The rule also specifies dependencies of the target. The dependency rules must be executed first depending on whether that is already processed by looking at the time stamps. In our example makefile above, we have a rule with target named hellomain and its dependencies. The target name is separated by a colon from the dependency list. The shell commands that will be executed are listed in the following line. The shell commands must start with a tab character.

If you specify no parameter with the make command then the first target is executed. In our example we did not specify any parameter and we had hellomain as the first and only target.

Variables

Variable are great way of writing value once and using them numerous times without repeating the value over and over again. It helps us keep our code DRY (Do Not Repeat Yourself). If you ever need to change some value all over the script, you just need to change that in one place to reflect the change everywhere if you are using variable.

In our example we used gcc as the compiler, but we may need to change the compiler to something else. So, we can keep the compiler name in a variable. Also, we can keep the compiler flags in another variable to reuse that. For setting a value to a variable we use equal sign (=) and to read that variable we use $(variable_name).

CC=gcc CFLAGS=-I. hellomain: hellomain.c hellofun.c $(CC) -o hello hellomain.c hellomain.c $(CFLAGS)

Cleaning The Environment

We often may need to clean our environment. If we want every piece of our project to be rebuilt from scratch, we need to clean it. In our simple example the only file that is generated is the hello executable. Without deleting that manually, we can delete that with make. So, we can create a rule for that and name the target as clean.

CC=gcc CFLAGS=-I. hellomain: hellomain.c hellofun.c $(CC) -o hello hellomain.c hellomain.c $(CFLAGS) clean: rm hello

The shell command in the clean target is just the age old shell command rm. Now, from the command line execute:

make clean

Look at the current directory to see that the hello executable file is gone.

Expanding Our Example To Solve More Problems

In our simple hello world compilation example we have a problem that we have not solved yet. hellomain target looks at hellomian.c and hellofun.c files' time stamps the next time you try to recompile it with make or make hellomain. So, if you change any of those two files, they will be recompiled. But, if you change hellofun.h then it will not recompile. That's unexpected!

Again, we have skipped an intermediate level. We did not generate the object files and directly generated the executable. But, behind the scene the object files are created in a temporary directory and deleted. We want to generate the object files before building the executable. This time we are naming the main target as all

all: hellomain.o hellofun.o gcc hellomain.o hellofun.o -o hello hellomain.o: hellomain.c hellofun.h gcc -I. -c hellomain.c hellofun.o: hellofun.c hellofun.h gcc -I. -c hellofun.c clean: rm -rf *.o rm hello

Run the make command again to see if your program builds successfully or not. Run the hello executable to verify the result. Look at the current directory and you will see that objects files are created. We have added one more line to the clean target to clean up object files. This makefile script will help recompile the hello world program even if the hellofun.h file is modified.

Conclusion

Make is one of the most essential tools for linux users and programmers. If you are an end user then knowledge of make will help you fix many broken things in your world of linux. If you are a programmer then you just cannot walk away by writing the code and let your users figure out how to compile that source code to executables. You must create a makefile script for the end users, so that they do not play some guess game to compile your source to executables.

References

GNUMake Project Homepage
GNU Make Documentation

Gry Bitwa o Wesnoth 1.13.6 Wydanie rozwojowe
Bitwa o Wesnoth 1.13.6 Wydanie rozwojowe
Bitwa o Wesnoth 1.13.6 wydana w zeszłym miesiącu jest szóstą wersją rozwojową w 1.13.Seria x i zapewnia szereg ulepszeń, w szczególności w interfejsie...
Gry Jak zainstalować League Of Legends na Ubuntu 14.04
Jak zainstalować League Of Legends na Ubuntu 14.04
Jeśli jesteś fanem League of Legends, to jest okazja do przetestowania League of Legends. Pamiętaj, że LOL jest obsługiwany w PlayOnLinux, jeśli jeste...
Gry Zainstaluj najnowszą grę strategiczną OpenRA na Ubuntu Linux
Zainstaluj najnowszą grę strategiczną OpenRA na Ubuntu Linux
OpenRA to darmowy silnik gier strategicznych czasu rzeczywistego, który odtwarza wczesne gry Westwood, takie jak klasyczny Command & Conquer: Red Aler...