2016-09-24 136 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 –