关于多处include头文件出现multiple definition的问题原因分析和解决方法

之前使用C++时也经常出现multiple definition的问题,搜了一圈基本上是在#include的时候加上#ifndef或者#paraonce

 

Scanning dependencies of target ieskf
[ 33%] Building CXX object CMakeFiles/ieskf.dir/src/main.o
[ 66%] Building CXX object CMakeFiles/ieskf.dir/src/ieskf.o
[100%] Linking CXX executable ieskf
CMakeFiles/ieskf.dir/src/ieskf.o: In function `ieskf::Ieskf::Ieskf()':
ieskf.cpp:(.text+0x0): multiple definition of `ieskf::Ieskf::Ieskf()'
CMakeFiles/ieskf.dir/src/main.o:main.cpp:(.text+0x0): first defined here
CMakeFiles/ieskf.dir/src/ieskf.o: In function `ieskf::Ieskf::Ieskf()':
ieskf.cpp:(.text+0x0): multiple definition of `ieskf::Ieskf::Ieskf()'
CMakeFiles/ieskf.dir/src/main.o:main.cpp:(.text+0x0): first defined here
CMakeFiles/ieskf.dir/src/ieskf.o: In function `ieskf::Ieskf::~Ieskf()':
ieskf.cpp:(.text+0x10): multiple definition of `ieskf::Ieskf::~Ieskf()'
CMakeFiles/ieskf.dir/src/main.o:main.cpp:(.text+0x10): first defined here
CMakeFiles/ieskf.dir/src/ieskf.o: In function `ieskf::Ieskf::~Ieskf()':
ieskf.cpp:(.text+0x10): multiple definition of `ieskf::Ieskf::~Ieskf()'
CMakeFiles/ieskf.dir/src/main.o:main.cpp:(.text+0x10): first defined here
collect2: error: ld returned 1 exit status
CMakeFiles/ieskf.dir/build.make:120: recipe for target 'ieskf' failed
make[2]: *** [ieskf] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/ieskf.dir/all' failed
make[1]: *** [CMakeFiles/ieskf.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

其实真正原因是编译链接时候出错,认真看编译结果:已经100%完成了,在链接的时候,由于两个.o文件中含有同名文件,所以会链接报错,参考上篇文章的static和extern用法,可以解决这个问题。

https://blog.csdn.net/mxdsdo09/article/details/107047523

其他参考资料:

https://blog.csdn.net/mantis_1984/article/details/53571758

https://www.cnblogs.com/longdouhzt/archive/2012/07/03/2574071.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文锦渡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值