LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1366|回复: 3

如何学习makefile???有个makefile实在看不懂大牛来解析下?

[复制链接]
发表于 2009-4-7 19:54:17 | 显示全部楼层 |阅读模式
# Comment/uncomment the following line to disable/enable debugging
#DEBUG = y


# Add your debugging flag (or not) to CFLAGS
ifeq ($(DEBUG),y)
  DEBFLAGS = -O -g -DSBULL_DEBUG # "-O" is needed to expand inlines
else
  DEBFLAGS = -O2
endif

CFLAGS += $(DEBFLAGS)
CFLAGS += -I..

ifneq ($(KERNELRELEASE),)
# call from kernel build system

obj-m        := snull.o

else

KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD       := $(shell pwd)

default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif



clean:
        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

depend .depend dep:
        $(CC) $(CFLAGS) -M *.c > .depend


ifeq (.depend,$(wildcard .depend))
include .depend
endif
发表于 2009-4-10 17:50:50 | 显示全部楼层
学习中,期待详细解答
回复 支持 反对

使用道具 举报

发表于 2009-4-21 17:01:07 | 显示全部楼层
我也刚学, 不太懂. 不过makefile无非是一些宏替换和规则,

规则的基本格式:
  1. 目标 : 依赖项
  2.       命令
复制代码

示例:
  1. main.exe : main.cpp main.h
  2.       gcc main.cpp -o main.exe
复制代码
上面是说main.exe是一个生成目标, 它依赖于 main.cpp和main.h,
用来生成这个目标的命令是: gcc main.cpp -o main.exe

把它存为Makefile, 并在同一个目录敲入命令: make main.exe
就会执行"gcc main.cpp -o main.exe"这个命令, 把main.cpp编译成main.exe

宏替换就是那些美元符号$, 例如之前定义了:
CFLAGS = -O2
那么后面的$(CFLAGS)就会被替换为 -O2

楼主那段的其中一个目标:
  1. clean:
  2.     rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
复制代码

意思是目标"clean"没有依赖项,
对应的命令是: rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

也就是说, 在终端敲入: make clean 回车
就会执行那个命令, 删掉一堆东西
回复 支持 反对

使用道具 举报

发表于 2009-4-21 19:04:27 | 显示全部楼层
读O'Reilly的Managing Projects with GNU Make, 3rd Edition和官方手册GNU make: A Program for Directing Recompilation

虽然我两本都读了,并且也实践中使用着make,但是真的觉得它很复杂(那些隐晦的功能用法无法用正常的逻辑推理出来)。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表