Chinaunix首页 | 论坛 | 博客
  • 博客访问: 127243
  • 博文数量: 70
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 380
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 18:53
文章分类
文章存档

2015年(8)

2014年(14)

2011年(1)

2010年(21)

2009年(26)

我的朋友

分类: LINUX

2010-01-24 14:28:53


 
程序1 s.cpp

extern "C"
{
        int soTest(int a,int b) ;
}

int soTest(int a,int b)
{
        return a+b;
}


编译:
g++  -c s.cpp
g++ -shared -o s.so s.o


程序2 t.cpp

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

int main(int argc, char **argv)
{
        void *handle;
        int (*soTest)(int,int);
        char *error;

        handle = dlopen ("./s.so", RTLD_LAZY);
        if (!handle)
        {
                fprintf (stderr, "%s\n", dlerror());
                exit(1);
        }

        (void *)soTest = (int (*)(int,int))dlsym(handle, "soTest");
        if ((error = dlerror()) != NULL)
        {
                fprintf (stderr, "%s\n", error);
                exit(1);
        }

        printf ("%d\n", (*soTest)(2,3));
        dlclose(handle);

        return 0;
}


编译:
g++ -c t.cpp
g++ -o t t.o -ldl
阅读(673) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~