Chinaunix
标题:
Makefile 要怎么产生出 so
[打印本页]
作者:
shihyu
时间:
2015-10-20 15:44
标题:
Makefile 要怎么产生出 so
本帖最后由 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 文件?
作者:
zsszss0000
时间:
2015-10-20 16:02
本帖最后由 zsszss0000 于 2015-10-20 16:03 编辑
all:
gcc -o hello bb.c -ldl
ld.so:
gcc -O -fPIC -shared -o dl.so aa.c
复制代码
试试这种写法看看是否可以 ?
作者:
yulihua49
时间:
2015-10-20 16:12
shihyu 发表于 2015-10-20 15:44
gcc -O -fPIC -shared -o dl.so d1.c && gcc -o tds tds.c -ldl
$(HOME)/lib/libudrt.so:udrt.o
$(CC) -shared -o$(HOME)/lib/libudrt.so udrt.o
作者:
shihyu
时间:
2015-10-20 18:12
CC = gcc
all: dl.so
$(CC) -o main tds.c -ldl
dl.so:
$(CC) -O -fPIC -shared -o dl.so d1.c
.PHONY: clean
clean:
@rm -rf *.o *.so main
复制代码
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2