免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2435 | 回复: 2

[C++] c++ 重定义问题 [复制链接]

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
发表于 2013-12-19 16:02 |显示全部楼层
  1. head.h

  2. class Base
  3. {
  4. public:
  5.    void f();
  6. /*
  7.    {
  8.      printf("Base::f()\n");         -------1
  9.    }
  10. */
  11. };

  12. void Base::f()
  13. {
  14.   printf("Base::f()\n");            -------2
  15. }

  16. s.cpp
  17. #include "header.h"
  18. int count()  
  19. {  
  20.     Base base;
  21.     base.f();
  22. }

  23. t.cpp
  24. #include "header.h"  
  25. int main()
  26. {
  27.     Base base;
  28.     base.f();
  29. }
复制代码
g++ -g -fno-inline -o test t.c s.c;  

error:
multiple definition of `Base::f()`

g++ -v
gcc 版本 4.1.2 20080704 (Red Hat 4.1.2-4

c++版本:
gnu++98

如果把2注释,加入1,则编译正确。

可以通过nm查看,发现:
当代码1有效时,则:
W _ZN4Base1fEv
当代码2有效时,则
T _ZN4Base1fEv

为什么代码2时,_ZN4Base1fEv变成strong引用? 谁知道gcc编译器有没有相关说明?c++标准又是怎样说明这些的呢?

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
发表于 2013-12-19 16:19 |显示全部楼层
1相当于 2加上inline修饰

论坛徽章:
1
申猴
日期:2014-02-11 14:50:31
发表于 2013-12-19 16:24 |显示全部楼层
回复 2# bruceteen


    细心看啊。
   -fno-inline
   Don’t pay attention to the "inline" keyword.  Normally this option is used to keep the compiler from expanding any functions inline.  Note that if you are not   optimizing, no functions can be expanded inline.

   且inline后nm是看不到符号的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP