Makefile 常见自动变量:
$* 不包括扩展名的目标文件名称
$+ 所有依赖的文件,以空格分开,并以出现的先后为序,可能包含重复的依赖文件
$? 所有时间戳比目标文件晚的依赖文件,并以空格分开
$< 第一个依赖文件的名称
$@ 目标文件的完整名称
$^ 所有不重复的依赖文件,以空格分开
$% 如果目标是归档成员,则该变量表示目标的归档成员名称
隐式规则:所有“.o”文件都可自动由“.c”文件使用命令
" $(CC) $(CPPFLAGS) $(CFLAGS) -c *.c -o *.o "生成。
注意:隐式规则只能查找到相同文件名的不同后缀名文件
模式规则:相关文件必须用 "%"标明
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
使用autotools 生成Makefile
1.首先创建目录automake ,将要生成Makefile的文件,本例hello.c拷贝到这个目录;
2.执行命令:autoscan,ls展开文件显示:autoscan.log configure.scan hello.c
3.修改configure.scan 为configure.in(configure.in文件要作相关的修改:
添加AC_INIT(hello,1.0) AM_AUTOMAKE(hello,1.0) AC_CONFIG_FILES([Makefile])
)
4.执行命令:aclocal,ls展开文件显示:autoscan.log configure.in hello.c aclocal.m4 autom4te.cache
5.执行命令:autoconf ,
ls展开文件显示:autoscan.log configure.in hello.c aclocal.m4 autom4te.cache configure
6.执行命令:autoheader(功能:从acconfig.h文件中复制用户附加的符号定义,本例中没有)
7.创建Makefile.am文件,内容如下:
AUTOMAKE_OPTIONS=foreign (可以设置三个级别foreign 、gnu、 gnits)
bin_PROGRAMS=hello(多个目标文件,另起一行)
hello_SOURCES=hello.c (多个源文件以空格隔开)
9.执行命令:automake --add-missing
ls展开文件显示:autoscan.log configure.in hello.c aclocal.m4 autom4te.cache configure
makefile.am makefile.in missing depcomp install-sh config.h.in
10.执行命令:./configure 就生成了makefile
ls展开文件显示:autoscan.log configure.in hello.c aclocal.m4 autom4te.cache configure
makefile.am makefile.in missing depcomp install-sh config.h.i makefile
命令:make dist 将程序和相关文档打包为一个压缩文档。
本例打包后:hello-1.0-tar.gz