- 论坛徽章:
- 1
|
本帖最后由 shihyu 于 2015-10-20 15:46 编辑
- #include <stdio.h>int p = 2;
- void print()
- {
- printf("This is the first dll src!\n");
- }
复制代码
- #include <dlfcn.h>
- #include <stdlib.h>
- #include <stdio.h>
- int main()
- {
- void* pHandle;
- void (*pFunc)(); /* 指向函式的指標 */
- int* p;
- pHandle = dlopen("./dl.so", RTLD_NOW); /* 開啟舊檔動態庫*/
- if (!pHandle) {
- printf("Cann't find dll.so\n");
- exit(1);
- }
- *(void **)(&pFunc) = dlsym(pHandle, "print"); /* 獲取庫函式位址 */
- if (pFunc) { /* 獲取庫函式物件 成功,執行庫函式 */
- pFunc();
- } else { /* 獲取庫函式物件 失敗,提示之 */
- printf("Cann't find func print\n");
- }
- p = (int*) dlsym(pHandle, "p"); /* 獲取庫變數位址 */
- if (p) { /* 獲取庫變數成功,使用之 */
- printf("p=%d\n", *p);
- } else { /* 獲取庫變數失敗,提示之 */
- printf("Cann't find int p\n");
- }
- dlclose(pHandle); /* 關閉動態庫 */
- return 0;
- }
复制代码 gcc -O -fPIC -shared -o dl.so d1.c && gcc -o tds tds.c -ldl
Makefile 要怎么产生出 dl.so , 之后再 -ldl 成 tds 文件? |
|