免费注册 查看新帖 |

Chinaunix

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

关于模板编译成.so,超难,各位帮忙,谢谢 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-01-11 15:25 |只看该作者 |倒序浏览
各位前辈,在solaris上编译.so文件的时候,能否调用模板类》?

如tmplate1.h中定义了一个模板类,同时提供了一些公用方法

在toso.cc中
#include “tmplate1.h”
然后将toso编译成一个libt.so文件并调用其方法,结果在dlopen的时候报错,提示
open libt.so error,strerr = ld.so.1: m: fatal: relocation error: file ./libt.so: symbol __1cLCDch4Ci_Fch6Mpkcri_b_: referenced symbol not found!!

CDch是模板中的类名,谁知道是怎么回事啊?多谢

论坛徽章:
0
2 [报告]
发表于 2005-01-11 17:34 |只看该作者

关于模板编译成.so,超难,各位帮忙,谢谢

你的libt.so放在什么路径下了?是/usr/lib下么?

论坛徽章:
0
3 [报告]
发表于 2005-01-11 17:41 |只看该作者

关于模板编译成.so,超难,各位帮忙,谢谢

lib在本地路径下,肯定是可以找到的

编译.so文件的时候,能否调用模板类的函数?有人这样用过吗?

论坛徽章:
0
4 [报告]
发表于 2005-01-11 17:56 |只看该作者

关于模板编译成.so,超难,各位帮忙,谢谢

lib在本地路径下你怎么确定肯定可以找到呢?
你ls看到和程序里面动态加载是2码事呀

论坛徽章:
0
5 [报告]
发表于 2005-01-11 17:58 |只看该作者

关于模板编译成.so,超难,各位帮忙,谢谢

liupch及各位兄弟,这是我的code,环境是Solaris,帮我试一下,谢谢,

t.h

  1. template <class T>;
  2. void AddBit(const char *instr,char *outstr,T &_t)
  3. {
  4.         strcpy(outstr,instr);
  5.         _t = 1;
  6.         for(int i = 0; i < 5 ; i++)
  7.         {
  8.                 printf("********************************");
  9.                 printf("outstr = %s",outstr);
  10.                 printf("********************************");
  11.         }
  12. }
复制代码


t.cc
  1. #include <stdio.h>;
  2. #include <string.h>;
  3. #include <unistd.h>;
  4. #include "t.h"
  5. void TEST()
  6. {
  7.         char instr[20];
  8.         char outstr[20];
  9.         int i;
  10.         strcpy(instr,"fffff");
  11.         AddBit(instr,outstr,i);
  12.         printf("out = %s\n",outstr);
  13. }
复制代码


invoke.cc
  1. #include <stdio.h>;
  2. #include <string.h>;
  3. #include <dlfcn.h>;

  4. void *handle;
  5. void (*p)(const char *,char *);
  6. int main()
  7. {
  8.         int i;
  9.         char corig[8];
  10.         strcpy(corig,"12345678");
  11.         char cret[9];
  12.         memset(cret,0x00,9);

  13.         //invoke the function dynamicly
  14.         if ((handle = dlopen("./libt.so", RTLD_LAZY)) != NULL)
  15.         {
  16.                 p = (void(*)(const char *,char *))dlsym(handle, "TEST");
  17.                 if (p != NULL)
  18.                 {
  19.                         (p)(corig,cret);
  20.                 }
  21.                 else
  22.                 {
  23.                         printf("Get function poiter error!!\n");
  24.                         return 0;
  25.                 }
  26.         }
  27.         else
  28.         {
  29.                 printf("open libt.so error!![%s]\n",dlerror());
  30.         }

  31.         return 0;
  32. }
复制代码


makefile

  1. VPATH=.

  2. CC=CC
  3. CFLAGS += -I./include

  4. LDFLAGS += -L. -L/usr/lib -ldl

  5. all: invoke libt.so

  6. libt.o: t.cc
  7.         $(CC) $(CFLAGS) -c t.cc -o libt.o

  8. libt.so:libt.o
  9.         ld -G -Bdynamic -o $@ libt.o

  10. invoke:$(EXE_SOURCE)
  11.         $(CC) $(LDFLAGS) -o $@ invoke.cc

  12. clean:
  13.         rm *.o
  14.         rm *.so
  15.         rm -r SunWS_cache
  16. .DONE:
复制代码


我这里报错:
open libt.so error!![ld.so.1: invoke: fatal: relocation error: file ./libt.so: symbol __1cGAddBit4Ci_6FpkcpcrTA_v_: referenced symbol not found]

论坛徽章:
0
6 [报告]
发表于 2005-01-11 18:08 |只看该作者

关于模板编译成.so,超难,各位帮忙,谢谢

我这是S9,gcc编译
运行后
Get function poiter error!!

论坛徽章:
0
7 [报告]
发表于 2005-01-11 19:24 |只看该作者

关于模板编译成.so,超难,各位帮忙,谢谢

我的make文件有没有问题?我用的是CC编译器。
不知道是怎么回事啊?谁知道
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP