Makefile很重要所以一定要学好Makefile,在编译内核或者bootloader时一定先看项层Makefile,我简单的写了几个,不正确的地方大家请指出。
1:内核驱动编译使用的makefile
obj-m := buttons.o //目标文件
KERNELDIR ?= /home/linux-2.6.24 //内核路径
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
2:编译测试代码时:
CROSS=arm-linux-
all: buttons
buttons:
$(CROSS)gcc -o buttons buttons_test.c
clean:
@rm -vf buttons *.o *~
3:我们在学习内核或者bootloader的时候一定要先把项层的Makefile看懂:
下面为VIVI的Makefile:
#make clean:清理中间生成文件,不包括配置文件
# make distclean:清理所有生成文件
# make config:文本配置,如果没有[.config],默认读取[arch/defconfig]
#make oldconfig: 文本配置,默认读取[.config]
# make menuconfig:图像配置,需要图形库的支持
#make debug:变量调试
#make vivi:编译,生成目标文件
#Modified by DaiCheng
#this document is only used for studying
VERSION = 0
PATCHLEVEL = 1
SUBLEVEL = 4
#版本号定义
VIVIRELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)
#将版本号传递给 VIVIRELEASE =0.1.4
# ARM架构
ARCH := arm
#定义shell的接口 用来去执行配置脚本scripts
CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
else if [ -x /bin/bash ]; then echo /bin/bash; \
else echo sh; fi ; fi)
TOPDIR := $(shell /bin/pwd)
#TOPDIR 为项层文件的指引 当编译Make时找到项层的makefile
#
# change this to point to the Linux include directory
#
LINUX_INCLUDE_DIR = /usr/local/arm/3.4.1/include
VIVIPATH = $(TOPDIR)/include
#编译工具 头文件 VIVI的头文件指引
HOSTCC = gcc
HOSTCFLAGS = -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer
CROSS_COMPILE = /usr/local/arm/3.4.1/bin/arm-linux-
#编译工具定义
# Include the make variables (CC, etc...)
AS = $(CROSS_COMPILE)as
LD = $(CROSS_COMPILE)ld
CC = $(CROSS_COMPILE)gcc
CPP = $(CC) -E
AR = $(CROSS_COMPILE)ar
NM = $(CROSS_COMPILE)nm
STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
MAKEFILES = $(TOPDIR)/.config
MD5SUM = md5sum
PERL = perl
AWK = awk
export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE \
CONFIG_SHELL TOPDIR VIVIPATH HOSTCC HOSTCFLAGS CROSS_COMPILE AS LD CC \
CPP AR NM STRIP OBJCOPY OBJDUMP MAKE MAKEFILES MD5SUM PERL AWK
all: do-it-all
ifeq (.config,$(wildcard .config))
include .config
else
CONFIGURATION = config
do-it-all: config
endif
do-it-all: Version vivi
# standard CFLAGS
#标准 CPPFLAGS
CPPFLAGS := -I$(VIVIPATH) -I$(LINUX_INCLUDE_DIR)
#CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes -Wno-trigraphs -O2 \
# -fomit-frame-pointer -fno-strict-aliasing -fno-common
#normal flags
CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes -O2 -fPIC -fomit-frame-pointer
#symbol table make up
#CFLAGS := $(CPPFLAGS) -Wall -Wstrict-prototypes -O2 -fPIC -fomit-frame-pointer -ggdb
AFLAGS := -D__ASSEMBLY__ $(CPPFLAGS)
#内核 文件
CORE_FILES = init/main.o init/version.o lib/lib.o
LIBS := lib/priv_data/priv_data.o
#SUBDIRS = drivers lib net
SUBDIRS = drivers lib net usb
NET :=net
USB :=usb
DRIVERS-y := drivers/net/dm9000.o
DRIVERS-$(CONFIG_SERIAL) += drivers/serial/serial.o
DRIVERS-$(CONFIG_MTD) += drivers/mtd/mtd.o
#DRIVERS-y+=drivers/net/dm9000_net.o
DRIVERS := $(DRIVERS-y)
#make clean 时删除 的东西
CLEAN_FILES = \
vivi-elf \
vivi \
vivi.nm \
vivi.map \
net/libnet.a
# Location of the gcc arm libs.
ARM_GCC_LIBS = /usr/local/arm/3.4.1/lib/gcc-lib/arm-linux/3.4.1
OBJCOPYFLAGS = -R .comment -R .stab -R .stabstr
CLIBS = -L$(ARM_GCC_LIBS) -L/usr/local/arm/3.4.1/arm-linux/lib -lc -lgcc
#laputa symbolic
LINKFLAGS = -Tarch/vivi.lds -Bstatic
#make distclean 时删除 的东西
DISTCLEAN_FILES = \
include/autoconf.h include/version.h \
scripts/lxdialog/*.o scripts/lxdialog/lxdialog \
.menuconfig.log \
.config .config.old
include arch/Makefile
export CPPFLAGS CFLAGS AFLAGS
export DRIVERS LDFLAGS
Version: dummy
@rm -f include/compile.h
vivi: include/version.h $(CONFIGURATION) init/main.o init/version.o linuxsubdirs
$(LD) -v $(LINKFLAGS) \
$(HEAD) \
$(CORE_FILES) \
$(LIBS) \
$(DRIVERS) \
$(USB)/usb.o \
$(NET)/netlib.o \
-o vivi-elf $(CLIBS)
$(NM) -v -l vivi-elf > vivi.map
$(OBJCOPY) -O binary -S vivi-elf vivi $(OBJCOPYFLAGS)
$(OBJDUMP) -D vivi-elf >asm.txt
oldconfig:
$(CONFIG_SHELL) scripts/Configure -d arch/config.in
config:
$(CONFIG_SHELL) scripts/Configure arch/config.in
menuconfig: include/version.h
$(MAKE) -C scripts/lxdialog all
$(CONFIG_SHELL) scripts/Menuconfig arch/config.in
clean:
find . \( -name '*.o' -o -name core -o -name ".*.flags" \) -type f -print \
| grep -v lxdialog/ | xargs rm -f
rm -f $(CLEAN_FILES)
distclean: clean
rm -f $(DISTCLEAN_FILES)
linuxsubdirs: $(patsubst %, _dir_%, $(SUBDIRS))
$(patsubst %, _dir_%, $(SUBDIRS)) : include/version.h
$(MAKE) CFLAGS="$(CFLAGS) $(CFLAGS_KERNEL)" -C $(patsubst _dir_%, %, $@)
$(TOPDIR)/include/version.h: include/version.h
$(TOPDIR)/include/compile.h: include/compile.h
include/compile.h: $(CONFIGURATION) include/version.h
@echo -n \#define UTS_VERSION \"\#$(VIVIRELEASE) > .ver
@if [ -f .name ]; then echo -n \-`cat .name` >> .ver; fi
@echo ' '`date`'"' >> .ver
@echo \#define VIVI_COMPILE_TIME \"`date +%T`\" >> .ver
@echo \#define VIVI_COMPILE_BY \"`whoami`\" >> .ver
@echo \#define VIVI_COMPILE_HOST \"`hostname`\" >> .ver
@if [ -x /bin/dnsdomainname ]; then \
echo \#define VIVI_COMPILE_DOMAIN \"`dnsdomainname`\"; \
elif [ -x /bin/domainname ]; then \
echo \#define VIVI_COMPILE_DOMAIN \"`domainname`\"; \
else \
echo \#define VIVI_COMPILE_DOMAIN ; \
fi >> .ver
@echo \#define VIVI_COMPILER \"`$(CC) $(CFLAGS) -v 2>&1 | tail -1`\" >> .ver
@mv -f .ver $@
include/version.h:
@echo \#define VIVI_RELEASE \"$(VIVIRELEASE)\" > .ver
@echo \#define VIVI_VERSION_CODE `expr $(VERSION) \\* 65536 + $(PATCHLEVEL) \\* 256 + $(SUBLEVEL)` >> .ver
@echo '#define VIVI_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))' >>.ver
@mv -f .ver $@
init/version.o: init/version.c include/compile.h
$(CC) $(CFLAGS) -DUTS_MACHINE='"$(ARCH)"' -c -o init/version.o init/version.c
init/main.o: init/main.c
$(CC) $(CFLAGS) $(CFLAGS_KERNEL) $(PROFILING) -c -o $*.o $<
%: ./arch/def-configs/%
$(MAKE) distclean
cp arch/def-configs/$* ./.config -f
$(MAKE) oldconfig
$(MAKE)
ifdef CONFIGURATION
..$(CONFIGURATION):
@echo
@echo "You have a bad or nonexistent" .$(CONFIGURATION) ": running 'make" $(CONFIGURATION)"'"
@echo
$(MAKE) $(CONFIGURATION)
@echo
@echo "Successful. Try re-making (ignore the error that follows)"
@echo
exit 1
dummy:
else
dummy:
endif
include Rules.make