1、编译所有子目录的makefile

经常有人需要顺序编译一个一个的模块,最后才连接生成可执行程序,但是如果一个模块一个模块地执行make,比较马法,下面是一个经过验证的makefile;
可以根据自己的需要修改SUBDIRS宏的值,设定自己需要编译的目录的顺序.就可以了.


#编译所有子目录
#SUBDIRS=`ls -d */ | grep -v 'bin' | grep -v 'lib' | grep -v 'include'`

#编译指定子目录
SUBDIRS=dir1 dir2 dir3

define make_subdir
 @for subdir in $(SUBDIRS) ; do \
 ( cd $$subdir && make $1) \
 done;
endef

all:
 $(call make_subdir , all)
 
install :
 $(call make_subdir , install)
 
debug:
 $(call make_subdir , debug)
clean:
 $(call make_subdir , clean)


2、gcc -I -L -l 的区别

       gcc -o hello hello.c -I /home/hello/include -L /home/hello/lib -lworld

       上面这句表示在编译hello.c时-I /home/hello/include表示将/home/hello/include目录作为第一个寻找头文件的目录,

   寻找的顺序是:/home/hello/include-->/usr/include-->/usr/local/include

   -L /home/hello/lib表示将/home/hello/lib目录作为第一个寻找库文件的目录,

   寻找的顺序是:/home/hello/lib-->/lib-->/usr/lib-->/usr/local/lib

       -lworld表示在上面的lib的路径中寻找libworld.so动态库文件(如果gcc编译选项中加入了“-static”表示寻找libworld.a静态库文件)


3、Makefile中的 符号 $@, $^, $<

  $@  表示目标文件
  $^  表示所有的依赖文件
  $<  表示第一个依赖文件
  $?  表示比目标还要新的依赖文件列表


4、wildcard、notdir、patsubst的意思

  wildcard : 扩展通配符
  notdir : 去除路径
  patsubst :替换通配符