Chinaunix首页 | 论坛 | 博客
  • 博客访问: 368933
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: 系统运维

2011-04-27 21:53:42

  1. -fpic
  2.            Generate position-independent code (PIC) suitable for use in a shared library, if supported for
  3.            the target machine.
  4.  -shared
               Produce a shared object which can then be linked with other objects to form an executable.

print.c
  1. int i = 1;

  2. void
  3. print()
  4. {
  5.         printf("OK\n");
  6. }
gcc -fpic -shared -o print.so print.c

test.c
  1. #include <stdio.h>

  2. int
  3. main(void)
  4. {
  5.         print();

  6.         return (0);
  7. }

use1: 隐式调用
  1. gcc -o test test.c ./print.so

test2.c
  1. #include <stdio.h>
  2. #include <dlfcn.h>

  3. int
  4. main(void)
  5. {
  6.         void *pHandle;
  7.         void (*pFunc)();
  8.         int *p;

  9.         pHandle = dlopen("./print.so", RTLD_LAZY);
  10.         if (!pHandle) {
  11.                 printf("%s\n", dlerror());
  12.                 return (-1);
  13.         }

  14.         pFunc = (void (*)())dlsym(pHandle, "print");
  15.         if (pFunc)
  16.                 pFunc();
  17.         else
  18.                 printf("%s\n", dlerror());
  19.     
  20.         p = (int *)dlsym(pHandle, "i");
  21.         if (p)
  22.                 printf("%d\n", *p);
  23.         else
  24.                 printf("%s\n", dlerror());
  25.     
  26.         dlclose(pHandle);

  27.         return (0);
  28. }

use2: 显示调用
  1. gcc -o test2 test2.c -ldl

  1. SYNOPSIS
  2.        #include <dlfcn.h>

  3.        void *dlopen(const char *filename, int flag);

  4.        char *dlerror(void);

  5.        void *dlsym(void *handle, const char *symbol);

  6.        int dlclose(void *handle);

  7.        Link with -ldl.

  8. DESCRIPTION
  9.        The four functions dlopen(), dlsym(), dlclose(), dlerror() implement the interface to the dynamic linking loader.

dlerror()
       The  function  dlerror()  returns a human readable string describing the most recent error that occurred from dlopen(), dlsym() or dlclose() since
       the last call to dlerror().  It returns NULL if no errors have occurred since initialization or since it was last called.

dlopen()
       The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for  the  dynamic library.   If filename is NULL, then the returned handle is for the main program.

One of the following two values must be included in flag:

RTLD_LAZY
         Perform  lazy  binding.   Only resolve symbols as the code that references them is executed.  If the symbol is never referenced, then it is never resolved.  (Lazy binding is only performed for function references; references to variables are always  immediately  bound  when  the library is loaded.)

RTLD_NOW
         If  this value is specified, or the environment variable LD_BIND_NOW is set to a non-empty string, all undefined symbols in the library are resolved before dlopen() returns.  If this cannot be done, an error is returned.

dlsym()
       The  function  dlsym() takes a "handle" of a dynamic library returned by dlopen() and the null-terminated symbol name, returning the address where that symbol is loaded into memory.

dlclose()
       The function dlclose() decrements the reference count on the dynamic library handle handle.

阅读(1662) | 评论(0) | 转发(0) |
0

上一篇:静态链接库

下一篇:winDbg 起步

给主人留下些什么吧!~~