库代码:

#include <stdio.h>
void hello(void)
{
    printf(
"hello\n");
}

编译命令:

gcc -shared -o hello.so hello.c

 

使用库的代码:

#include <stdio.h>
#include 
<stdlib.h>
#include 
<dlfcn.h>

int main(int argc, char **argv)
{
    
void *handle;
    
void (*callfun)();
    
char *error;
    handle 
= dlopen("/root/tmp/hello.so",RTLD_LAZY);  //如果hello.so不是在LD_LIBRARY_PATH所申明
                                                      
//的路径中必须使用全路径名
    if(!handle)
    {
        printf(
"%s \n",dlerror());
        exit(
1);
    }
    dlerror();
    callfun
=dlsym(handle,"hello");
    
if((error=dlerror())!=NULL)
    {
        printf(
"%s \n",error);
        exit(
1);
    }
    callfun();
    dlclose(handle);
    
return 0;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-10
  • 2021-12-23
  • 2021-12-25
  • 2021-09-14
  • 2021-07-08
猜你喜欢
  • 2021-06-13
  • 2021-10-24
  • 2021-09-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案