LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-20-2006, 12:54 AM   #1
linuxravin
LQ Newbie
 
Registered: Jul 2006
Posts: 4

Rep: Reputation: 0
Smile dlopen() does not loads the dependent libraries


Hi all,

I am trying to link a library "libfunc1.so" at runtime. This library is having dependency on "libfunc2.so" library. As per the manpage the dlopen() should load the dependent libraries automatically. But it's not happening for me. When I compile an execute the following executable, it throws an error as
---
tsf:skumar % ./a.out
ERROR:: ld.so.1: a.out: fatal: relocation error: file ./libfunc1.so: symbol func2: referenced symbol not found
tsf:skumar %
---

my program looks as follows

=======
#include <stdio.h>
#include <dlfcn.h>
#include <link.h>

main( int argc, char *argv[])
{

int (*func1)( int, int);
void *handle;
int result;

if (( handle = dlopen("libfunc1.so", RTLD_NOW | RTLD_GLOBAL)) == NULL )
{
printf( "ERROR:: %s\n", dlerror());
exit(1);
}
printf("Library loading is successful\n");

func1 = dlsym(handle, "func1");
if (func1 == NULL)
{
printf("ERROR:: %s\n",dlerror());
dlclose(handle);
exit(1);
}
result = (*func1)(2,3);
printf("\n result %d\n",result);

dlclose(handle);
}

=======

The library code is as follows

libfunc1.so
====
#include <stdio.h>
int func2( int a, int b);
int func1(int x, int y)
{
printf("\nIn func1()\n");
func2( x, y);
return (x+y);
}
====


libfunc2.so
====
#include <stdio.h>
int func2(int x, char y)
{
printf("\nIn func2()\n");
return (x*y);
}
====
 
Old 07-20-2006, 10:53 AM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
What is compilation command line? I hope you didn't include -lfunc1 ?
 
Old 07-20-2006, 11:09 AM   #3
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Please perform 'ldd' on libfunc1.so and make sure it does, indeed, reference libfunc2.so.
 
Old 07-21-2006, 01:35 AM   #4
linuxravin
LQ Newbie
 
Registered: Jul 2006
Posts: 4

Original Poster
Rep: Reputation: 0
a

I performed ldd it shows as below

===
tsf:skumar % ldd -r libfunc1.so
libgcc_s.so.1 => /export/home/diameter/library32//libgcc_s.so.1
libc.so.1 => /usr/lib/libc.so.1
libdl.so.1 => /usr/lib/libdl.so.1
/usr/platform/SUNW,Ultra-4/lib/libc_psr.so.1
symbol not found: func2 (./libfunc1.so)
tsf:skumar %
===

I think the library libfunc1.so is having lazy reference for the symbol "func2"
 
Old 07-21-2006, 04:38 AM   #5
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
But it doesn't know it can be taken from libfunc2.so.. What is compilation line, anyway?
 
Old 07-21-2006, 05:53 AM   #6
linuxravin
LQ Newbie
 
Registered: Jul 2006
Posts: 4

Original Poster
Rep: Reputation: 0
compilation line is as follows


$gcc -fPIC -c func1.c
$gcc -shared -o libfunc1.so func1.o
$gcc -fPIC -c func2.c
$gcc -shared -o libfunc2.so func2.o

$gcc -ldl -g testlib.c
 
Old 07-21-2006, 06:10 AM   #7
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Oh. Try
gcc -shared -o libfunc1.so func1.o -L. -lfunc2
and swap compiling order.
 
Old 07-21-2006, 09:33 AM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
Yes, you'll need to link against func2 so it can find it at runtime.
 
Old 07-25-2006, 06:26 AM   #9
linuxravin
LQ Newbie
 
Registered: Jul 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Hmmm... Actaully I wanted dlopen() to load libfunc2.so automatically. But that doesn't seems to be happening.
Let me link it at compile time.

Anyways thanks you all for your valuable suggestions.

--ravin
 
Old 07-25-2006, 06:52 AM   #10
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
It loads automatically - you don't to do anything on every load, but only if libfunc1.so is marked as dependent on libfunc2.so (which -l does).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LD_LIBRARY_PATH and dlopen smrcis Programming 5 03-08-2006 06:10 PM
shared libraries, dlopen and static member variables? (C/C++) Thinking Programming 2 12-19-2005 12:55 AM
gcc dependent packages? trumpetdork Linux - Software 1 04-08-2005 05:21 PM
installed dropline, root loads kde3.2, user loads drop pgrimes Linux - Software 7 06-28-2004 06:11 PM
two dependent rpms pasalacqua128 Linux - Software 1 06-14-2004 02:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration