我的makefile代码一直出错,我不知道如何修复它,我在网站上搜索了一个答案并最终建立了一个帐户来寻求帮助 这是错误代码
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 :(得分: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 $@
(这应该产生g++ -Wall date.o testdate.o -o testdate
)
确实,您可能需要考虑:
testdate: date.o testdate.o
g++ -Wall $^ -o $@
date.o: date.cpp date.h
g++ -Wall -c $< -o $@
testdate.o: testdate.cpp date.h
g++ -Wall -c $< -o $@
$^
是所有依赖项,$<
只是第一个,$@
是当前目标。
有关Makefile规则的更多信息:https://www.chemie.fu-berlin.de/chemnet/use/info/make/make_4.html