您的位置:首页 > 运维架构 > Linux

linux c dynamic library programming

2014-07-31 18:58 351 查看
.h

#ifndef __TESTDL
#define __TESTDL

#ifdef __cplusplus
extern "C"
{
#endif
int (*add)(int,int,int*);
//int addint(int,int,int*);
#ifdef __cplusplus
}
#endif

#endif


.c

//#include "testdl.h"
int addint(int a,int b,int* c){
*c = a + b;
return 0;
}


.c

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
//#include "testdl.h"
int main(int argc,char** argv){

void* handler;
int (*add)(int,int,int*);
int c = 0;
handler = dlopen("./testdl.so",RTLD_LAZY);
add = dlsym(handler,"addint");
add(1,2,&c);
printf("%d",c);
dlclose(handler);
return 0;
}
gcc -shared -fpic testdl.c -o testdl.so

gcc -ldl dltest.c -o dltest.exe

./dltest.exe
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: