2016-12-23 186 views
0

我有兩種相同源代碼的makefile。 一個正在工作,但另一個不工作。 我寫下了符合GNU指南的makefile。 但我找不到第二個makefile不工作的原因。 你能讓我知道原因嗎? 我想要的最終生成文件是隻重新編譯更改的文件。爲什麼這個makefile不起作用?

1st makefile:working。但它會編譯所有文件沒有更改的文件。

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

clean: 
     rm -rf test.out test2.out 

第二生成文件:不工作

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 
+0

解決當你嘗試使用第二個Makefile時會發生什麼? – duskwuff

+0

您的第一個文件不會生成任何可執行程序。你爲什麼認爲它有效? – DyZ

回答

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,因爲他們可以在連接階段