c++ - '_start'的多重定义

标签 c++ ubuntu makefile

我的 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

最佳答案

规则

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

关于c++ - '_start'的多重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39681859/

相关文章:

c++ - 寻求将构建工具链从 bash/grep/sed/awk/(auto)make/configure 重新实现为更理智的东西(例如 boost.build 等)

c++ - VideoWriter 不写任何东西

c - 使用 make 文件在 c 中的单独目录中生成输出文件

c++ - constexpr with operator|=

c++ - boost 线程禁用

c++ - 什么时候在C++中生成默认构造函数

c++ - 将重载函数转换为专用函数模板

Python:在 Ubuntu 上覆盖 os.path.supports_unicode_filenames

php - 调用未定义函数 imagecreatefromjpeg() 并启用 GD

Makefile 依赖项不适用于虚假目标