免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 968 | 回复: 0
打印 上一主题 下一主题

使用Linux共享数据对象 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-07 12:51 |只看该作者 |倒序浏览
Linux共享数据对象类似于windows中的动态链接库,其后缀通常为so.* (*为版本号),例如为我们所熟知的libpcap,它对应的文件为/usr/lib/libpcap.so。如果程序中使用了某共享数据对象文件,需要在链接时指定gcc的链接参数。如使用libpcap库时,加入lpcap;使用POSIX Thread时,加lpthread。原则就是当库文件为libname.so.*时,相应的链接参数为lname,当然,libname.so.*文件或其自身的符号链接需要放在/lib或者/usr/lib或者LIB_LIBRARY_PATH环境变量指定的路径下。

使用共享数据对象主要涉及一下四个函数:

void *dlopen(const char *filename, int flag);
const char *dlerror(void);
void *dlsym(void *handle, char *symbol);
int dlclose(void *handle);

dlopen函数负责载入动态连接库文件,成功时返回动态链接库的句柄。flag参数为RTLD_LAZY时表示在执行动态链接库文件时解析未被决议的符号;为RTLD_NOW时表示在dlopen函数返回是解析未被决议的符号。dlsym函数根据动态链接库的句柄,返回名称为symbol的函数指针。dlerror返回asci形式的错误信息。dlclose负责将打开动态链接库的引用计数减一,仅当引用计数为0时,dlclose才执行关闭句柄操作,这意味着相同的动态链接库文件可以被打开多次。

下面给一段演示程序:
sort.h

#ifndef _SORT_H_
#define _SORT_H_
void bubble_sort(int[], int);
#endif /* _SORT_H_ */
sort.c
#include "sort.h"
void bubble_sort(int elems[], int elem_count)
{
    int tmp, i, j;
   
    for (i = 0; i  elem_count - 1; i++)
        for (j = 0; j  elem_count - i - 1; j++)
            if (elems[j+1]  elems[j]){
                tmp = elems[j];
                elems[j] = elems[j+1];
                elems[j+1] = tmp;        
            }
}
test.c
#include stdio.h>
#include dlfcn.h>
#include "sort.h"
int main()
{
    int items[] = {2, 5, 6, 1, -2, 6, 2, 10};
    void (*sort)(int[], int);
    void *h;
    int i;
   
    h = dlopen("./libsort.so.1", RTLD_LAZY);
    if (!h){
        fprintf(stderr, "Failed to load sort.so\n");
        exit(-1);
    }
    sort = dlsym(h, "bubble_sort");
    if (!sort){
        fprintf(stderr, "Failed to export function bubble_sort\n");
        exit(-1);
    }
    sort(items, sizeof(items) / sizeof(items[0]));
        
    for (i = 0; i  sizeof(items) / sizeof(items[0]); i++)
        fprintf(stdout, "%d\t", items);
   
    fprintf(stdout, "\n");
    dlclose(h);
    return 0;
}
Makefile
CC=gcc
CFLAG=-rdynamic -ldl
TARGET_lIB_NAME=libsort.so.1
OBJECTS=libsort.so.1 *.o test
test: $(TARGET_lIB_NAME) test.o
    $(CC) $^ $(CFLAG) -o $@
libsort.so.1: sort.c
    $(CC) $ -shared -o $@
test.o: test.c
    $(CC) -c $
.PHONY: clean
clean:
    rm -rf $(OBJECTS)


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/13494/showart_416443.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP