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文件有问题,可是我不知道怎么改???

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值