| 1 | = Basics of Makefile = |
| 2 | [[http://en.wikipedia.org/wiki/Make_(software)|Wiki]] |
| 3 | |
| 4 | == Subprogram/Module/Class == |
| 5 | * functions can be defined in separated files. |
| 6 | * subprogram/module (Fortran, C...) |
| 7 | * class (C++, python, Objective-C,Java...) |
| 8 | * subprogram/module/class is defined for some specific purpose. one can use it without looking in the detail, just know what inputs and outputs / functions... |
| 9 | * C module |
| 10 | * .c files: source code / functions |
| 11 | * .h files: header files, define functions and etc... |
| 12 | |
| 13 | == Makefile == |
| 14 | * A common way of automating software builds and other complex tasks with dependencies. |
| 15 | * A Makefile is itself a program in a special language. |
| 16 | * The command 'make' reads 'Makefile' and do some tasks. |
| 17 | * Can specify file name 'make -f mymakefile'. |
| 18 | * The default name is "Makefile" |
| 19 | |
| 20 | == Structure of Makefile == |
| 21 | Typical element in the simple Makefile: |
| 22 | |
| 23 | {{{ |
| 24 | target: dependencies |
| 25 | [TAB] command to make target |
| 26 | }}} |
| 27 | |
| 28 | It is important to use tab character, not spaces. |
| 29 | |
| 30 | command 'make target' means: |
| 31 | * Make sure all the dependencies are up to date |
| 32 | * If target is older than any dependency, recreate it using the specified commands. |
| 33 | |
| 34 | For example: |
| 35 | {{{#!make |
| 36 | # |
| 37 | # Large Scale Computing |
| 38 | # Heat / Mass Transfer |
| 39 | # |
| 40 | ALL: ex20 ex21 |
| 41 | # |
| 42 | CC = gcc |
| 43 | CFLAGS = -g |
| 44 | # |
| 45 | SRC_EX20 = ex20.c cg.c |
| 46 | SRC_EX21 = ex21.c cg.c |
| 47 | # |
| 48 | OBJ_EX20 = $(SRC_EX20:.c=.o) |
| 49 | OBJ_EX21 = $(SRC_EX21:.c=.o) |
| 50 | # |
| 51 | ex20 : $(OBJ_EX20) |
| 52 | $(CC) $(CFLAGS) -o $@ $(OBJ_EX20) |
| 53 | |
| 54 | ex21 : $(OBJ_EX21) |
| 55 | $(CC) $(CFLAGS) -o $@ $(OBJ_EX21) |
| 56 | # |
| 57 | %.o : %.c |
| 58 | $(CC) $(CFLAGS) -c $< |
| 59 | cg.o : cg.c cg.h |
| 60 | $(CC) $(CFLAGS) -c cg.c |
| 61 | clean: |
| 62 | rm -f *.o ex20 |
| 63 | }}} |
| 64 | lines begin with # is comments |
| 65 | |
| 66 | == Age of dependency == |
| 67 | The last modification time of the file is used. |
| 68 | {{{ |
| 69 | $ ls -l |
| 70 | -rw-r--r-- 1 fuji staff 388 Sep 21 22:37 Makefile |
| 71 | -rw-r--r-- 1 fuji staff 2030 Sep 21 19:31 cg.c |
| 72 | -rw-r--r-- 1 fuji staff 838 Sep 21 19:31 cg.h |
| 73 | -rw-r--r-- 1 fuji staff 4768 Sep 21 22:37 cg.o |
| 74 | -rwxr-xr-x 1 fuji staff 13308 Sep 22 09:44 ex20 |
| 75 | -rw-r--r-- 1 fuji staff 3035 Sep 21 22:39 ex20.c |
| 76 | -rw-r--r-- 1 fuji staff 7728 Sep 22 09:44 ex20.o |
| 77 | }}} |
| 78 | * '''cg.c''' and '''cg.h''' to generate '''cg.o''' |
| 79 | * '''ex20.c''' to generate '''ex20.o''' |
| 80 | * '''ex20.o''' and '''cg.o''' to generate '''ex20''' |
| 81 | Since all target files are newer than dependencies, |
| 82 | |
| 83 | {{{ |
| 84 | $ make |
| 85 | make: Nothing to be done for `ALL'. |
| 86 | }}} |
| 87 | |
| 88 | '''make''' command does not do anything. |
| 89 | |
| 90 | {{{ |
| 91 | $ touch cg.h |
| 92 | $ ls -l |
| 93 | -rw-r--r-- 1 fuji staff 388 Sep 21 22:37 Makefile |
| 94 | -rw-r--r-- 1 fuji staff 2030 Sep 21 19:31 cg.c |
| 95 | -rw-r--r-- 1 fuji staff 838 Sep 22 09:51 cg.h |
| 96 | -rw-r--r-- 1 fuji staff 4768 Sep 21 22:37 cg.o |
| 97 | -rwxr-xr-x 1 fuji staff 13308 Sep 22 09:44 ex20 |
| 98 | -rw-r--r-- 1 fuji staff 3035 Sep 21 22:39 ex20.c |
| 99 | -rw-r--r-- 1 fuji staff 7728 Sep 22 09:44 ex20.o |
| 100 | }}} |
| 101 | |
| 102 | Now '''cg.h''' is newer than '''cg.o''', |
| 103 | {{{ |
| 104 | $ make |
| 105 | gcc -g -c cg.c |
| 106 | gcc -g -o ex20 ex20.o cg.o |
| 107 | }}} |
| 108 | '''make''' command generates '''cg.o''' then generates '''ex20'''. |
| 109 | |
| 110 | |
| 111 | == implicit rules == |
| 112 | General rule to make the *.o files from *.c files: |
| 113 | {{{ |
| 114 | %.o : %.c %.h |
| 115 | gcc -c $< |
| 116 | }}} |
| 117 | To make target |
| 118 | {{{ |
| 119 | ex20 : $(OBJ_EX20) |
| 120 | gcc -o $@ $(OBJ_EX20) |
| 121 | }}} |
| 122 | == variables / macros == |
| 123 | {{{#!make |
| 124 | CC = gcc |
| 125 | CFLAGS = -g |
| 126 | # |
| 127 | SRC_EX20 = ex20.c cg.c |
| 128 | SRC_EX21 = ex21.c cg.c |
| 129 | # |
| 130 | OBJ_EX20 = $(SRC_EX20:.c=.o) |
| 131 | OBJ_EX21 = $(SRC_EX21:.c=.o) |
| 132 | }}} |
| 133 | |
| 134 | [[http://www.gnu.org/software/make/manual/make.html|GNU make web pape]] |