Estimated time: 20 minutes
make is a task runner for targets described in a Makefile. It is mostly used to control the compilation of an executable from source code. Thus, you can use it to automate the tidious task of compiling your c code, or even automate the compilation of a report made with LaTeX.
make a new beginning
To give you a first taste, open up a terminal and type the following command:
make will great you with the following message:
make: *** No targets specified and no makefile found. Stop.
So what happened?
make first start to search in your current directory for a file called
Makefile. This file contains instructions, aka rules, that tell
make what to do. Since there are no such file,
make stops almost instantly.
make it simple
Now write a simple hello world program, which you will save into a file called
hello.c. This programs will print the following on the standard output:
Now, fire up your terminal, use
cd path/to/hello/folder/ to go to the directory which contains
hello.c (download hello.c to compare with what you did) and type:
make comes alive and tells you something like:
gcc hello.c -o hello
Wow! What happened? When you typed
hello is what is called a target. A target is usually the name of a file that is generated by a program; examples of targets are executable or object files.
make will search for a file named
hello and detect from that file what programming language it uses. For most languages,
make has some basic builtin recipes, called implicit rules, to compile it. Here the recipe is given in the above output.
In that output:
gccstands for GNU C Compiler;
hello.cthe C program to be compiled;
-o helloan option to place the output of the compilation in the file
Now if you type
ls in your command line, you will see that a file
hello appeared. This is the executable built by
hello.c. Now you can execute it and verify what is printed on the standard output.
If you type again
make hello in your command line, it will tell you:
make: 'hello' is up to date.
That is because
make only builds the files that are changed. If
hello is more recent than its source file
make will skip the compilation process.
To see this, modify
hello.c to write the following on the standard output:
make hello again. Since
hello.c is more recent than
make will compile the source file again.
Thats it, you made your first experiences with
Now I strongly recommend you read sections 2.1 to 2.3. in the GNU make manual. It will only take you 10 minutes (included in the above given estimated time) and will help you understand how to
make magic happen.
Once you read these 3 sections, let us practice a bit.
To try the following questions locally, you can download a zip file of the questions folder here.
|Author(s)||Pablo Gonzalez Alvarez|
|Submission limit||No limitation|