2012-04-07 110 views
3

我試圖編譯和運行C一個簡單的「Hello World」程序,我得到的一些C STDLIB功能鏈接錯誤在Ubuntu使用gcc 11.10

的.C多重定義的奇怪的錯誤 文件是:

#include <stdio.h> 
#include <stdlib.h> 

int main(int argc, char **argv) { 

    printf("hello world\n"); 
    return 0; 
} 

我用它來編譯和創建可執行的命令是:

gcc -Werror -Wall src/main.c -o bin/m.o 
gcc -Werror -Wall bin/m.o -o bin/m 

編譯部分去罰款,但在第二個命令,我在那裏調用GCC鏈接 創建可執行我收到以下錯誤:

bin/m.o: In function `_fini': 
(.fini+0x0): multiple definition of `_fini' 
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here 
bin/m.o: In function `__data_start': 
(.data+0x0): multiple definition of `__data_start' 
/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.1/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here 
bin/m.o: In function `__data_start': 
(.data+0x8): multiple definition of `__dso_handle' 
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/crtbegin.o:(.data+0x0): first defined here 
bin/m.o:(.rodata+0x0): multiple definition of `_IO_stdin_used' 
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/crt1.o:    (.rodata.cst4+0x0): first defined here 
bin/m.o: In function `_start': 
(.text+0x0): multiple definition of `_start' 
/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.6.1/../../../x86_64-linux-gnu/crt1.o:/build/buildd /eglibc-2.13/csu/../sysdeps/x86_64/elf/start.S:109: first defined here 
bin/m.o: In function `_init': 
(.init+0x0): multiple definition of `_init' 
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first  defined here 
/usr/lib/gcc/x86_64-linux-gnu/4.6.1/crtend.o:(.dtors+0x0): multiple definition of  `__DTOR_END__' 
bin/m.o:(.dtors+0x8): first defined here 
/usr/bin/ld: error in bin/m.o(.eh_frame); no .eh_frame_hdr table will be created. 
collect2: ld returned 1 exit status 

順便說一下,當我運行bash的執行前綴莫文件,它工作得很好

在C從不程序性或者任何東西,我試着開始,但是這讓我想起

回答

10

您需要編譯步驟中的-c,否則您的m.o對象將是完全鏈接的可執行文件。例如:

gcc -Werror -Wall -c src/main.c -o bin/m.o 
gcc -Werror -Wall bin/m.o -o bin/m 

如果你的程序是由的不僅僅是單一的翻譯單元更多的差別是顯而易見。如果你有file1.cfile2.c,例如:

gcc -c file1.c -o file1.o 
gcc -c file2.c -o file2.o 
gcc file1.o file2.o -o app 

是正確的,而如果你有:

gcc file1.c -o file1.o 
gcc file2.c -o file2.o 
gcc file1.o file2.o -o app 

兩個之前,你甚至搞到最後鏈接階段的第一行和第二行會失敗,因爲他們會丟失符號。

3
gcc -Werror -Wall src/main.c -o bin/m.o 
gcc -Werror -Wall bin/m.o -o bin/m 

第一個省略了-c選項,所以它創建了一個完全鏈接的可執行文件。 ELF對象的一個​​特點是可執行文件和目標文件之間沒有顯着差異,因此它對於另一個ld運行是完全有效的輸入 - 但是然後您將鏈接到完整程序中的符號與要鏈接的符號衝突進入新的一個。

課:只做編譯時使用gcc -c

gcc -Werror -Wall -c src/main.c -o bin/m.o # this needs -c so it only compiles 
gcc -Werror -Wall bin/m.o -o bin/m   # this links, so no -c