vim my_cpp_function.so
bool test_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return true;
else
return false;
}
int is_First(int value)
{
if(test_First(value))
return 1;
else
return -1;
}
double value_Average(int *tab, int size);
{
int i,sum;
double value;
value=1.0;
sum=0;
for(i=0;i<size;i++)
{
sum+=tab[i];
}
value=(1.*sum)/(1.*size);
return value;
}
vim test_dynamic.c
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#define max 1000
int main()
{
int *tab_First;
char *text_Error;
int is_F(int value);
double v_A(int *tab, int size);
double average;
int i,j;
#ifdef __cplusplus
extern "C"
{
#endif
int is_First (int value);
double value_Average(int *tab, int size);
#ifdef __cplusplus
}
#endif
void *handle=dlopen("my_cpp_function.so",RTLD_LAZY);
if(!handle)
{
fprintf(stderr,"%s\n",dlerror());
exit(EXIT_FAILURE);
}
dlerror();
*(void**)(&is_F)=dlsym(handle,"is_First");
if((text_Error=dlerror())!=NULL)
{
fprintf(stderr,"%s\n",text_Error);
exit(EXIT_FAILURE);
}
*(void**)(&v_A)=dlsym(handle,"value_Average");
if((text_Error=dlerror())!=NULL)
{
fprintf(stderr,"%s\n",text_Error);
exit(EXIT_FAILURE);
}
tab_First=(int*) calloc(max,sizeof(int));
if(tab_First==NULL)
exit(1);
i=0;
j=2;
do
{
if(is_F(j))
{
tab_First[i]=j;
++i;
}
++j;
}while(i<max);
average=v_A(tab_First,max);
for(i=0;i<max;i++)
{
printf("%d \n",tab_First[i]);
}
printf("Average: %.2f ",average);
printf("\n");
dlclose(handle);
free(tab_First);
return 0;
}
gcc -shared -rdynamic -o test_dynamic test_dynamic.c -ldl
Brak komentarzy:
Prześlij komentarz