Chinaunix

标题: undifined reference for template [打印本页]

作者: SybaseLU    时间: 2007-08-07 09:20
标题: undifined reference for template
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 编辑 ]
作者: lenovo    时间: 2007-08-07 09:28
一般情况下,模板实现要放在.h文件里的。
作者: SybaseLU    时间: 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.
作者: lenovo    时间: 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》。
作者: xujg    时间: 2007-08-07 10:10
编译main.cpp的时候
编译器不知道void display(int value)的定义
想让链接器去找
但是链接器没找到
作者: SybaseLU    时间: 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 编辑 ]
作者: lenovo    时间: 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程序里能出现类,对象,模板这些东西吗?
作者: SybaseLU    时间: 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 】




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2