make command and Makefile

make is a tool for defining source file dependencies and compiling only the source files that need to be compiled. Write a configuration file called Makefile. There are dialects such as nmake, dmake, and cmake in make, but the make introduced here is GNU make.

Why use make?

I will write about the motivation for using make.

It's hard to wait for the C language source file to compile

The main motivation for writing make is that as the number of C language source files increases and the amount of description increases, it is difficult to wait for the C language source files to compile.

Often gives gcc the "-O3" option to maximize C performance. I think. This takes a lot of time to compile as the source files get bigger.

Recompiling only those that depend on the modified file will save time.

General installation procedure on UNIX / Linux

The general software installation procedure for UNIX / Linux is standardized as follows. Check the OS settings with a command such as "./configure" and generate a Makefile. Execute the generated Makefile with make, compile it, and install the software with "make install".

./configure
make
make install

Makefile basics

Here, I will explain only the basics of Makefile. Let's write a Makefile to generate the executable file of the application explained in Split compilation. Use "make" to generate an executable file of the application, and "make clean" to delete the object files and executable files that can be created in the middle.

Below is the contents of the Makefile. The description of "all" describes the operation of "make". The description of "clean" describes the operation of "make clean". The part with a tab at the beginning is the command you want to execute. After "::", describe the dependent file (or just a symbol). From the second line onward, start with a tab (not blank) and write the command you want to execute.

Dependencies can be described continuously, and if the dependent file is updated, the command will be executed.

Let's write a Makefile for the file explained in Split compilation. Use the "make" command to link with split compilation to generate an executable file. Use "make clean" to delete the generated object files and executable files.

all::myapp

myapp: myapp.o mylib1.o mylib2.o
gcc -o myapp myapp.o mylib1.o mylib2.o

myapp.o::src / myapp.c
gcc -Iinclude -c -o myapp.o src / myapp.c

mylib1.o::src / mylib1.c include / mylib1.h
gcc -Iinclude -c -o mylib1.o src / mylib1.c

mylib2.o::src / mylib2.c include / mylib2.h
gcc -Iinclude -c -o mylib2.o src / mylib2.c

clean::rm * .o
rm -f myapp

Let's run make.

$make
gcc -Iinclude -c -o myapp.o src / myapp.c
gcc -Iinclude -c -o mylib1.o src / mylib1.c
gcc -Iinclude -c -o mylib2.o src / mylib2.c
gcc -o myapp myapp.o mylib1.o mylib2.o

The executable file "myapp" is generated. Let's try to modify only "src / mylib1.c". Running make will only execute the commands you need.

$make
gcc -Iinclude -c -o mylib1.o src / mylib1.c
gcc -o myapp myapp.o mylib1.o mylib2.o

Run "make clean". The object file and the executable file are deleted.

$make clean
rm * .o
rm -f myapp

How does make detect that a dependent file has been updated?

For example, suppose "mylib.c" depends on "mylib1.h". In this case, if the update time of "mybli1.h" is newer than the update time of "mylib.c", it is judged that the file has been updated.

I want to automate the generation of Makefile

Since Makefile is a text file, it is a practical text processing programming language in Linux / UNIX Perl. Makefile has dialects depending on the type of make, so you can use it to absorb it on the Perl side.

Can make be used on Windows?

When using MinGW on Windows, use the "gmake" command instead of the make command. The description of Makefile is the same as the one introduced. On Windows, make means "nmake", which is probably gmake because of the conflict.

Associated Information