2014-12-02 28 views
0

我想編寫一個makefile來編譯多個相互依賴的文件。該文件結構如下:Makefile引發重定位錯誤

- ext2.h 
- ext_helper.c 
- ext2_cp.c 
- ext2_ln.c 
- ext2_mkdir.c 
- ext2_rm 

每個源文件的依賴於ext2.h頭文件。最後四個文件彼此獨立,但取決於ext2_helper.c的一些幫助功能。

我試圖寫一個makefile對於這種情況如下:

ext2_cp : ext2_helper.o 
    gcc -Wall -g -o ext2_cp $^ 

ext2_mkdir : ext2_helper.o 
    gcc -Wall -g -o ext2_mkdir $^ 

ext2_ln : ext2_helper.o 
    gcc -Wall -g -o ext2_ln $^ 

ext2_rm : ext2_helper.o 
    gcc -Wall -g -o ext2_rm $^ 

%.o : %.c ext2.h 
    gcc -Wall -g -c $< 

clean : 
    rm *.o ext2_cp ext2_mkdir ext2_ln ext2_rm 

但是,在運行此生成的文件,我收到幾個錯誤回報搬遷如下:

make 
gcc -Wall -g -c ext2_helper.c 
gcc -Wall -g -o ext2_cp ext2_helper.o 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 10 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 11 
/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 10 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 2 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 2 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 11 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 19 
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start': 
(.text+0x20): undefined reference to `main' 
collect2: ld returned 1 exit status 
make: *** [ext2_cp] Error 1 

我已經看了一些其他有相同問題的帖子,但並沒有完全理解錯誤的原因。我將如何去解決這個問題?

+0

'的Makefile:2:***遺漏分隔符。 Stop.' – User 2015-12-09 16:07:32

回答

1

錯誤消息說明了這一切:您正嘗試創建沒有main()的可執行文件。看起來你忘記了你的依賴列表中的某些東西。

試試這個:

ext2_cp: ext2_cp.o ext2_helper.o 
    gcc -Wall -g -o ext2_cp $^ 

ext2_mkdir: ext2_mkdir.o ext2_helper.o 
    gcc -Wall -g -o ext2_mkdir $^ 

ext2_ln: ext2_ln.o ext2_helper.o 
    gcc -Wall -g -o ext2_ln $^ 

ext2_rm: ext2_rm.o ext2_helper.o 
    gcc -Wall -g -o ext2_rm $^ 

%.o: %.c ext2.h 
    gcc -Wall -g -c $< 

clean: 
    rm *.o ext2_cp ext2_mkdir ext2_ln ext2_rm 
+1

@mCode分隔符錯誤意味着您在使用此答案時忘記將空格修復爲製表符。 – 2014-12-02 17:11:03

+1

你不能從這裏複製/ pase到Makefile中,縮進的行需要一個'[tab]'字符(每行第二行:以'gcc'和'rm'開頭的行,它必須是一個製表符,而不是隻是空間)。 – xbug 2014-12-02 17:11:10

+1

謝謝你們兩位! – n0shadow 2014-12-02 17:12:18