Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063127
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: C/C++

2009-08-07 12:08:46

makefile:
 

#LIB = ./libfun.so

all : clean libfun.so sem end

.PHONY : all

  clean : -rm ./exe/*.o ./exe/core ./exe/libfun.so sem

echo ========================

libfun.so : libfun.o

  cc -G -o libfun.so libfun.o

  echo ========================

libfun.o : libfun.c

  cc -g -KPIC -c libfun.c

sem : sem.o

  cc -g -o sem sem.o $(LIB)

  echo ========================

sem.o : sem.c

  cc -g -c sem.c $(LIB)

end :

  -mkdir exe -mv *.o core *.so sem ./exe/

 

sem.c :

#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <dlfcn.h>
#include <link.h>

typedef void (*FunPtr)(void);
FunPtr ptr;
void main(int argc,char **argv[])
{
   void *handle;
   char *err;
   if((handle=dlopen("./libfun.so",RTLD_LAZY)) == NULL){
      return;
   }
   ptr = (FunPtr)dlsym(handle, "pp");
   if((err = dlerror())!=NULL) {
      printf("error :%s\n",err);
      return;
   }
   (*ptr)();
   dlclose(handle);

}

 

libfun.c

#include <stdlib.h>
#include <stdio.h>
void pp();

void pp()
{
   printf("i am toy\n");
}

阅读(633) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~