The Wonderful World of Make

This page is approximately the talk from the July 14, 2009 meeting, wherein Jās showed the basics of how to use GNU Make. (I say "approximately", because I don't remember exactly everything that I said, but based on the source code, this is probably what I meant to say. --Jās)

= Program files =

The source code to be compiled into a program: (The code samples below are also combined into .)

main.cpp
// demo program for make int main(int argc, char **argv) {    std::string toWhom("world"); if (1 < argc) { toWhom = argv[1]; }    hello(toWhom.c_str); return 0; }
 * 1) include
 * 2) include "hello.h"

hello.c
/* hello function */ void hello(const char *to_whom) {    printf("Hello, %s\n", to_whom); }
 * 1) include 

hello.h
/* header for hello function */ extern "C" { void hello(const char *to_whom); }
 * 1) ifndef _HELLO_H_
 * 2) define _HELLO_H_
 * 1) ifdef __cplusplus
 * 1) endif
 * 1) ifdef __cplusplus
 * 1) endif
 * 1) endif /* _HELLO_H_ */

The goal is to take these three files and compile them into a program called my_hello that takes one optional argument: $ ./my_hello Hello, world $ ./my_hello jas Hello, jas

To do this,  and   need to be compiled into object code (  and , respectively), then the object codes need to be linked together (into  ). If you wanted to do this manually, you would issue: gcc -c hello.c -o hello.o g++ -c main.cpp -o main.o gcc -lstdc++ hello.o main.o -o my_hello

= First Makefile =

To have  do these commands, you would create a file, called   (  in the tarball) with the following contents:

my_hello: hello.o main.o        gcc -lstdc++ hello.o main.o -o my_hello main.o: main.cpp g++ -c main.cpp -o main.o hello.o: hello.c hello.h        gcc -c hello.c -o hello.o
 * 1) Makefile:  explicit rules

This  has all the rules explicitly. What follows is a line-by-line description of the syntax of the above :


 * 1) Makefile:  explicit rules

Comments in a  start with a   and end at the end of the line. Everything inside that is ignored by.

my_hello: hello.o main.o

This is the first target, called. Its dependencies are  and. The form of all targets is:. The block that follows a target line is the series of commands to execute if the target file is older than the dependency files. For example, if the file  is missing or older than its dependencies, it will execute the command:

gcc -lstdc++ hello.o main.o -o my_hello

Notice that the block of commands is indented with the  character, not a series of spaces. The  character indicates to   that this is a command script for a target. The  character at the beginning of the command is the first thing you should verify when debugging a  .

The command  in this case executes the GNU C compiler, using the standard C++ library , linking the object files   and   into an output program binary  called.

main.o: main.cpp

The next target is the object file, and it depends on the file.

g++ -c main.cpp -o main.o

It is created with this line, the GNU C++ compiler, compile without linking ( , for creating object files),  is the source, and   is the output.

hello.o: hello.c hello.h

Target  is similar, it depends on   and.

gcc -c hello.c -o hello.o Use  to  (ompile-without-linking) the source file   and  (utput) the object file.

Now, to build our program, we issue :

$ ls Makefile Makefile.0 Makefile.1 Makefile.2 Makefile.3 Makefile.4 hello.c hello.h main.cpp make.cheat $ make gcc -c hello.c -o hello.o g++ -c main.cpp -o main.o gcc -lstdc++ hello.o main.o -o my_hello $ ls Makefile Makefile.0 Makefile.1 Makefile.2 Makefile.3 Makefile.4 hello.c hello.h hello.o main.cpp main.o make.cheat my_hello

Here you can see that  created the files ,   and.

In order to know what to do, the program  looks in the current directory for a file named, in order:  ,  , or. Common convention is to use. It finds, then begins reading it. Since there was no target given as a commandline parameter,  builds the first target, which is   in this case. It determines its prerequisites have not been met, so it builds them, based on their rules:   first, then. Once they have been built, it executes the rule to make, then exits.

'''To be continued... chruck 01:07, 17 August 2009 (UTC)'''