Linux 环境下 Makefile 文件制作浅谈(二)(转)[@more@]
编写:Leaf Zhou
EMAIL:leaf_zhou_8@hotmail.com
可自由复制但禁止删改
2003-10-15
在介绍了简单的如何产生Makefile文件之后,相信已经能够编写Makefile文件了,但那还远
远不够,因为编写那么简单的Makefile文件还要如此繁琐和兴师动众,那未免小题大做了。因此,
我们有必要进一步了解如何制作Makefile文件。在制作Makefile文件的过程中,编写configure.in
文件是关键,因此这一部分将重点介绍configure.in文件的编写的相关问题。对于Makefile.am文
件的编写在编译多文件和多目录中要用到,所以下一篇将会介绍。
在打开生成的Makefile文件,才发现能够编译的目标(target)有很多,因此有必要介绍一下
其中比较重要而且常用的目标(target)的含义,也就是说,如何使用这个Makefile文件。
1〉make 或 make all
开始执行编译过程,产生我们设定的目标。这时会开始编译必要的源代码文件,然后进
行连结,并且最终生成可执行文件或我们想要生成库文件。 具体命令行如下:
[root@localhost testmk]#make
或
[root@localhost testmk]#make all
2〉make dist
将程序的源代码和相关文档打包成一个压缩文件,以用来备份源代码文件。命令完成后
会在目录下会产生一个以 PACKAGE-VERSION.tar.gz 为名称的打包文件。PACKAGE 和 VERSION
这两个变量是在configure.in文件中定义的。在我们的例子中定义如下:
AM_INIT_AUTOMAKE(hello,1.0)
因此会在目录中生成一个名为hello-1.0.tar.gz的文件。具体命令行如下:
[root@localhost testmk]#make dist
3〉make install
将正确编译生成的可执行文件或库文件安装到系统中,通常是/usr/local/bin这个目录。
如果没有需要安装的可执行文件或库文件,将会自动执行make命令进行编译,然后再进行安装操
作。具体命令行如下:
[root@localhost testmk]#make install
4〉make clean
清除之前所编译的可执行文件以及目标文件(Object Files, *.o)。 具体命令行如下:
[root@localhost testmk]#make clean
5〉make distclean
清除之前所编译的可执行文件、目标文件(Object Files, *.o)以及由执行./configure
所产生的 Makefile文件。 具体命令行如下:
[root@localhost testmk]#make distclean
在弄清Makefile文件如何使用之后,我们来进一步了解生成Makefile文件的有关问题。先看
一下源文件的结构和内容:
/hello-1.0
/hello-1.0/pubfun.h
/hello-1.0/pubfun.c
/hello-1.0/hello.c
-------------------------------------------------
/*filename:pubfun.h */
#include
void *printA(void *pdata);-------------------------------------------------/*filename:pubfun.c */#include #include #include void *printA(void *pdata){printf("%f
",sin(2));printf("Hello,World -->%d
",getpid());} -------------------------------------------------/*filename:hello.c */#include #include #include "pubfun.h"int main(int argc,char **argv){int ret;pthread_t ptid;int index;// create a threadret = pthread_create(&ptid,NULL,printA,(void *)&index);if(ret)return -1;printA(NULL);return 0;} ---------------------------------------------------对于我们的源文件中用到了数学库和线程库,还有我们自己写的头文件,我们修改configure.in 文件如下:01:dnl Process this file with autoconf to produce a configure script.02:AC_INIT(hello.c)03:04:dnl Add the file by leaf05:AM_INIT_AUTOMAKE(hello,1.0)06:07:dnl 检查C编译器.如果在环境中没有设定CC,就查找gcc,如果没有找到,就使用cc.08:AC_PROG_CC09:10: dnl 为C编译器提供的调试和优化选项.11: CFLAGS=" -O2"12:13: dnl 为C预处理器和编译器提供头文件搜索目录选项('-Idir')以及其他各种选项.14: CPPFLAGS=" -I."15:16: dnl 自定义输出的检查信息17:AC_MSG_CHECKING([for architecture type])18:19:dnl 输出检查结果20: AC_MSG_RESULT([ok])21:22: dnl 传递给连接器的'-l'和'-L'选项.23: LIBS=" -L."24:25:dnl Checks for libraries,Might abort.26:AC_CHECK_LIB(m,sin,[LIBS="$LIBS -lm"],exit 1)27: AC_CHECK_LIB(pthread,pthread_create,[LIBS="$LIBS -pthread"],exit 1)28: dnl AC_CHECK_LIB(socket, connect)29:30:dnl Checks for header files.(检查头文件是否存在)31:AC_CHECK_HEADERS(sys/socket.h)32:33:dnl Checks for typedefs, structures, and compiler characteristics.34:35:dnl Checks for library functions.36:37:AC_OUTPUT(Makefile) 这个configure.in文件中使用了几个常用的宏,还有一些如AC_ARG_ENABLE宏、AC_MSG_ERROR 宏、AC_MSG_RESULT宏、AM_PATH_GTK宏等有用的宏,你可以在附带的附件文件里查找,以便使用。这里我要重点提一下第26到28行,对于程序中需要用到一些特定的库,需要在编译时进行指定,否则会出现连结错误。例如第26行就对数学库进行了检查,如果没有,将退出Makefile文件的生成,因为找不到数学库生成也编译不过去;同理第27行对线程库进行了检查,第28行对socket库进行了检查(被注释掉的原因是引用的例子中没用到此库)。这里面用到了AC_CHECK_LIB宏。具体用法如下:AC_CHECK_LIB(库名称,需要库中的函数,[如果找到,[如果没找到]])在这个宏中的[库名称]实在编译时 -l 选项后面的名称,如数学库 -lm 就用 m 就行了。在例子中,如果找到了库,就在编译选项中添加 -lm 选项。还是如上一篇中所述的那样,执行aclocal和autoconf命令,然后再创建并编辑Makefile.am文件内容如下:AUTOMAKE_OPTIONS=foreignbin_PROGRAMS=hellohello_SOURCES=hello.c pubfun.c pubfun.h Makefile.am文件和上一篇中相比就增加了源代码的数量,其他都没有改变。 然后再执行automake --add-missing即可。信息如下:[root@leaf hello-1.0]# ./configurechecking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking for gcc... gccchecking for C compiler default output... a.outchecking whether the C compiler works... yeschecking whether we are cross compiling... nochecking for suffix of executables...checking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ANSI C... none neededchecking for style of include used by make... GNUchecking dependency style of gcc... gcc3checking for sin in -lm... yeschecking for pthread_create in -lpthread... yesconfigure: creating ./config.statusconfig.status: creating Makefileconfig.status: executing depfiles commands[root@leaf hello-1.0]# makesource='hello.c' object='hello.o' libtool=no depfile='.deps/hello.Po' tmpdepfile='.deps/hello.TPo' depmode=gcc3 /bin/sh ./depcomp gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING="" -DPACKAGE_BUGREPORT="" -DPACKAGE="hello" -DVERSION="1.0" -I.-I. -I. -O2 -c `test -f 'hello.c' || echo './'`hello.csource='pubfun.c' object='pubfun.o' libtool=no depfile='.deps/pubfun.Po' tmpdepfile='.deps/pubfun.TPo' depmode=gcc3 /bin/sh ./depcomp gcc -DPACKAGE_NAME="" -DPACKAGE_TARNAME="" -DPACKAGE_VERSION="" -DPACKAGE_STRING=""-DPACKAGE_BUGREPORT="" -DPACKAGE="hello" -DVERSION="1.0" -I.-I. -I. -O2 -c `test -f 'pubfun.c' || echo './'`pubfun.cgcc -O2 -o hello hello.o pubfun.o -L. -lm -pthread 备注: 1.以上内容均在RedHat Linux 9.0环境下测试通过。2.详细的选项可参考由王立翻译的文档http://www.cngnu.org/technology/1657/297.html。3.其它国内外网站资料4.RedHat 9.0下带的程序文件及版本autoconf-2.57-3.noarch.rpmautomake-1.6.3-5.noarch.rpmgcc-3.2.2-5.i386.rpm5.附件源文件用如下命令打开即可:tar xvzf hello-1.0.tar.gz
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/8225414/viewspace-944615/
,如需转载,请注明出处,否则将追究法律责任。