Chinaunix

标题: unix makefile 的总结 [打印本页]

作者: 云杉上的蝴蝶    时间: 2009-10-21 00:54
标题: unix makefile 的总结
hello: main.cpp hello.cpp factorial.cpp
        $(CC) $(CFLAGS) $? $(LDFLAGS) -o [email=$@]$@
或者[/email]
hello: main.cpp hello.cpp factorial.cpp
        $(CC) $(CFLAGS) $@.cpp $(LDFLAGS) -o [email=$@o.cpp:]$@
o.cpp:[/email]        $(CC) $(CFLAGS) -c $<

或者
.o.cpp:
        $(CC) $(CFLAGS) -c $*.c

$@ is the name of the file to be made.  就是目标文件,在本例中就是 hello
$? is the names of the changed dependents. 比hello更新的源文件(被更新过).
$< the name of the related file that caused the action.
$* the prefix shared by target and dependent files

.foo.bar:
        tr \'[A-Z][a-z]\' \'[N-Z][A-M][n-z][a-m]\' < $< > $@
.c.o:
        $(CC) $(CFLAGS) -c $<


libs_for_gcc = -lgnu
normal_libs =

foo: $(objects)
ifeq ($(CC),gcc)
        $(CC) -o foo $(objects) $(libs_for_gcc)
else
        $(CC) -o foo $(objects) $(normal_libs)
endif
include foo *.mk $(bar)
The override DirectiveIf a variable has been set with a command argument then ordinary assignments in the makefile are ignored. If you want to set the variable in the makefile even though it was set with a command argument, you can use an override directive, which is a line that looks like this:
override variable := value
Recursive Use of make
subsystem:
        cd subdir && $(MAKE)
Communicating Variables to a Sub-make
export variable

This is an example of the Makefile for compiling the hello program. This program consists of three files main.cpp, factorial.cpp and hello.cpp.
# Define required macros here
SHELL = /bin/sh

OBJ =  main.o factorial.o hello.o
CFLAG = -Wall -g
CC = gcc
INCLUDE =
LIB = -lm

hello:${OBJ}
   ${CC} ${CFLAGS} ${INCLUDES} -o $@ ${OBJS} ${LIBS}

clean:
        -rm -f *.o core *.core

.cpp.o:
   ${CC} ${CFLAGS} ${INCLUDES} -c $<
order-only”依赖的使用举例:
    LIBS= libtest.a
foo : foo.c| $(LIBS)

      $(CC) $(CFLAGS) $< -o $@ $(LIBS)
make在执行这个规则时,如果目标文件“foo”已经存在。当“foo.c”被修改以后,目标“foo”将会被重建,但是当“libtest.a”被修改以后。将不执行规则的命令来重建目标“foo”。
就是说,规则中依赖文件$(LIBS)只有在目标文件不存在的情况下,才会参与规则的执行。当目标文件存在时此依赖不会参与规则的执行过程。


another Makefile example for solaris CC

fndef NO_PATTERN_RULES
%$(_g).a:
@echo Rebuilding $@
@if [ ! -d $(dir $@) ] ;\\
then \\
  mkdir -p $(dir $@) ;\\
fi;
time $(CC) -xar $(LDFLAGS) -o $@ $(filter%.o,$^)
%$(_g).so:
@echo Rebuilding $@
@if [ ! -d $(dir $@) ] ;\\
then \\
  mkdir -p $(dir $@) ;\\
fi;
time $(CC) -G -Kpic $(LDFLAGS) -o $@ $(filter%.o,$^)
endif
SS7FSM_SRC =
SS7FSM_OBJ = $(SS7FSM_SRC:.C=$(32)$(_g).o)

all: $(SS7FSM_LIBNAME)

$(SS7FSM_LIBNAME) : $(SS7FSM_OBJ)
作者: owood    时间: 2009-10-21 17:53
我喜欢总结的东西,从实践中来好




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2