hello_Makefile_c++

本文探讨了使用Makefile编译C++程序时遇到的多个定义错误和未定义引用问题,详细分析了错误原因,并提供了修改Makefile的具体方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、错误:

hello.o: In function `_start':

(.text+0x0): multiple definition of `_start'

/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.text+0x0): first defined here

hello.o:(.rodata+0x0): multiple definition of `_fp_hw'

/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata+0x0): first defined here

hello.o: In function `_fini':

(.fini+0x0): multiple definition of `_fini'

/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.fini+0x0): first defined here

hello.o:(.rodata+0x4): multiple definition of `_IO_stdin_used'

/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here

hello.o: In function `__data_start':

(.data+0x0): multiple definition of `__data_start'

/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.data+0x0): first defined here

hello.o: In function `__data_start':

(.data+0x4): multiple definition of `__dso_handle'

/usr/lib/gcc/i486-linux-gnu/4.4.3/crtbegin.o:(.data+0x0): first defined here

hello.o: In function `_init':

(.init+0x0): multiple definition of `_init'

/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.init+0x0): first defined here

/usr/lib/gcc/i486-linux-gnu/4.4.3/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'

hello.o:(.dtors+0x4): first defined here

/usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored.

/usr/bin/ld: error in hello.o(.eh_frame); no .eh_frame_hdr table will be created.

collect2: ld returned 1 exit status

make: *** [hello] Error 1

原因:

经测试:单独编译hello.cpp的时候不会报上面的错误,而使用Makefile时会报出上面的错误,说明是Makefile的原因;

原版的Makefile是:

hello:hello.o

        g++ -o hello hello.o

hello.o:hello.cpp hello.h

        g++ -o hello.o hello.cpp

clean:

        rm hello.o hello a.out

而修改后的Makefile为:

hello:hello.o

        g++ -o hello hello.o

hello.o:hello.cpp hello.h

        g++ -c hello.cpp

clean:

        rm hello.o hello a.out

导致的原因是:参数-c是生成.o文件的(只编译,不连接),-o是确定生成文件名称;

感悟:

Makefile对文件的编译是分步进行的:

a)         –c(只编译不连接);

b)        连接,生成执行文件;

2、错误:

/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

原因:一般出现这个问题是main打错了,或者没有写main,或者main被其他子函数包含了,注意检查下就好,我之前粗心,出现过这个问题,但是其他的文件gcc编译链接可以通过,就这个不能。

转载于:https://www.cnblogs.com/Joker714/p/4982160.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值