为什么这个makefile不起作用?

时间:2016-12-23 02:56:15

标签: gcc compilation

我有两种相同源代码的makefile。 一个是工作,但另一个不工作。 我写下了根据gnu指南的makefile。 但我无法找出第二个makefile无法正常工作的原因。 你能告诉我原因吗? 我想要的最终makefile是只重新编译已更改的文件。

第一个makefile:工作。但它编译所有未更改文件的文件。

test:
        gcc -c test.c -o test.out
        gcc -c test2.c -o test2.out

clean:
        rm -rf test.out test2.out

第二个makefile:不工作

all: program
program: test.o test2.o
        gcc test.o test2.o -o program

test.o: test.c
        gcc -c test.c
test2.o: test2.c
        gcc -c test2.c

clean:
        rm -rf test.o test2.out

修改 工作意味着编译成功完成。 当我尝试用第二个makefile编译源代码时,会出现许多编译错误,如下所示:

gcc test.o test2.o -o program
test2.o: In function `paths':
test2.c:(.text+0x1ca): multiple definition of `paths'
test.o:test.c:(.text+0x1d2): first defined here
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
test.o: In function `dead_code':
test.c:(.text+0xce): undefined reference to `some_func'
test.o: In function `reverse_negative':
test.c:(.text+0x150): undefined reference to `some_func'
test.o: In function `paths':
test.c:(.text+0x1fc): undefined reference to `some_other_function'
test.c:(.text+0x21d): undefined reference to `yet_another_function'
test.c:(.text+0x239): undefined reference to `do_some_things'
test2.o: In function `dead_code2':
test2.c:(.text+0xc6): undefined reference to `some_func2'
test2.o: In function `negative_returns2':
test2.c:(.text+0x109): undefined reference to `read2'
test2.o: In function `reverse_negative2':
test2.c:(.text+0x148): undefined reference to `some_func2'
test2.o: In function `paths':
test2.c:(.text+0x1f4): undefined reference to `some_other_function'
test2.c:(.text+0x215): undefined reference to `yet_another_function'
test2.c:(.text+0x231): undefined reference to `do_some_things'
collect2: error: ld returned 1 exit status
make: *** [program] Error 1

1 个答案:

答案 0 :(得分:0)

不同之处如下:
在第一个makefile中,您正在编译2个分隔的 object文件,这就是全部。

  

command-line option -c is used to compile a source file to an object file,使用.o扩展程序代替.out也可以更好   目标文件。

在第二个makefile中,您尝试以相同的方式编译两个目标文件,但之后您尝试link它们可执行。
而且它的主要区别在于 在这种情况下,Linker找到multiple definition of和大量undefined reference

对于目标文件,它运行良好,因为在编译目标文件时,您并不关心unresolved references,因为它们可能会在链接阶段得到解决