Hi,
I created two library libsum.so and libproduct.so. In the libproduct.so is defined function "product" that use function "sum" defined in libsum.so. So libproduct depends on libsum.
In main function I called product function by a dlopen request. Unfortunaly, when I execute program I have an error: symbol lookup error, undefined symbol sum. Why this? I don't open sum function with another dlopen request. This solution resolve problem but is, in generally, a bad solution because the main function developer can not say that product depends on sum. Is there another solution?
Plese help me, Thanks to all and nice week-end.
I paste code below.
/*
sum.c
*/
#include <stdlib.h>
#include <stdio.h>
int sum(int a, int b)
{
int c;
c = a+b;
return(c);
}
/*
product.c
*/
#include <stdio.h>
int product(int a, int b)
{
int product = 0;
while(b>0)
{
product = sum(product, a);
b--;
}
return(product);
}
/*
product_main_dyn.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
int
main(int argc, char **argv)
{
int a;
int b;
int ris;
void *hdl_prd;
void *hdl_smm;
int (*prod)(int, int);
char *error;
if(argc != 3)
{
printf("insert two int number to run\n");
exit(EXIT_FAILURE);
}
a = atoi(argv[1]);
b = atoi(argv[2]);
hdl_prd = dlopen("/home/shade82/product/libproduct.so.1", (RTLD_LAZY));
if (!hdl_prd)
{
printf("error open library\n");
exit(EXIT_FAILURE);
}
dlerror();
*(void **) (&prod) = dlsym(hdl_prd, "product");
if ((error = dlerror()) != NULL)
{
printf("error: not resolve symbol\n");
exit(EXIT_FAILURE);
}
ris = (*prod)(a,b);
printf("product: %d\n",ris);
dlclose(hdl_prd);
exit(EXIT_SUCCESS);
}
#! /bin/bash
rm *.o *.exe libsum.so.1 libproduct.so.1 libsum.so.1.0.0 libproduct.so.1.0.0
gcc -fPIC -c sum.c
gcc -fPIC -c product.c
gcc -shared -Wl,-soname,libsum.so.1 -o libsum.so.1.0.0 sum.o -lc
gcc -shared -Wl,-soname,libproduct.so.1 -o libproduct.so.1.0.0 product.o -lc
export LD_LIBRARY_PATH=/home/shade82/
sudo ldconfig -n .
gcc prod_main_dyn.c -o prod_main_dyn.exe -ldl