Dynamic library load with dlopen

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

I am assuming you are playing dynamic loading. the symbol sum is undefined, period.
You will have to load the symbol for it in the prod module, since it the only module that knows about it.

You now see why the normal way to link is to allow the link editor to resolve ALL symbols before runtime.

Since you are dynamically loading libraries, consider making sum a static module in the prod library - assuming you are trying to abstract (hide from view) symbols.

Think about 2 following approaches:

  1. Use dlopen in libproduct library, so that it should resolve sum before usage.
  2. Link libproduct with -lsum flag.

Thanks, kandrewo.

Thanks,
I resolved linking sum library to product library.
I believe that was unnecessary do this but unfortunately not so.

Why would you think it was unnecessary? Someone has to know that the product library needs the sum library...if you don't link it, the dynamic loader won't know to pull it in when loading product. If the dynamic loader doesn't know to do it, you need to do it yourself.

ldconfig command adds new library libsum and libproduct in system library cache (printable with ldconfig -p command), so I thought that the linker searched the library to resolve symbol "sum" in the default list like do with standard C lib. In fact if I use fprints function I don't have to link the library that resolve symbol "fprints". But I was wrong.