1 回答

TA貢獻1772條經驗 獲得超6個贊
如何在Mac OSX 中制作dylib和使用dylib
1.首先是構建一個函數庫
編輯add.c
int add(int a,int b)
{
return a+b;
}
int axb(int a,int b)
{
return a*b;
}
保存
其中兩個函數 add axb
這是簡單的寫的,復雜的自己開發,這里主要介紹方法
2.編譯函數庫
gcc -c add.c -o add.o
//下面是linux系統時
ar rcs libadd.a add.o
//如果你是linux 就用這種庫
//下面是Mac OSX
gcc add.o -dynamiclib -current_version 1.0 -o libadd.dylib
得到 libadd.dylib
3.編輯testadd.c
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(int argc,char *argv[])
{
int a,b;
a=10;
b=9;
int c;
c=add(a,b);
printf(“%d\n”,c);
return 1;
}
保存
4.編譯testadd.c
gcc testadd.c -o testadd -L. -ladd
./testadd
輸出19
5.編輯dladd.c
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(int argc,char *argv[])
{
int *ab;
void *h=dlopen(“./libadd.dylib”,RTLD_LAZY);
ab=dlsym(h,“add”);
printf(“add=address is 0x %x\n”,ab);
dlclose(h);
return 1;
}
這個是為了查看函數庫在庫中的地址的
6.編譯dladd.c
gcc dladd.c -o dladd -ldl
./dladd
add=address is 0x 23fe2
- 1 回答
- 0 關注
- 1721 瀏覽
添加回答
舉報