免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3935 | 回复: 1
打印 上一主题 下一主题

unix makefile 的总结 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-21 00:54 |只看该作者 |倒序浏览
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)

论坛徽章:
0
2 [报告]
发表于 2009-10-21 17:53 |只看该作者
我喜欢总结的东西,从实践中来好
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP