Creating a shared library and loading it

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • abhi147@gmail.com

    #1

    Creating a shared library and loading it

    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 source.o
    bash-3.00# gcc -o library.so -Wl,-h,library.so -shared source.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 I am wrong ?

    Thanks !

  • spibou@gmail.com

    #2
    Re: Creating a shared library and loading it

    abhi147@gmail.c om 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 source.o
    bash-3.00# gcc -o library.so -Wl,-h,library.so -shared source.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 I am wrong ?
    You are wrong in posting here about it. Here we only
    deal with standard C and things like dlfcn.h , dlopen()
    etc. are not part of standard C. You should ask the
    question at comp.unix.progr ammer or
    comp.os.linux.d evelopment.apps

    Spiros Bousbouras

    Comment

    • Bill Pursell

      #3
      Re: Creating a shared library and loading it

      abhi147@gmail.c om wrote:
      /*** Steps for *.so creation ***/
      bash-3.00# gcc -fPIC -c test.c
      bash-3.00# gcc -o library.so -shared source.o
      bash-3.00# gcc -o library.so -Wl,-h,library.so -shared source.o
      >
      >
      /*** Steps for creating load executable***/
      bash-3.00# gcc test.c -ldl
      I assume you meant to compile load.c here...
      >
      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 I am wrong ?
      You should either specify "./library.so" in load.c, or
      set LD_LIBRARY_PATH . (At least, that works on
      Linux...since dlopen() is not standard C, we are already
      OT, and this thread really belongs on a different NG).
      For example:

      [tmp]$ export LD_LIBRARY_PATH =
      [tmp]$ ./a.out
      library.so: cannot open shared object file: No such file or directory
      [tmp]$ export LD_LIBRARY_PATH =.
      [tmp]$ ./a.out
      [tmp]$




      Stylistically, it's a bit odd to have a file called library.so, since
      that implies that the library's name is "rary". The usual
      naming convention is libNAME.so.

      Comment

      • abhi147@gmail.com

        #4
        Re: Creating a shared library and loading it


        Bill Pursell wrote:
        abhi147@gmail.c om wrote:
        >
        /*** Steps for *.so creation ***/
        bash-3.00# gcc -fPIC -c test.c
        bash-3.00# gcc -o library.so -shared source.o
        bash-3.00# gcc -o library.so -Wl,-h,library.so -shared source.o


        /*** Steps for creating load executable***/
        bash-3.00# gcc test.c -ldl
        >
        I assume you meant to compile load.c here...

        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 I am wrong ?
        >
        You should either specify "./library.so" in load.c, or
        set LD_LIBRARY_PATH . (At least, that works on
        Linux...since dlopen() is not standard C, we are already
        OT, and this thread really belongs on a different NG).
        For example:
        >
        [tmp]$ export LD_LIBRARY_PATH =
        [tmp]$ ./a.out
        library.so: cannot open shared object file: No such file or directory
        [tmp]$ export LD_LIBRARY_PATH =.
        [tmp]$ ./a.out
        [tmp]$
        >
        >
        >
        >
        Stylistically, it's a bit odd to have a file called library.so, since
        that implies that the library's name is "rary". The usual
        naming convention is libNAME.so.
        Thanks Bill . It worked :- )

        Comment

        Working...