免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3039 | 回复: 7
打印 上一主题 下一主题

undifined reference for template [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-07 09:20 |只看该作者 |倒序浏览
a.h
  1. template<class T>
  2. class AList
  3. {
  4. public:
  5.         AList() {}
  6.         ~AList() {}

  7.         void display(T value); /* if implemented the routine as inline function, then that's ok */
  8. };
复制代码


a.cpp
  1. #include "a.h"

  2. template<class T>
  3. void AList<T>::display(T value)
  4. {
  5.         printf("value:%d\n", value);
  6. }
复制代码


main.cpp
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "a.h"

  4. typedef AList<int> iA;
  5. iA x;

  6. int main()
  7. {
  8.         x.display(10);
  9.         
  10.         exit(0);
  11. }
复制代码

complied
  1. $ g++ -c a.cpp -o a.o
  2. $ g++ -c main.cpp -o main.o
  3. $ g++ -o bin main.o a.o
  4. $ main.o(.text+0x1b): In function `main':
  5. : undefined reference to `AList<int>::display(int)'
  6. collect2: ld returned 1 exit status
  7. [george@ahapvr LuYanJun]$
复制代码

[ 本帖最后由 SybaseLU 于 2007-8-7 09:21 编辑 ]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2007-08-07 09:28 |只看该作者
一般情况下,模板实现要放在.h文件里的。

论坛徽章:
0
3 [报告]
发表于 2007-08-07 09:45 |只看该作者
Definitely, I try putting implementation of those functions in the header, everything goes well and work out nice. It's seem that only god know why.

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2007-08-07 10:03 |只看该作者
原帖由 SybaseLU 于 2007-8-7 09:45 发表
Definitely, I try putting implementation of those functions in the header, everything goes well and work out nice. It's seem that only god know why.

实现其实可以和声明分开的,
如果你想知道怎么做,
参考《C++ Templates: The Complete Guide》。

论坛徽章:
0
5 [报告]
发表于 2007-08-07 10:10 |只看该作者
编译main.cpp的时候
编译器不知道void display(int value)的定义
想让链接器去找
但是链接器没找到

论坛徽章:
0
6 [报告]
发表于 2007-08-07 11:41 |只看该作者
Ok, let's go ahead. as for *.cpp project you should need the extern "C" only if you are calling a  C function , vice versa,  you should do the same for  *.c project . for instance

b.cpp

  1. #include "a.h"

  2. typedef AList<int> iA;
  3. iA x;

  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif

  7. void OutputInfo()
  8. {
  9.         x.display(10);
  10. }

  11. #ifdef __cplusplus
  12. }
  13. #endif
复制代码


main.c

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. extern  void OutputInfo();

  4. int main()
  5. {
  6.         OutputInfo();
  7.         exit(0);
  8. }
复制代码

The result of compilation

  1. $ gcc -c b.cpp -o b.o
  2. $ gcc -c main.c -o main.o
  3. $ gcc -o bin main.o b.o
  4. b.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
  5. collect2: ld returned 1 exit status
  6. $

复制代码

I mean that you need extern "C" only if you are calling a fcuntion which be implemented within *.C/.cpp/.cc

[ 本帖最后由 SybaseLU 于 2007-8-7 11:45 编辑 ]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
7 [报告]
发表于 2007-08-07 11:43 |只看该作者
原帖由 SybaseLU 于 2007-8-7 11:41 发表
Ok, let's go ahead. as for *.cpp project you should need the extern "C" only if you are calling a  C function , vice versa,  you should do the same for  *.c project . for instance

b.cpp

#in ...

你觉得c程序里能出现类,对象,模板这些东西吗?

论坛徽章:
0
8 [报告]
发表于 2007-08-07 11:55 |只看该作者
1 如果想在*.c中调用函数,而这些函数在*.cpp/.C/.cc中实现的呢? 使用gcc。
2 在*.cpp/.C/.cc中使用函数,这些函数在*.c中实现的,同样需要extern "C", 使用g++
3 交叉编译

  1. $ arm-elf-gcc -c b.cpp -o b.o
  2. $ arm-elf-gcc -c main.c -o main.o
  3. $ arm-elf-gcc -o bin main.o b.o
  4. $
复制代码

为什么用arm-elf-gcc反而编译成功了,用gcc反而不行【BTW, the cross compilier was deprived from Sigma-design 】
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP