
Makefile简介
网络上关于 makefile的教程有很多,由于我日常不是写c/c++的, 不常使用makefile,需要用的时候总是要重新Google搜索makefile的语法。 索性整理出来这篇 makefile 教程,备忘。 教程 Makefile简易教程: 基本语法 1 2 3 4 target: dependency1 dependency2 ... [TAB] action1 [TAB] action2 ... 下面的makefile摘抄自GNU Make in Detail for Beginners,这篇入门文章把makefile的语法写的非常透彻。 推荐大家多读几遍 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 ##### makefile for compile C programs # Compiler to use CC = gcc # -g for debug, -O2 for optimise and -Wall additonal messages OPTIONS = -O2 -g -Wall # Directory for header file INCLUDES = -I . # List of objects to be build OBJS = main.o module.o .PHONY: all list clean all: ${OBJS} @echo "Building..." # print "Building..." message ${CC} ${OPTIONS} ${INCLUDES} ${OBJS} -o main_bin %.o: %.c # '%' pattern wildcard matching ${CC} ${OPTIONS} ${INCLUDES} -c %.c list: @echo $(shell ls) # print output of command `ls` clean: @echo Cleaning up... -rm -rf *.0 # '-' prefix for ignoring errors and continue execution -rm main_bin #### makefile for img manage FILES = $(shell find imgs -type f -iname "*.jpg" | sed 's/imgs/thumb/g') CONVERT_CMD = convert -resize "100x100" $< $@ MSG = "\nUpdating thumbnail" $@ all_thumb: ${FILES} thumb/%.jpg: imgs/%.jpg ${MSG} ${CONVERT_CMD} thumb/%.JPG: imgs/%.JPG ${MSG} ${CONVERT_CMD} clean_all: @echo Cleaning up files... -rm -rf thumb/*.{jpg,JPG} 变量 赋值 Simple assignment (:=) We can assign values (RHS) to variables (LHS) with this operator, for example: CC := gcc. With simple assignment (:=), the value is expanded and stored to all occurrences in the Makefile when its first definition is found. ...