2016-09-24 67 views
0

我一直在弄我的makefile代碼的錯誤,我不知道如何解決這個問題,我在網站上搜索了一個答案,並最終創建了一個賬戶來尋求幫助。 這裏是錯誤代碼'_start'的多重定義

date.o: In function `_start': 
(.text+0x0): multiple definition of `_start' 
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here 
date.o: In function `_fini': 
(.fini+0x0): multiple definition of `_fini' 
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o (.fini+0x0): first defined here 
date.o:(.rodata+0x0): multiple definition of `_IO_stdin_used' 
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o (.rodata.cst4+0x0): first defined here 
date.o: In function `data_start': 
(.data+0x0): multiple definition of `__data_start' 
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here 
date.o: In function `data_start': 
(.data+0x8): multiple definition of `__dso_handle' 
/usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o:(.data+0x0): first defined here 
date.o: In function `_init': 
(.init+0x0): multiple definition of `_init' 
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o (.init+0x0): first defined here 
/usr/lib/gcc/x86_64-linux-gnu/5/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__' 
date.o:(.data+0x10): first defined here 
/usr/bin/ld: error in date.o(.eh_frame); no .eh_frame_hdr table will be created. 
collect2: error: ld returned 1 exit status 
Makefile:3: recipe for target 'testdate' failed 
make: *** [testdate] Error 1 

和我的makefile

testdate: date.o testdate.o 
     g++ -Wall -o testdate.o date.o 

date.o: date.h date.cpp 
     g++ -Wall -c date.cpp 
testdate.o: date.h testdate.cpp 
     g++ -Wall -c testdate.cpp 
+0

看起來不相關的makefile文件,但關係到你的C++代碼,給我。 – mertyildiran

+1

您的第一條規則中的命令看起來不正確。 '-o'後面的單詞應該是g ++要創建的文件的名稱。 – Beta

回答

2

規則

testdate: date.o testdate.o 
     g++ -Wall -o testdate.o date.o 

應該

testdate: date.o testdate.o 
     g++ -Wall -o testdate testdate.o date.o 
#     ^^^^^^^^ 

或避免重複自己:

testdate: date.o testdate.o 
     g++ -Wall $^ -o [email protected] 

(這應該產生g++ -Wall date.o testdate.o -o testdate

事實上,你可能要考慮:

testdate: date.o testdate.o 
     g++ -Wall $^ -o [email protected] 
date.o: date.cpp date.h 
    g++ -Wall -c $< -o [email protected] 
testdate.o: testdate.cpp date.h 
    g++ -Wall -c $< -o [email protected] 

$^是所有的依賴關係,$<是ju st第一個和[email protected]是當前目標。

更多關於Makefile的規則:https://www.chemie.fu-berlin.de/chemnet/use/info/make/make_4.html

+0

您定義了$ ^和$ <,但是什麼是「$ @」? –

+0

啊,'$ @'是當前目標,所以'testdate:...''$ @'是'testdate'。 – kfsone

+0

我試着用你在第三個例子中發佈的確切代碼,它仍然給出了同樣的錯誤,有沒有辦法來防止makefile定義_start –