- 论坛徽章:
- 0
|
关于模板编译成.so,超难,各位帮忙,谢谢
liupch及各位兄弟,这是我的code,环境是Solaris,帮我试一下,谢谢,
t.h
- template <class T>;
- void AddBit(const char *instr,char *outstr,T &_t)
- {
- strcpy(outstr,instr);
- _t = 1;
- for(int i = 0; i < 5 ; i++)
- {
- printf("********************************");
- printf("outstr = %s",outstr);
- printf("********************************");
- }
- }
复制代码
t.cc
- #include <stdio.h>;
- #include <string.h>;
- #include <unistd.h>;
- #include "t.h"
- void TEST()
- {
- char instr[20];
- char outstr[20];
- int i;
- strcpy(instr,"fffff");
- AddBit(instr,outstr,i);
- printf("out = %s\n",outstr);
- }
复制代码
invoke.cc
- #include <stdio.h>;
- #include <string.h>;
- #include <dlfcn.h>;
- void *handle;
- void (*p)(const char *,char *);
- int main()
- {
- int i;
- char corig[8];
- strcpy(corig,"12345678");
- char cret[9];
- memset(cret,0x00,9);
- //invoke the function dynamicly
- if ((handle = dlopen("./libt.so", RTLD_LAZY)) != NULL)
- {
- p = (void(*)(const char *,char *))dlsym(handle, "TEST");
- if (p != NULL)
- {
- (p)(corig,cret);
- }
- else
- {
- printf("Get function poiter error!!\n");
- return 0;
- }
- }
- else
- {
- printf("open libt.so error!![%s]\n",dlerror());
- }
- return 0;
- }
复制代码
makefile
- VPATH=.
- CC=CC
- CFLAGS += -I./include
- LDFLAGS += -L. -L/usr/lib -ldl
- all: invoke libt.so
- libt.o: t.cc
- $(CC) $(CFLAGS) -c t.cc -o libt.o
- libt.so:libt.o
- ld -G -Bdynamic -o $@ libt.o
- invoke:$(EXE_SOURCE)
- $(CC) $(LDFLAGS) -o $@ invoke.cc
- clean:
- rm *.o
- rm *.so
- rm -r SunWS_cache
- .DONE:
复制代码
我这里报错:
open libt.so error!![ld.so.1: invoke: fatal: relocation error: file ./libt.so: symbol __1cGAddBit4Ci_6FpkcpcrTA_v_: referenced symbol not found] |
|