Skip to content

Commit

Permalink
copy from svn
Browse files Browse the repository at this point in the history
从svn迁移
  • Loading branch information
dusbin committed Feb 22, 2018
0 parents commit a8074e6
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dus_ccc_plugin/add.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int add(int a,int b)
{
return (a + b);
}

7 changes: 7 additions & 0 deletions dus_ccc_plugin/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#! /bin/bash
#gcc -fPIC -shared source.c -o libsource.so
gcc -fPIC -shared sub.c -o libsub.so
gcc -fPIC -shared mul.c -o libmul.so
gcc -fPIC -shared add.c -o libadd.so
gcc -fPIC -shared div.c -o libdiv.so
gcc -rdynamic -o test test.c -ldl
6 changes: 6 additions & 0 deletions dus_ccc_plugin/div.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int div(int a, int b)
{
if(b == 0)return -9999999;
return (a / b);
}

5 changes: 5 additions & 0 deletions dus_ccc_plugin/mul.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int mul(int a, int b)
{
return (a * b);
}

Binary file added dus_ccc_plugin/readme.docx
Binary file not shown.
20 changes: 20 additions & 0 deletions dus_ccc_plugin/source.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
int add(int a,int b)
{
return (a + b);
}

int sub(int a, int b)
{
return (a - b);
}

int mul(int a, int b)
{
return (a * b);
}

int div(int a, int b)
{
if(b == 0)return -9999999;
return (a / b);
}
5 changes: 5 additions & 0 deletions dus_ccc_plugin/sub.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int sub(int a, int b)
{
return (a - b);
}

76 changes: 76 additions & 0 deletions dus_ccc_plugin/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

//动态链接库路径
//#define LIB_CACULATE_PATH "./libsource.so"
#define LIB_MUL "./libmul.so" // '*'
#define LIB_SUB "./libsub.so" // '-'
#define LIB_ADD "./libadd.so" // '+'
#define LIB_DIV "./libdiv.so" // '/'
//函数指针
typedef int (*CAC_FUNC)(int, int);

int main(){
void *handle;
char *error;
//char *lib_name = LIB_CACULATE_PATH
CAC_FUNC cac_func = NULL;
char lib_name[4][15] ={LIB_ADD,LIB_SUB,LIB_MUL,LIB_DIV};
int a,b;
char c;
char opt[4][4]={"add","sub","mul","div"};
int opt_flag;
int exit_flag = 1;
do{
scanf("%d%c%d",&a,&c,&b);
switch(c){
case '+':
//char *lib_name = LIB_ADD;
//打开动态链接库
//opt = "add";
opt_flag = 0;
handle = dlopen(lib_name[opt_flag], RTLD_LAZY);
break;
case '-':
//char *lib_name = LIB_SUB;
//opt = "sub";
opt_flag = 1;
handle = dlopen(lib_name[opt_flag], RTLD_LAZY);
break;
case '*':
//char *lib_name = LIB_MUL;
//opt = "mul";
opt_flag = 2;
handle = dlopen(lib_name[opt_flag], RTLD_LAZY);
break;
case '/':
//char *lib_name = LIB_DIV;
//opt = "div";
opt_flag = 3;
handle = dlopen(lib_name[opt_flag], RTLD_LAZY);
break;
default:
printf("exit\n");
exit_flag = 0;
break;
}
if (!handle) {
//fprintf(stderr, "%s\n", dlerror());
printf("load %s error!\n",lib_name[opt_flag]);
//exit(EXIT_FAILURE);
continue;
}
//清除之前存在的错误
dlerror();
//获取函数
*(void **) (&cac_func) = dlsym(handle, opt[opt_flag]);
printf("%d %c %d = %d\n",a,c,b,(*cac_func)(a,b));
//关闭动态链接库
if (handle)
dlclose(handle);
//exit(EXIT_SUCCESS);
}while(exit_flag != 0);
return 0;
}

0 comments on commit a8074e6

Please sign in to comment.