Changes between Initial Version and Version 1 of Workshops/IntroToHpc2015/Programming/MakeFile


Ignore:
Timestamp:
Oct 12, 2015 4:03:55 PM (6 years ago)
Author:
pdejesus
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Workshops/IntroToHpc2015/Programming/MakeFile

    v1 v1  
     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 ==
     21Typical element in the simple Makefile:
     22
     23{{{
     24target: dependencies
     25[TAB] command to make target
     26}}}
     27
     28It is important to use tab character, not spaces.
     29
     30command '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
     34For example:
     35{{{#!make
     36#
     37# Large Scale Computing
     38# Heat / Mass Transfer
     39#
     40ALL: ex20 ex21
     41#
     42CC = gcc
     43CFLAGS = -g
     44#
     45SRC_EX20 = ex20.c cg.c
     46SRC_EX21 = ex21.c cg.c
     47#
     48OBJ_EX20 = $(SRC_EX20:.c=.o)
     49OBJ_EX21 = $(SRC_EX21:.c=.o)
     50#
     51ex20 : $(OBJ_EX20)
     52        $(CC) $(CFLAGS) -o $@ $(OBJ_EX20)
     53 
     54ex21 : $(OBJ_EX21)
     55        $(CC) $(CFLAGS) -o $@ $(OBJ_EX21)
     56#
     57%.o : %.c
     58        $(CC) $(CFLAGS) -c $<
     59cg.o : cg.c cg.h
     60        $(CC) $(CFLAGS) -c cg.c
     61clean: 
     62        rm -f *.o ex20
     63}}}
     64lines begin with # is comments
     65
     66== Age of dependency ==
     67The 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'''
     81Since all target files are newer than dependencies,
     82
     83{{{
     84$ make
     85make: 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
     102Now '''cg.h''' is newer than '''cg.o''',
     103{{{
     104$ make
     105gcc -g -c cg.c
     106gcc -g -o ex20 ex20.o cg.o
     107}}}
     108'''make''' command generates '''cg.o''' then generates '''ex20'''.
     109
     110
     111== implicit rules ==
     112General rule to make the *.o files from *.c files:
     113{{{
     114%.o : %.c %.h
     115        gcc  -c $<
     116}}}
     117To make target
     118{{{
     119ex20 : $(OBJ_EX20)
     120        gcc -o $@ $(OBJ_EX20)
     121}}}
     122== variables / macros ==
     123{{{#!make
     124CC = gcc
     125CFLAGS = -g
     126#
     127SRC_EX20 = ex20.c cg.c
     128SRC_EX21 = ex21.c cg.c
     129#
     130OBJ_EX20 = $(SRC_EX20:.c=.o)
     131OBJ_EX21 = $(SRC_EX21:.c=.o)
     132}}}
     133
     134[[http://www.gnu.org/software/make/manual/make.html|GNU make web pape]]