so 库加载 __attribute__((constructor))
来源:互联网 发布:强制进群软件 编辑:程序博客网 时间:2024/06/05 21:04
动态库生成:
gcc -Wall -shared -fPIC -o libss.so libss.c // 库名和源文件自定义
动态加载:
#include <dlfcn.h>
void *dlopen(const char *filename, int flag); // filename 动态库名(如libss.so),flag为 RTLD_NOW 或 RTLD_LAZY,RTLD_NOW 库中的所有函数在dlopen返回前都加载,
// RTLD_LAZY 库中的函数在用时才加载
char *dlerror(void); // dlopen dlsym dlclose的执行若有错误,返回描述错误的字符串
void *dlsym(void *handle, const char *symbol); //返回函数指针
int dlclose(void *handle); // 卸载库
Link with -ldl.
例:
udlopen.c
#include <stdio.h>#include <string.h>#include <dlfcn.h>int main(int argc, char **argv){void (*print)();int (*add)(int, int);void *handle;if (argc < 2)return -1;handle = dlopen(argv[1], RTLD_LAZY);if (!handle) {printf("dlopen failed: %s\n", dlerror());return -1;}print = dlsym(handle, "print");if (!print) {printf("dlsym failed: %s\n", dlerror());return -1;}print();add = dlsym(handle, "add");if (!add) {printf("dlsym failed: %s\n", dlerror());return -1;}add(1, 2);dlclose(handle);return 0;}
libss.c
#include <stdio.h>#include <string.h>void print() {printf("I am print\n");}int add(int a, int b){printf("Sum %d and %d is %d\n", a, b, a + b);return 0;}//static void king() __attribute__((constructor(101))); the following is also rightstatic __attribute__((constructor(101))) void king(){printf("I am king\n");}
gcc -Wall -shared -fPIC -o libss.so libss.c -ldl
gcc -Wall -o udlopen udlopen.c
./udlopen libss.so
I am king
I am print
Sum 1 and 2 is 3
0 0
- so 库加载 __attribute__((constructor))
- __attribute__ ((constructor));
- __attribute__((constructor))
- __attribute__((constructor)) 和 __attribute__((destructor))
- __attribute__((constructor))和__attribute__((destructor))
- GCC __attribute__((constructor)|(destructor))
- __attribute__ ((constructor)) 分享
- Gcc扩展:__attribute__((constructor))
- GCC __attribute__((constructor)|(destructor))
- GCC __attribute__((constructor)|(destructor))
- __attribute__((constructor)|(destructor)(PRIORITY))
- GCC __attribute__((constructor)|(destructor))
- DPDK-__attribute__((constructor))
- 函数属性__attribute__((constructor))和__attribute__((destructor))
- GCC的__attribute__ ((constructor))和__attribute__ ((destructor))
- gcc的__attribute__ ((constructor))和__attribute__ ((destructor))
- 函数属性__attribute__((constructor))和__attribute__((destructor))
- GCC的__attribute__ ((constructor))和__attribute__ ((destructor))
- 什么是搜索引擎的二次审核期
- ios开发中 应用设置的简单开发 (Settings.bundle)
- char *p="abc" 与 char p[]="abc" 的区别
- Android:No grammar constraints (DTD or XML schema) detected for the document
- 结构型设计模式
- so 库加载 __attribute__((constructor))
- 电商网站如何通过免费营销来营销
- IOS开发基础之——图像压缩成指定大小(高度/宽度)
- eclipse导入已存在的web项目
- log4j.properties配置详解
- HDU 3790
- 使用Varnish+ESI实现静态页面的局部缓存
- 第三周作业
- iOS 带IAP提交注意事项及无法submit for review的解决方案