引言
重点讲述linux上使用gcc编译动态库的一些操作.并且对其深入的案例分析.
最后介绍一下动态库插件技术, 让代码向后兼容.关于linux上使用gcc基础编译,
预编译,编译,生成机械码最后链接输出可执行文件流程参照下面.
gcc编译流程 http://www.jb51.net/article/46407.htm
而本文重点是分析动态库相关的知识点. 首先看需要用到的测试素材
heoo.h
#ifndef _H_HEOO
#define _H_HEOO /*
* 测试接口,得到key内容
* : 返回key的字符串
*/
extern const char* getkey(void); /*
* 测试接口,得到value内容
* arg : 传入的参数
* : 返回得到的结果
*/
extern void* getvalue(void* arg); #endif // !_H_HEOO
heoo-getkey.c
#include "heoo.h" /*
* 测试接口,得到key内容
* : 返回key的字符串
*/
const char*
getkey(void) {
return "heoo-getkey.c getkey";
}
heoo-getvalue.c
#include "heoo.h"
#include <stdio.h> /*
* 测试接口,得到value内容
* arg : 传入的参数
* : 返回得到的结果
*/
const void*
getvalue(void* arg) {
const char* key = "heoo-getvalue.c getvalue";
printf("%s - %s\n", key, (void*)arg);
return key;
}
heoo.c
#include "heoo.h"
#include <stdio.h> /*
* 测试接口,得到key内容
* : 返回key的字符串
*/
const char*
getkey(void) {
return "heoo.c getkey";
} /*
* 测试接口,得到value内容
* arg : 传入的参数
* : 返回得到的结果
*/
const void*
getvalue(void* arg) {
const char* key = "heoo.c getvalue";
printf("%s - %s\n", key, (char*)arg);
return key;
}
main.c
#include <stdio.h>
#include "heoo.h" // 测试逻辑主函数
int main(int argc, char* argv[]) {
// 简单的打印数据
printf("getkey => %s\n", getkey());
getvalue(NULL);
return ;
}
到这里也许感觉有点臃肿, 但是理解为什么是必要的. 会让你对于动态库高度高上0.01毫米的.哈哈.
先让上面代码跑起来.
gcc -g -Wall -o main.out main.c heoo.c
测试结果如下
测试完成,那就开始静态库到动态库扩展之旅.
前言
从静态库说起来
首先参照下面编译语句
gcc -c -o heoo-getkey.o heoo-getkey.c
gcc -c -o heoo-getvalue.o heoo-getvalue.c
对于静态库创建本质就是打包. 所以用linux上一个 ar创建静态库压缩命令.详细用法可以看
ar详细用法参照 http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201121093917552/
那么我们开始制作静态库
ar rcs libheoo.a heoo-getvalue.o heoo-getkey.o
那么我们采用静态库执行编译上面main.c 函数
gcc -g -Wall -o main.out main.c -L. -lheoo
运行的截图如下
运行一切正常. 对于静态库编译 简单说明一下. ar 后面的 rcs表示 替换创建和添加索引. 具体的看上面的网址.
后面gcc中 -L表示查找库的目录, -l表示搜索的 libheoo库. 还有其它的-I表示查找头文件地址, -D表示添加全局宏.......
对于上面静态库编译还有一种方式如下
gcc -g -Wall -o main.out main.c libheoo.a
执行结果也是一样的.可以将 *.a 理解成多个 *.o合体.
好到这里前言就说完了.那我们开始说正题动态库了.
正文
动态库的构建和使用
动态库构建命名如下,仍然以heoo.c heoo.h 为例
gcc -shared -fPIC -o libheoo.so heoo.c
开始编译代码 先介绍一种最简单的有点类似上面静态库最后一种方式.
gcc -g -Wall -o main.out main.c ./libheoo.so
这里是显式编译. 结果如下
对于 上面编译 动态库的时候如果 直接使用 libheoo.so. 例如
gcc -g -Wall -o main.out main.c libheoo.so
如果没有配置动态库路径, 查找动态库路径会出问题. 这里就不复现了(因为我把环境调好了). 会面会给出解决办法.
下面说libheoo.so 标准的使用方式
gcc -g -Wall -o main.out main.c -L. -lheoo
运行结果如下
上面是个常见错误, 系统找不见动态库在那. 需要配置一下, 再编译参照如下
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH;./"
gcc -g -Wall -o main.out main.c -L. -lheoo
上面第一句话是在当前会话层. 添加库查找路径,包含当前文件目录.这个会话层关闭了就失效了. Linux上shell确实很重要. 现在执行结果
到这里动态库的也都完毕了. 一切正常.
一个奇巧淫技
问: gcc -l 链接一个库的时候,但是库中存在同名的静态库和动态库. 会链接到那个库?
通过上面的那么多测试应该知道是动态库吧,因为使用动态库会报错.使用静态库没有事.
那么问题来了, 我想使用静态库怎么办.
-static
上面gcc 选项可以帮助我们强制链接静态库!
动态库的显示使用
到这里基本上是重头戏了. 扯一点,这些知识点在window也一样知识环境变了,设置变了.链接编译显式加载都有的. 下面是重新操作的代码.
heooso.c
#include <stdio.h>
#include <dlfcn.h> #define _STR_PATH "./libheoo.so" // 显示调用动态库, 需要 -ldl 链接程序库
int main(int argc, char* argv[]) {
const char* (*getkey)(void);
const void* (*getvalue)(void* arg);
/*
* 对于dlopen 函数第二个参数
* RTLD_NOW:将共享库中的所有函数加载到内存
* RTLD_LAZY:会推后共享库中的函数的加载操作,直到调用dlsym()时方加载某函数
*/
void* handle = dlopen(_STR_PATH, RTLD_LAZY);
// 下面得到错误信息,是一种小心的变成方式,每次都检测一下错误是否存在
const char* err = dlerror(); if(!handle || err) {
fprintf(stderr, "dlopen " _STR_PATH " no open! err = %s\n", err);
return -;
} getkey = dlsym(handle, "getkey");
if((err = dlerror())){
fprintf(stderr, "getkey err = %s\n", err);
dlclose(handle);
return -;
}
puts(getkey()); //这种显式调用dll代码,很不安全代码注入太简单了
getvalue = dlsym(handle, "getvalue");
if((err = dlerror())){
fprintf(stderr, "getvalue err = %s\n", err);
dlclose(handle);
return -;
}
puts(getvalue(NULL)); dlclose(handle);
return ;
}
编译代码
gcc -g -Wall -o heooso.out heooso.c -ldl
测试结果截图如下
运行一切正常. 功能是实现了.但是大家千万别这么用.否则还是比较危险的.也是一种编程思路吧.后面
后记会写一个向后兼容的插件机制. 大家可以观摩一下. 方便更深入的了解Linux系统开发.算是一个简易的
Linux运用插件技术的小项目吧.
后记
错误是难免的,欢迎吐槽. 最后献上一个linux上如何通过动态库运行时加载插件的案例.麻雀虽小,五脏俱全.
Makefile
CC = gcc
DEBUG = -g -Wall
LIB = -ldl
RUNSO = $(CC) -fPIC -shared -o $@ $^
RUN = $(CC) $(DEBUG) -o $@ $^ #总的任务
all:libheoo.so libheootwo.so libheoothree.so main.out #简单lib%.so生成
libheoo.so:heoo.c
$(RUNSO)
libheootwo.so:heootwo.c
$(RUNSO)
libheoothree.so:heoothree.c
$(RUNSO) #生成的主要内容
main.out:main.c
$(RUN) $(LIB) # 简单的清除操作 make clean
.PHONY:clean
clean:
rm -rf *.so *.s *.i *.o *.out *~ ; ls -hl
heoo.h
#ifndef _H_HEOO
#define _H_HEOO /*
* 测试接口,得到key内容
* : 返回key的字符串
*/
extern const char* getkey(void); /*
* 测试接口,得到value内容
* arg : 传入的参数
* : 返回得到的结果
*/
extern const void* getvalue(void* arg); #endif // !_H_HEOO
heootwo.c
#include "heoo.h"
#include <stdio.h> /*
* 测试接口,得到key内容
* : 返回key的字符串
*/
const char*
getkey(void) {
return "heootwo.c getkey";
} /*
* 测试接口,得到value内容
* arg : 传入的参数
* : 返回得到的结果
*/
const void*
getvalue(void* arg) {
const char* key = "heootwo.c getvalue";
printf("%s - %s\n", key, (char*)arg);
return key;
}
heoothree.c
#include "heoo.h"
#include <stdio.h> /*
* 测试接口,得到key内容
* : 返回key的字符串
*/
const char*
getkey(void) {
return "heoothree.c getkey";
} /*
* 测试接口,得到value内容
* arg : 传入的参数
* : 返回得到的结果
*/
const void*
getvalue(void* arg) {
const char* key = "heoothree.c getvalue";
printf("%s - %s\n", key, (char*)arg);
return key;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <dlfcn.h> //塞入的句柄数
#define _INT_HND (3)
// 最多支持108个插件
#define _INT_LEN (108)
// 文件路径最大长度
#define _INT_BUF (512) // 处理dll,并且将返回的数据保存在a[_INT_HND]中, 这个数组长度必须是
bool dll_add(void* a[], const char* dllpath);
// 处理指定目录得到结果塞入a中, nowpath为NULL表示当前目录
int dll_new(void* a[][_INT_HND], int len, const char* nowpath);
// 释放资源
void dll_del(void* a[][_INT_HND], int len); /*
* 动态加载机制
*/
int main(int argc, char* argv[]) {
int idx, len, i;
void* a[_INT_LEN][_INT_HND]; // 当前目录下,处理结果
len = dll_new(a, _INT_LEN, NULL);
if(len == ){
fprintf(stderr, "感谢使用,没有发现合法插件内容!\n");
exit();
} //数据展示
puts("------------------------------ 欢迎使用main插件 ----------------------------------");
for(i=; i<len; ++i){
const char* (*getkey)(void) = a[i][];
printf(" %d => %s\n", i, getkey());
}
printf(" 请输入 待执行的 索引[0, %d)\n", len);
if(scanf("%d", &idx)!= || idx< || idx >= len){
puts(" fake 错误的命令,程序退出中!");
goto __exit;
}
puts(" 执行结果如下:");
const void* (*getvalue)(void* arg) = a[idx][];
puts(getvalue(NULL)); __exit:
puts("------------------------------ 谢谢使用main插件 ----------------------------------");
dll_del(a, len);
return ;
} // 处理dll,并且将返回的数据保存在a[_INT_HND]中, 这个数组长度必须是
bool
dll_add(void* a[], const char* dllpath) {
const char* (*getkey)(void);
const void* (*getvalue)(void* arg); void* handle = dlopen(dllpath, RTLD_LAZY);
// 下面得到错误信息,是一种小心的变成方式,每次都检测一下错误是否存在
const char* err = dlerror(); if(!handle || err) return false; getkey = dlsym(handle, "getkey");
if((err = dlerror())){
dlclose(handle);
return false;
} //这种显式调用dll代码,很不安全代码注入太简单了
getvalue = dlsym(handle, "getvalue");
if((err = dlerror())){
dlclose(handle);
return false;
}
// 句柄, key, value, 协议订的
a[] = handle;
a[] = getkey;
a[] = getvalue;
return true;
} // 处理指定目录得到结果塞入a中, nowpath为NULL表示当前目录
int
dll_new(void* a[][_INT_HND], int len, const char* nowpath){
int j = , rt;
DIR* dir;
struct dirent* ptr;
char path[_INT_BUF]; // 设置默认目录
if(!nowpath || !*nowpath) nowpath = ".";
// 打开目录信息
if((dir = opendir(nowpath)) == NULL) {
fprintf(stderr, "opendir open %s, error:%s\n", nowpath, strerror(errno));
exit(-);
} //挨个读取文件
while(j<len && (ptr=readdir(dir))){
//只处理文件,包含未知文件
if(DT_BLK == ptr->d_type || DT_UNKNOWN == ptr->d_type){
rt = snprintf(path, _INT_BUF, "%s/%s", nowpath, ptr->d_name);// 只有确实是 *.so 文件才去出去运行
if(rt>&&rt < _INT_BUF&&path[rt-]=='o'&&path[rt-]=='s'&&path[rt-]=='.') {
// 添加数据 dao数组 a中
if(dll_add(a[j], path))
++j;
}
}
} closedir(dir);
return j;
} // 释放资源
void
dll_del(void* a[][_INT_HND], int len) {
int i=-;
while(++i < len)
dlclose(a[i][]);
}
最后运行截图
到这里一个小demo就完工了. 关于Linux gcc上动态库插件开发,剖析完毕.O(∩_∩)O哈哈~