Unix Programming - Creating a shared library and loading it

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2006 > Creating a shared library and loading it





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Creating a shared library and loading it
abhi147@gmail.com

2006-08-26, 7:45 am

Hi ,

I am trying to create a shared library and trying to load it
usinf dlopen() function . My code and steps to create and load the *.so
is :

/*** test.c ***/
#include <stdio.h>
void test()
{
printf("Hello World\n");

}

/*** Steps for *.so creation ***/
bash-3.00# gcc -fPIC -c test.c
bash-3.00# gcc -o library.so -shared test.o
bash-3.00# gcc -o library.so -Wl,-h,library.so -shared test.o

/***load.c***/
#include <stdio.h>
#include <dlfcn.h>

int main(int argc,char **argv)
{
void *handle;
char *error;

handle = dlopen ("library.so", RTLD_LAZY);
if (!handle) {
fprintf (stderr, "%s\n", dlerror());
exit(1);
}

}

/*** Steps for creating load executable***/
bash-3.00# gcc test.c -ldl

When I run the "a.out" it gives me the following error

bash-3.00# ./a.out
ld.so.1: a.out: fatal: library.so: open failed: No such file or
directory

Can someone tell me .. where am I doing wrong ?

Thanks !

spibou@gmail.com

2006-08-26, 7:45 am

abhi147@gmail.com wrote:

> Hi ,
>
> I am trying to create a shared library and trying to load it
> usinf dlopen() function . My code and steps to create and load the *.so
> is :
>
> /*** test.c ***/
> #include <stdio.h>
> void test()
> {
> printf("Hello World\n");
>
> }
>
> /*** Steps for *.so creation ***/
> bash-3.00# gcc -fPIC -c test.c
> bash-3.00# gcc -o library.so -shared test.o
> bash-3.00# gcc -o library.so -Wl,-h,library.so -shared test.o
>
> /***load.c***/
> #include <stdio.h>
> #include <dlfcn.h>
>
> int main(int argc,char **argv)
> {
> void *handle;
> char *error;
>
> handle = dlopen ("library.so", RTLD_LAZY);
> if (!handle) {
> fprintf (stderr, "%s\n", dlerror());
> exit(1);
> }
>
> }
>
> /*** Steps for creating load executable***/
> bash-3.00# gcc test.c -ldl
>
> When I run the "a.out" it gives me the following error
>
> bash-3.00# ./a.out
> ld.so.1: a.out: fatal: library.so: open failed: No such file or
> directory
>
> Can someone tell me .. where am I doing wrong ?
>
> Thanks !


In the line
handle = dlopen ("library.so", RTLD_LAZY);
replace "library.so" by "./library.so" or even
better by an absolute pathname.

Spiros Bousbouras

abhi147@gmail.com

2006-08-28, 1:30 am


spibou@gmail.com wrote:

> abhi147@gmail.com wrote:
>
>
> In the line
> handle = dlopen ("library.so", RTLD_LAZY);
> replace "library.so" by "./library.so" or even
> better by an absolute pathname.
>
> Spiros Bousbouras



Thanks a lot ! . It worked :-)

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com