Sponsored Content
Top Forums Programming Dynamic library load with dlopen Post 302381490 by shade82 on Friday 18th of December 2009 11:50:45 AM
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...
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
wsreg_can_access_registry(3WSREG)		    Product Install Registry Library Functions			 wsreg_can_access_registry(3WSREG)

NAME
wsreg_can_access_registry - determine access to product install registry SYNOPSIS
cc [flag ...] file ...-lwsreg [library ...] #include <fcntl.h> #include <wsreg.h> int wsreg_can_access_registry(int access_flag); DESCRIPTION
The wsreg_can_access_registry() function is used to determine what access, if any, an application has to the product install registry. The access_flag argument can be one of the following: O_RDONLY Inquire about read only access to the registry. O_RDWR Inquire about modify (read and write) access to the registry. RETURN VALUES
The wsreg_can_access_registry() function returns non-zero if the specified access level is permitted. A return value of 0 indicates the specified access level is not permitted. EXAMPLES
Example 1: Initialize the registry and determine if access to the registry is permitted. #include <fcntl.h> #include <wsreg.h> int main(int argc, char **argv) { int result; if (wsreg_initialize(WSREG_INIT_NORMAL, NULL)) { printf("conversion recommended, sufficient access denied "); } if (wsreg_can_access_registry(O_RDONLY)) { printf("registry read access granted "); } else { printf("registry read access denied "); } if (wsreg_can_access_registry(O_RDWR)) { printf("registry read/write access granted "); } else { printf("registry read/write access denied "); } } USAGE
The wsreg_initialize(3WSREG) function must be called before calls to wsreg_can_access_registry() can be made. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
wsreg_initialize(3WSREG), attributes(5) SunOS 5.10 22 Sep 2000 wsreg_can_access_registry(3WSREG)
All times are GMT -4. The time now is 09:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy