Makefile学习(13)

今天就实践一下目标文件依赖在不同文件夹中的文件:

 

 

VPATH的作用:是指定我们在当前目录中如果找不到依赖文件,则在VPATH所指定的文件中找。比如:

我们我们的文件目录结构如下:

make(文件夹)

|——makefile(里面的文件依赖于submake1  submake2文件夹中的文件)

|——submake1(文件夹)

|——submake2(文件夹)

 

则我们只需要在makefile文件开头指定:

VPATH=./submake1 ./submake2

 

例如:

#my first makefile
VPATH= ./submake1 ./submake2
$(warning finish include)
jar:= f10.o  f11.o
$(warning start gcc)
f:f10.o f11.o
	gcc -c $(jar) -o f
f10.o: f.o f1.o f2.o f3.o
	@echo "gcc"
	gcc -o f10.o  f.o f1.o f2.o f3.o
f11.o:f7.o f5.o f6.o f4.o
	@echo "gcc"
	gcc -o f11.o f7.o f5.o f6.o f4.o
$(warning finish gcc)
.PHONY:clean
clean:
	rm f  *.o

 

通过以上代码我们执行的结果如下:

写道
makefile:3: finish include
makefile:5: start gcc
makefile:14: finish gcc
cc -c -o f7.o ./submake2/f7.c
cc -c -o f5.o ./submake2/f5.c
cc -c -o f6.o ./submake2/f6.c
cc -c -o f4.o ./submake2/f4.c
gcc
gcc -o f11.o f7.o f5.o f6.o f4.o
gcc -c f10.o f11.o -o f
gcc: f10.o: linker input file unused because linking not done
gcc: f11.o: linker input file unused because linking not done

 

 

对于其中的" linker input file unused because linking not done"这个问题,造成的原因是因为发f10.o f11.o也是目标文件,所以gcc -c  $(jar) -o f应改为  gcc -o f $(jar)。

 

 

修改后执行上一段代码:

执行make,生成了相应的f10.o  f11.o ,但是还报以下错误:

写道
makefile:3: finish include
makefile:5: start gcc
makefile:14: finish gcc
cc -c -o f.o ./submake1/f.c
cc -c -o f1.o ./submake1/f1.c
cc -c -o f2.o ./submake1/f2.c
cc -c -o f3.o ./submake1/f3.c
gcc
gcc -o f10.o f.o f1.o f2.o f3.o
cc -c -o f7.o ./submake2/f7.c
cc -c -o f5.o ./submake2/f5.c
cc -c -o f6.o ./submake2/f6.c
cc -c -o f4.o ./submake2/f4.c
gcc
gcc -o f11.o f7.o f5.o f6.o f4.o
gcc -o f f10.o f11.o
f10.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.text+0x0): first defined here
f10.o:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata+0x0): first defined here
f10.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.fini+0x0): first defined here
f10.o:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here
f10.o: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.data+0x0): first defined here
f10.o: In function `__data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i486-linux-gnu/4.4.3/crtbegin.o:(.data+0x0): first defined here
f10.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.init+0x0): first defined here
f11.o: In function `__libc_csu_fini':
(.text+0x130): multiple definition of `__libc_csu_fini'
f10.o:(.text+0x130): first defined here
f11.o: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.text+0x0): first defined here
f11.o:(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata+0x0): first defined here
f11.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.fini+0x0): first defined here
f11.o:(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here
f11.o: In function `__data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.data+0x0): first defined here
f11.o: In function `__data_start':
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/gcc/i486-linux-gnu/4.4.3/crtbegin.o:(.data+0x0): first defined here
f11.o:(.dtors+0x4): multiple definition of `__DTOR_END__'
f10.o:(.dtors+0x4): first defined here
f11.o: In function `__libc_csu_init':
(.text+0x140): multiple definition of `__libc_csu_init'
f10.o:(.text+0x140): first defined here
f11.o: In function `__i686.get_pc_thunk.bx':
(.text+0x19a): multiple definition of `__i686.get_pc_thunk.bx'
f10.o:(.text+0x19a): first defined here
f11.o: In function `main':
(.text+0xb4): multiple definition of `main'
f10.o:(.text+0xb4): first defined here
f11.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/i486-linux-gnu/4.4.3/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
f10.o:(.dtors+0x4): first defined here
collect2: ld returned 1 exit status
make: *** [f] 错误 1

重复定义,可是目前还是不知道怎么改!!!!


对几个依赖文件的说明:

f10.o依赖的文件中f.c包含main方法

f11.o依赖的文件中f7.c包含main方法

其他的文件中都是各自的方法

 

最终目标f依赖于f10.o  f11.0

 

 

 

网上找了找,说可能是我的c文件有问题,可是我不知道怎么改???

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(makefile)