Dynamic library load with dlopen


 
Thread Tools Search this Thread
Top Forums Programming Dynamic library load with dlopen
# 1  
Old 12-18-2009
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.

Code:
/*
sum.c
*/

#include <stdlib.h>
#include <stdio.h>


int sum(int a, int b)
{
  int c;
  c = a+b;
  return(c);
}

Code:
/*
product.c
*/

#include <stdio.h>

int product(int a, int b)
{
  int product = 0;
  while(b>0)
  {
    product = sum(product, a);
    b--;
  }
  return(product);
}

Code:
/*
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);
}

Code:
#! /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


Last edited by pludi; 12-18-2009 at 01:06 PM.. Reason: code tags, please...
# 2  
Old 12-19-2009
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.
# 3  
Old 12-20-2009
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.
# 4  
Old 12-21-2009
MySQL

Thanks,
I resolved linking sum library to product library.
I believe that was unnecessary do this but unfortunately not so.
# 5  
Old 12-21-2009
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.
# 6  
Old 12-21-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

problem in dynamic library*.so

hello I apologize if my question bothers you I work on a code developed in C + + which worked well on mac os, this code will help create libraries *.so from *.cc and *.h I used this as flags:CXXFLAGS = -g -O2 -fPIC -Wall -ldl -D_GNU_SOURCE ,CXX := g++ and $(CXX)-shared -o $(LIBNAME) $(CLIBLIB)... (0 Replies)
Discussion started by: pheapc
0 Replies

2. Programming

dlopen failing on library with statically linked dependencies

I am attempting to port a program from OS X to Linux. It's C++ & Qt Creator and I did the original Windows to OS X port, so I tried to make it as POSIX-compliant as possible; the OS X port works well, and the Linux port builds and starts (it's on Ubuntu 9.10) but has some issues running. The... (2 Replies)
Discussion started by: Hodapp87
2 Replies

3. UNIX for Dummies Questions & Answers

Problem with shared dynamic library files

I am having a major problem. Most of the commands that i am running on my centos 5 system is giving the error of type: <dynamic shared library file>:open failed: No such file or directory For example: libgcc_s.so.1: open failed: No such file or directory How can i solve this? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

4. Programming

overriding the dynamic library

Hi, I wonder how can we override the dynamic library loaded by ld on start up.(dynamic linked application). so that linker uses the new library to find symbols. Is it possible to do. Cheers. (4 Replies)
Discussion started by: Raom
4 Replies

5. UNIX for Dummies Questions & Answers

Library won't load

Trying to run an X11 executable under Darwin, I keep getting: dyld: Library not loaded: /sw/lib/libglib-1.2.0.dylib Referenced from: /usr/X11R6/bin/wav2rsomac Reason: image not found Trace/BPT trap I can't figure out if this is an error in the way the program is running, or if the... (0 Replies)
Discussion started by: sansan
0 Replies

6. Programming

loading a dynamic library in linux

is there a way to load a dynamic library in linux? I know tht in AIX u can do it using the loadAndInit() function. Please guide me (2 Replies)
Discussion started by: jacques83
2 Replies

7. Programming

Can't dlopen() a library containing Thread Local Storage

Hi, I have a small test c program which tries to dlopen a shared library(libjvm.sl). But i get error as "Can't dlopen() a library containing Thread Local Storage" My program is as below when i run the program i get error any pointers why the error?? I am using hp-ux . The... (1 Reply)
Discussion started by: shriashishpatil
1 Replies

8. Linux

load dynamic and shared library in kernel

hi how can i load dynamic or shared library in linux kernel modules . mtaghiloo@yahoo.com (0 Replies)
Discussion started by: mtaghiloo
0 Replies

9. Programming

HOw to load dynamic lib from a statically linked program ?

I need to load a dynamic library from a statically linked program. Is there a way without recompiling my program. when i try to do that my program just crashes. If not possible, how can I avoid crashing the program when i try to load the dynamic lib, again without recompiling. If my... (1 Reply)
Discussion started by: disclaimer
1 Replies

10. Programming

how to auto load a dynamic library

hi, every body. i have in trouble that how to load a dynamic library by hand. for example, i know how to generate a dynamic library and how to link a dynamic library in makefile. generate a dynamic library: add -dy -G behind cc link a dynamic library in makefile: add -dy -Bdynamic behind cc... (0 Replies)
Discussion started by: subrain
0 Replies
Login or Register to Ask a Question