makefile编写

来源:互联网 发布:js切割字符串 编辑:程序博客网 时间:2024/04/28 23:03



自动生成依赖性

在Makefile中,我们的依赖关系可能会需要包含一系列的头文件,比如,如果我们的main.c中有一句“#include "defs.h"”,那么我们的依赖关系应该是:

main.o : main.c defs.h

但是,如果是一个比较大型的工程,你必需清楚哪些C文件包含了哪些头文件,并且,你在加入或删除头文件时,也需要小心地修改Makefile,这是一个很没有维护性的工作。为了避免这种繁重而又容易出错的事情,我们可以使用C/C++编译的一个功能。大多数的C/C++编译器都支持一个“-M”的选项,即自动找寻源文件中包含的头文件,并生成一个依赖关系。例如,如果我们执行下面的命令:

cc -M main.c

其输出是:

main.o : main.c defs.h

于是由编译器自动生成的依赖关系,这样一来,你就不必再手动书写若干文件的依赖关系,而由编译器自动生成了。需要提醒一句的是,如果你使用GNU的C/C++编译器,你得用“-MM”参数,不然,“-M”参数会把一些标准库的头文件也包含进来。

gcc -M main.c的输出是:

main.o: main.c defs.h /usr/include/stdio.h /usr/include/features.h \    /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h \    /usr/lib/gcc-lib/i486-suse-linux/2.95.3/include/stddef.h \    /usr/include/bits/types.h /usr/include/bits/pthreadtypes.h \    /usr/include/bits/sched.h /usr/include/libio.h \    /usr/include/_G_config.h /usr/include/wchar.h \    /usr/include/bits/wchar.h /usr/include/gconv.h \    /usr/lib/gcc-lib/i486-suse-linux/2.95.3/include/stdarg.h \    /usr/include/bits/stdio_lim.h


gcc -MM main.c的输出则是:

main.o: main.c defs.h

那么,编译器的这个功能如何与我们的Makefile联系在一起呢。因为这样一来,我们的Makefile也要根据这些源文件重新生成,让 Makefile自已依赖于源文件?这个功能并不现实,不过我们可以有其它手段来迂回地实现这一功能。GNU组织建议把编译器为每一个源文件的自动生成的依赖关系放到一个文件中,为每一个“name.c”的文件都生成一个“name.d”的Makefile文件,[.d]文件中就存放对应[.c]文件的依赖关系。

于是,我们可以写出[.c]文件和[.d]文件的依赖关系,并让make自动更新或自成[.d]文件,并把其包含在我们的主Makefile中,这样,我们就可以自动化地生成每个文件的依赖关系了。

这里,我们给出了一个模式规则来产生[.d]文件:

%.d: %.c@set -e; rm -f $@; \         $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \         sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \         rm -f $@.$$$$


这个规则的意思是,所有的[.d]文件依赖于[.c]文件,“rm -f $@”的意思是删除所有的目标,也就是[.d]文件,第二行的意思是,为每个依赖文件“$<”,也就是[.c]文件生成依赖文件,“$@”表示模式“%.d”文件,如果有一个C文件是name.c,那么“%”就是 “name”,“$$$$”意为一个随机编号,第二行生成的文件有可能是“name.d.12345”,第三行使用sed命令做了一个替换,关于sed命令的用法请参看相关的使用文档。第四行就是删除临时文件。

总而言之,这个模式要做的事就是在编译器生成的依赖关系中加入[.d]文件的依赖,即把依赖关系:

main.o : main.c defs.h

转成:

main.o main.d : main.c defs.h

于是,我们的[.d]文件也会自动更新了,并会自动生成了,当然,你还可以在这个[.d]文件中加入的不只是依赖关系,包括生成的命令也可一并加入,让每个[.d]文件都包含一个完赖的规则。一旦我们完成这个工作,接下来,我们就要把这些自动生成的规则放进我们的主Makefile中。我们可以使用Makefile的“include”命令,来引入别的Makefile文件(前面讲过),例如:

sources = foo.c bar.cinclude $(sources:.c=.d)

上述语句中的“$(sources:.c=.d)”中的“.c=.d”的意思是做一个替换,把变量$(sources)所有[.c]的字串都替换成[.d],关于这个“替换”的内容,在后面我会有更为详细的讲述。当然,你得注意次序,因为include是按次来载入文件,最先载入的[.d]文件中的目标会成为默认目标。




#file start

SOURCES = $(wildcard *.c)

OBJS := $(patsubst %.c,%.o, $(SOURCES))

 

all: main

 

%.d: %.c

        @set -e; rm -f $@;\

        $(CC) -MM  $< > $@.$$$$; \

        sed 's,/($*/)/.o[ :]*,/1.o $@ : ,g' < $@.$$$$ > $@; \

        rm -f $@.$$$$

 

sinclude $(SOURCES:.c=.d)

 

main:$(OBJS)

        $(CC) -o main $(OBJS)

 

clean:

        rm -f *.o test *.d

#file end




gcc include路径  

2009-12-07 16:18:54|  分类:编程|举报|字号 订阅

《GCC:the complete referrence》说,gcc的include有以下几个组成:

On a UNIX system, the standard set of system directories is as follows:
/usr/local/include
/usr/lib/gcc-lib/target/version/include
/usr/target/include
/usr/include

通过 gcc -v   *.c 可以看编译时具体过程:
[hyang0@pek-wb-auto ~]$ gcc -v hello.c
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man--infodir=/usr/share/info --enable-shared --enable-threads=posix--enable-checking=release --with-system-zlib --enable-__cxa_atexit--disable-libunwind-exceptions --enable-libgcj-multifile--enable-languages=c,c++,objc,obj-c++,java,fortran,ada--enable-java-awt=gtk --disable-dssi --enable-plugin--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre--with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.1 20070105 (Red Hat 4.1.1-52)
/usr/libexec/gcc/i386-redhat-linux/4.1.1/cc1 -quiet -v hello.c -quiet-dumpbase hello.c -mtune=generic -auxbase hello -version -o/tmp/ccPUf6Jg.s
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc/i386-redhat-linux/4.1.1/include
/usr/include
End of search list.

GNU C version 4.1.1 20070105 (Red Hat 4.1.1-52) (i386-redhat-linux)
compiled by GNU C version 4.1.1 20070105 (Red Hat 4.1.1-52).
GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=129176
Compiler executable checksum: 92efef273c2911dfe2bff20f05a618b3
as -V -Qy -o /tmp/ccEed4Cr.o /tmp/ccPUf6Jg.s
GNU assembler version 2.17.50.0.6-2.el5 (i386-redhat-linux) using BFD version 2.17.50.0.6-2.el5 20061020
/usr/libexec/gcc/i386-redhat-linux/4.1.1/collect2 --eh-frame-hdr -melf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../crt1.o/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../crti.o/usr/lib/gcc/i386-redhat-linux/4.1.1/crtbegin.o-L/usr/lib/gcc/i386-redhat-linux/4.1.1-L/usr/lib/gcc/i386-redhat-linux/4.1.1-L/usr/lib/gcc/i386-redhat-linux/4.1.1/../../.. /tmp/ccEed4Cr.o -lgcc--as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s--no-as-needed /usr/lib/gcc/i386-redhat-linux/4.1.1/crtend.o/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../crtn.o
[hyang0@pek-wb-auto ~]$

可以看到search过程:
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/lib/gcc/i386-redhat-linux/4.1.1/include
/usr/include
End of search list.

如果要添加查找路径:
The following is a list of the environment variables that can be set to pass instructions
to the preprocessor.

C_INCLUDE_PATH, CPATH, CPLUS_INCLUDE_PATH, DEPENDENCIES_OUTPUT,
OBJC_INCLUDE_PATH, SUNPRO_DEPENDENCIES

对所有用户有效在/etc/profile增加以下内容。只对当前用户有效在Home目录下的
.bashrc或.bash_profile里增加下面的内容:
(注意:等号前面不要加空格,否则可能出现 command not found)

#在PATH中找到可执行文件程序的路径。
export PATH =$PATH:$HOME/bin

#gcc找到头文件的路径
C_INCLUDE_PATH=/usr/include/libxml2:/MyLib
export C_INCLUDE_PATH #(可在makefile中)

#g++找到头文件的路径
CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/include/libxml2:/MyLib
export CPLUS_INCLUDE_PATH

#找到动态链接库的路径
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/MyLib
export LD_LIBRARY_PATH

#找到静态库的路径
LIBRARY_PATH=$LIBRARY_PATH:/MyLib
export LIBRARY_PATH

连接器ld搜索路径和顺序
bash$  ld --verbose | grep SEARCH
SEARCH_DIR("/usr/i486-linux-gnu/lib32");SEARCH_DIR("/usr/local/lib32"); SEARCH_DIR("/lib32");SEARCH_DIR("/usr/lib32"); SEARCH_DIR("/usr/i486-linux-gnu/lib");SEARCH_DIR("/usr/local/lib"); SEARCH_DIR("/lib");SEARCH_DIR("/usr/lib");
bash$ 


0 0
原创粉丝点击