Create and use a dynamic shared library on solaris

advertisements

I am reading the procedure to create and use a dynamic shared library on solaris sparc server. below are the file i have :

> ls -lrt
total 8
-rw-rw-r--   1 nobody   nobody       848 Nov 22 14:45 badmemlib.hh
-rw-rw-r--   1 nobody   nobody      1924 Nov 22 14:46 badmemlib.cc
-rw-rw-r--   1 nobody   nobody       330 Nov 22 14:49 my_prog.cc
>

in the above files badmemlib.hh is the header file using which i am creating a shared library with the below command following what is mentioned in this link.

> CC -G -o libbad.so -h libbad.so -Kpic badmemlib.cc

after i run the above command i have these below files:

> ls -lrt
total 26
-rw-rw-r--   1 nobody   nobody       848 Nov 22 14:45 badmemlib.hh
-rw-rw-r--   1 nobody   nobody      1924 Nov 22 14:46 badmemlib.cc
-rw-rw-r--   1 nobody   nobody       330 Nov 22 14:49 my_prog.cc
-rwxrwxr-x   1 nobody   nobody      8284 Nov 22 15:11 libbad.so*
>

as you can see libbad.so is built without any errors. Now my main is in the file my_prog.cc i am compiling the file my_prog.cc as below which following this: and there were no errors.the binary a.out is built.

> CC -L. -lbad my_prog.cc
> ls -lrt
total 184
-rw-rw-r--   1 nobody   nobody       848 Nov 22 14:45 badmemlib.hh
-rw-rw-r--   1 nobody   nobody      1924 Nov 22 14:46 badmemlib.cc
-rw-rw-r--   1 nobody   nobody       330 Nov 22 14:49 my_prog.cc
-rwxrwxr-x   1 nobody   nobody      8284 Nov 22 15:11 libbad.so*
-rwxrwxr-x   1 nobody   nobody     80224 Nov 22 15:14 a.out*
>

Now if i run a.out then the error i am facing is :

> ./a.out
ld.so.1: a.out: fatal: libbad.so: open failed: No such file or directory
Killed

It mentions that library is not found but as you can see above the library is in the same directory!

Could anybody pls help me!

content of my_prog.cc.it actually generates a sigsegv and and the badmemlib files has a signal handler.

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include "badmemlib.hh"
#ifdef SOLARIS /* needed with at least Solaris 8 */
#include <siginfo.h>
#endif

int main(){
        char *where=(char *)0x123;

        printf( "About to write to %p\n", where);
        strcpy(where,"vijay");

        return(0);
}


You need to tell your application to find .so file from your current directory:

CC -L. -lbad my_prog.cc -R <your current directory>

http://docs.oracle.com/cd/E18659_01/html/821-1383/bkana.html#bkawi