LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 838|回复: 4

为什么我不能使用我创建的so,哪里出问题了?

[复制链接]
发表于 2006-3-26 13:42:12 | 显示全部楼层 |阅读模式
//print.h
#ifdef  SHARED
        int (*PrintInt)(int i);
        int (*PrintFloat)(float f);
#else
        int (PrintInt)( int i);
        int (PrintFloat)(float f);
#endif

//printfloat.c
#include "print.h"
#include "iostream.h"
int  PrintFloat(float f)
{
        cout<<"f:"<<f<<endl;
        return 1;
}

//printint.c
#include "print.h"
#include "iostream.h"
int  PrintInt(int i)
{
        cout<<"i:"<<i<<endl;
        return 1;
}

//makefile_so
OBJO= printint.o printfloat.o
TARGET= print.so

all : print.so

printint.o: print.h  printint.c
        g++ -c printint.o printint.c

printfloat.o: print.h printfloat.c
        g++ -c  printfloat.o printfloat.c
       
print.so: printint.o printfloat.o
        g++ -shared -o $(TARGET) $(OBJO)
//使用上面的东西生成print.so

//useprintso.c
#define  SHARED
#include "dlfcn.h"
#include "print.h"
//#include "iostream.h"
#include "stdio.h"

int main()
{
        void *handle;
//        int (*PrintInt)(int i);
//        int (*PrintFloat)(float f);

        handle=dlopen("./print.so", RTLD_LAZY);
        if(handle == NULL)
        {
                //cout<<"open dll error:"<<dlerror()<<endl;
                printf("open dll error %s \n", dlerror());
                dlclose(handle);
                exit(1);
        }

        PrintInt=dlsym(handle,"rintInt");
        if(PrintInt==NULL)
        {
                printf("get printint  error %s \n", dlerror());
                dlclose(handle);
                exit(1);
        }
        PrintInt(10);

        PrintFloat = dlsym(handle, "rintFloat");

        if(PrintFloat==NULL)
        {
                printf("get PrintFloat  error %s \n", dlerror());
                dlclose(handle);
                exit(1);
        }

        PrintFloat(10.1);

        dlclose(handle);
}

//生成app.out
gcc  -rdynamic  -s -o app.out  useprintso.c  -ldl  

//执行文件
./app.out

//提示错误:
./print.so undefine PrintInt

//然后我用命令
ldd app.out
发现app.out的依赖的动态库没有print.so

各位。。。我应该怎么处理呢?哪里出问题了呢??
 楼主| 发表于 2006-3-26 14:31:11 | 显示全部楼层
可是我用 nm app.out来查看该文件的外部符号表找到了PrintInt 和PrintFloat这两项

为什么app.out依赖的动态链接库就没有print.so呢??

应该怎么设置 gcc的参数呢?
回复 支持 反对

使用道具 举报

发表于 2006-3-26 15:24:56 | 显示全部楼层
dlopen打开的用ldd是看不出依赖关系的
只有连接时用-l加进来的才是ldd能看到的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-26 15:29:51 | 显示全部楼层
打扰各位了..
自己搜了不少网页,终于发现了错误.
原来是了 库的命名格式不对..
不符合规则: lib*.so
回复 支持 反对

使用道具 举报

发表于 2006-3-27 00:37:11 | 显示全部楼层
真是这样吗?
你用g++编译库接口函数时,要注意应用extern "C" {}括号起来,要不然编译器将给它另一个名字。这样dlsym就找不到这个函数了。
  1. //print.h
  2. #ifdef __cplusplus
  3. extern
  4.     "C"
  5. {
  6. #endif
  7. #ifdef SHARED
  8. int (*PrintInt)(int i);
  9. int (*PrintFloat)(float f);
  10. #else
  11. int (PrintInt)( int i);
  12. int (PrintFloat)(float f);
  13. #endif
  14. #ifdef __cplusplus
  15. }  // extern "C"
  16. #endif
  17. //printfloat.c
  18. #include "print.h"
  19. #include "iostream.h"
  20. #ifdef __cplusplus
  21. extern
  22.     "C"
  23. {
  24. #endif
  25. int PrintFloat(float f)
  26. {
  27. cout<<"f:"<<f<<endl;
  28. return 1;
  29. }
  30. #ifdef __cplusplus
  31. }  // extern "C"
  32. #endif
  33. //printint.c
  34. #include "print.h"
  35. #include "iostream.h"
  36. #ifdef __cplusplus
  37. extern
  38.     "C"
  39. {
  40. #endif
  41. int PrintInt(int i)
  42. {
  43. cout<<"i:"<<i<<endl;
  44. return 1;
  45. }
  46. #ifdef __cplusplus
  47. }  // extern "C"
  48. #endif
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表