为什么这个makefile不起作用



我有两种用于同一源代码的makefile。一个在起作用,但另一个是不起作用的。我将Makefile Accortor授予GNU指南。但是我找不到第二个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

差异为:
在第一个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,因为它们可能会在链接阶段

上解决。

最新更新