makefile实践三-为多目录源文件建立makefile

如何为多目录源文件建立makefile

只能举例说明:

1、代码源文件目录关系如下

manyFiletest/
    Complex/
        Complex.h
        Complex.m
    Fraction/
        Fraction.h
        Fraction.m
    Main.m


2、实践环境:GNUStep工具;代码语言:Objective-C

3、在manyFiletest建立manyfile.makefile
内如如下:
CC = gcc
CFLAGS = -g
LIBS = -lobjc -lgnustep-base
SRC=Complex/Complex.m Complex/Complex.h Fraction/Fraction.m Fraction/Fraction.h Main.m
OBJ=Complex/Complex.o Fraction/Fraction.o Main.o
HeaderPath=-I/GNUstep/System/Library/Headers
LibraryPath=-L/GNUstep/System/Library/Libraries
Constant=-fconstant-string-class=NSConstantString

ComplexMain:$(OBJ)
    $(CC) -o ComplexMain $(OBJ) $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
Complex/Complex.o:Complex/Complex.m Complex/Complex.h
    $(CC) -o Complex/Complex.o -c Complex/Complex.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
Fraction/Fraction.o:Fraction/Fraction.m Fraction/Fraction.h
    $(CC) -o Fraction/Fraction.o -c Fraction/Fraction.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
Main.o:main.m Fraction/Fraction.h Complex/Complex.h
    $(CC) -c main.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)


clean:
    rm ComplexMain $(OBJ)

make -f manyfile.makefile
实践结果如下图目录文件:
manyFiletest/
    Complex/
        Complex.h
        Complex.m
        Complex.o        生成
    Fraction/
        Fraction.h
        Fraction.m
        Fraction.o        生成
    Main.m
    Main.o                生成
    ComplexMain.exe        生成
    manyfile.makefile

也就是说目标文件创建在各自的目录下的

注意,在此makefile中
Complex/Complex.o:Complex/Complex.m Complex/Complex.h
    $(CC) -o Complex/Complex.o -c Complex/Complex.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)

如果要把目标文件创建在各自目下,需要-o Complex/Complex.o,且Complex/Complex.o:不能写成Complex.o:

4、为了简化manyfile.makefile,可以如下编写,新建manyfile01.makefile
CC = gcc
CFLAGS = -g
LIBS = -lobjc -lgnustep-base
HeaderPath=-I/GNUstep/System/Library/Headers
LibraryPath=-L/GNUstep/System/Library/Libraries
Constant=-fconstant-string-class=NSConstantString

P1=Complex/
P2=Fraction/

SRC=$(P1)Complex.m $(P1)Complex.h $(P2)Fraction.m $(P2)Fraction.h Main.m
OBJ=$(P1)Complex.o $(P2)Fraction.o Main.o

ComplexMain:$(OBJ)
    $(CC) -o ComplexMain $(OBJ) $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
$(P1)Complex.o:$(P1)Complex.m $(P1)Complex.h
    $(CC) -o $(P1)Complex.o -c $(P1)Complex.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
$(P2)Fraction.o:$(P2)Fraction.m $(P2)Fraction.h
    $(CC) -o $(P2)Fraction.o -c $(P2)Fraction.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
Main.o:main.m $(P2)Fraction.h $(P1)Complex.h
    $(CC) -c main.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)


clean:
    rm ComplexMain $(OBJ)

你可发现
P1=Complex/
P2=Fraction/

替换了对应的目录

使用make -f manyfile01.makefile也是成功的,目录效果同manyfile.makefile

5、继续实践
把所有目标文件建立在同一目录下
manyfile02.makefile
内容如下:
CC = gcc
CFLAGS = -g
LIBS = -lobjc -lgnustep-base
SRC=Complex/Complex.m Complex/Complex.h Fraction/Fraction.m Fraction/Fraction.h Main.m
OBJ=Complex.o Fraction.o Main.o
HeaderPath=-I/GNUstep/System/Library/Headers
LibraryPath=-L/GNUstep/System/Library/Libraries
Constant=-fconstant-string-class=NSConstantString

ComplexMain:$(OBJ)
    $(CC) -o ComplexMain $(OBJ) $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
Complex.o:Complex/Complex.m Complex/Complex.h
    $(CC) -c Complex/Complex.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
Fraction.o:Fraction/Fraction.m Fraction/Fraction.h
    $(CC) -c Fraction/Fraction.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
Main.o:main.m Fraction/Fraction.h Complex/Complex.h
    $(CC) -c main.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)


clean:
    rm ComplexMain $(OBJ)

make -f manyfile02.makefile
实践结果如下图目录文件:
manyFiletest/
    Complex/
        Complex.h
        Complex.m       
    Fraction/
        Fraction.h
        Fraction.m       
    Main.m
    Main.o                生成
    ComplexMain.exe        生成
    Fraction.o            生成
    Complex.o            生成
    manyfile.makefile
    manyfile01.makefile
    manyfile02.makefile

6、为了让gcc输出到特定的目录上
manyfile03.makefile
内容如下:
CC = gcc
CFLAGS = -g
LIBS = -lobjc -lgnustep-base
HeaderPath=-I/GNUstep/System/Library/Headers
LibraryPath=-L/GNUstep/System/Library/Libraries
Constant=-fconstant-string-class=NSConstantString

P1=Complex/
P2=Fraction/

ComplieOut=Out/

SRC=$(P1)Complex.m $(P1)Complex.h $(P2)Fraction.m $(P2)Fraction.h Main.m
OBJ=$(ComplieOut)Complex.o $(ComplieOut)Fraction.o $(ComplieOut)Main.o

$(ComplieOut)ComplexMain:$(OBJ)
    $(CC) -o $(ComplieOut)ComplexMain $(OBJ) $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
$(ComplieOut)Complex.o:$(P1)Complex.m $(P1)Complex.h
    $(CC) -o $(ComplieOut)Complex.o -c $(P1)Complex.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
$(ComplieOut)Fraction.o:$(P2)Fraction.m $(P2)Fraction.h
    $(CC) -o $(ComplieOut)Fraction.o -c $(P2)Fraction.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)
$(ComplieOut)Main.o:main.m $(P2)Fraction.h $(P1)Complex.h
    $(CC) -o $(ComplieOut)Main.o -c main.m $(HeaderPath) $(Constant) $(LibraryPath) $(LIBS)


clean:
    rm ComplexMain $(OBJ)

首先要先建立输出目录,与makefile同目录下

编译效果目录关系效果如下:
manyFiletest/
    Complex/
        Complex.h
        Complex.m       
    Fraction/
        Fraction.h
        Fraction.m       
    Main.m   
    manyfile.makefile
    manyfile01.makefile
    manyfile02.makefile
    manyfile03.makefile
    Out/
        Main.o                生成
        ComplexMain.exe        生成
        Fraction.o            生成
        Complex.o            生成

个人建议采用最后manyfile03.makefile来为多文件编译建立makefile

最后,说明一下:
简单的可以手工编写makefile,一般都是使用工具来处理,这里仅仅学习或应用在较少的文件处理
关于工具AutoTools的实践用法看看这个例子http://blogold.chinaunix.net/u/21948/showart_175235.html

另外网友说使用vpath来处理目录查询,我没有实践。
我看了
vpath %c func1:func1/func2:func1/func2/func3

意思是:把这3个目录下.c文件通配查询
ok,这个留到以后想学习的再说吧

posted @ 2011-04-22 11:41  西就东城  阅读(3224)  评论(0编辑  收藏  举报