shared objects - Linux

This is a discussion on shared objects - Linux ; Hi! I have some problems getting around with shared libraries under linux. I'm an experienced Delphi developer and way used to the windows way of DLLs, so don't blame me as I may still think too much in the way ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: shared objects

  1. shared objects

    Hi!

    I have some problems getting around with shared libraries under linux.
    I'm an experienced Delphi developer and way used to the windows way of
    DLLs, so don't blame me as I may still think too much in the way of how
    things are down in Windows/Delphi...

    Allright, here we go: At the moment I try to learn how to write shared
    objects under linux and have some problems. I read the HOWTO on
    [1]tldp.org and I read [2]IBM's paper on the topic, but both didn't
    really work. Now here is what i've done so far that still won't work.
    I'm a bit frustrated now, as it seems that libraries under linux are way
    harder to write/use/bla than it was under windows:

    1. I created the file "libhello.c":
    #include "stdio.h"

    void hello(void) {
    printf("Hello, this comes from within a library");
    }

    2. I created the header file "libhello.h":
    void hello(void);

    3. I created a "client"-app (demo_use.c):
    #include "libhello.h"

    int main(void) {
    hello();
    return 0;
    }

    4. I compiled and linked the library:
    gcc -fPIC -c libhello.c
    ld -shared -soname libhello.so.1 -o libhello.so.1.0 -lc libhello.o

    5. I copied the .so* files to /usr/local/lib

    6. I ran "ldconfig -v -n ." when located in /usr/local/lib

    7. I created an additional symbolic link to the libhelloso.1.0 fiel:
    ln -s libhello.so.1.0 libhello.so

    8. I compiled the demo client:
    gcc -o demo_use demo_use.c -lhello

    9. I tried to run "demo_use", but everything I get is:
    "error while loading shared libraries: libhello.so.1: cannot open
    shared object file: No such file or directory"

    Now im stuck with this and don't really know what to do. It would be
    really nice if you could tell me how to install the library properly
    and/or show me a howto where the "shared object"-topic is covered a bit
    better than in those I just read...

    Best Regards and sorry for my english, it seems to be rusty
    Nicolai Waniek

    [1] http://www.tldp.org/HOWTO/Program-Li...WTO/index.html
    [2] http://www-128.ibm.com/developerworks/library/l-shobj/

  2. Re: shared objects



    On 1月8日, 上午7时23分, rochus wrote:
    > Hi!
    >
    > I have some problems getting around with shared libraries under linux.
    > I'm an experienced Delphi developer and way used to the windows way of
    > DLLs, so don't blame me as I may still think too much in the way of how
    > things are down in Windows/Delphi...
    >
    > Allright, here we go: At the moment I try to learn how to write shared
    > objects under linux and have some problems. I read the HOWTO on
    > [1]tldp.org and I read [2]IBM's paper on the topic, but both didn't
    > really work. Now here is what i've done so far that still won't work.
    > I'm a bit frustrated now, as it seems that libraries under linux are way
    > harder to write/use/bla than it was under windows:
    >
    > 1. I created the file "libhello.c":
    > #include "stdio.h"
    >
    > void hello(void) {
    > printf("Hello, this comes from within a library");
    > }
    >
    > 2. I created the header file "libhello.h":
    > void hello(void);
    >
    > 3. I created a "client"-app (demo_use.c):
    > #include "libhello.h"
    >
    > int main(void) {
    > hello();
    > return 0;
    > }
    >
    > 4. I compiled and linked the library:
    > gcc -fPIC -c libhello.c
    > ld -shared -soname libhello.so.1 -o libhello.so.1.0 -lc libhello.o
    >
    > 5. I copied the .so* files to /usr/local/lib
    >
    > 6. I ran "ldconfig -v -n ." when located in /usr/local/lib
    >
    > 7. I created an additional symbolic link to the libhelloso.1.0 fiel:
    > ln -s libhello.so.1.0 libhello.so
    >
    > 8. I compiled the demo client:
    > gcc -o demo_use demo_use.c -lhello
    >
    > 9. I tried to run "demo_use", but everything I get is:
    > "error while loading shared libraries: libhello.so.1: cannot open
    > shared object file: No such file or directory"
    >

    in /usr/lib/:

    ln -s libhello.so.1.0 libhello.so.1

    > Now im stuck with this and don't really know what to do. It would be
    > really nice if you could tell me how to install the library properly
    > and/or show me a howto where the "shared object"-topic is covered a bit
    > better than in those I just read...
    >
    > Best Regards and sorry for my english, it seems to be rusty
    > Nicolai Waniek
    >
    > [1]http://www.tldp.org/HOWTO/Program-Library-HOWTO/index.html
    > [2]http://www-128.ibm.com/developerworks/library/l-shobj/



  3. Re: shared objects

    rochus writes:

    > I'm a bit frustrated now, as it seems that libraries under linux are way
    > harder to write/use/bla than it was under windows:


    Actually they are simpler to write: all you need is:

    gcc -fPIC -shared -o libhello.so hello.c &&
    cp libhello.so /usr/local/lib

    > 4. I compiled and linked the library:
    > gcc -fPIC -c libhello.c
    > ld -shared -soname libhello.so.1 -o libhello.so.1.0 -lc libhello.o


    Never use 'ld' directly, unless you *know* what you are doing. Always
    use correct compiler driver (gcc for C, g++ for C++) instead.

    > 7. I created an additional symbolic link to the libhelloso.1.0 fiel:
    > ln -s libhello.so.1.0 libhello.so


    The problem is that you are trying to learn too many things at once:
    - how to create shared libraries,
    - how to use external versioning (the -soname and the symlink
    business).

    > It would be
    > really nice if you could tell me how to install the library properly


    In /usr/local/lib you need:

    # Actual library
    libhello.so.1.0

    # Symlink that could later point to an updated version, e.g.
    # libhello.so.1.0.3, or libhello.so.1.1
    # This symlink is used by the runtime linker, because "libhello.so.1"
    # is encoded into the executable as a dependency.

    libhello.so.1 -> libhello.so.1.0

    # Finally, you need a symlink for (static) linker:

    libhello.so -> libhello.so.1

    > and/or show me a howto where the "shared object"-topic is covered a bit
    > better than in those I just read...


    I find Sun "Linker and Libraries Guide" clearer than Linux docs:
    http://docs.sun.com/app/docs/doc/817-1984

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

  4. Re: shared objects

    "Bin Chen" writes:

    >> 5. I copied the .so* files to /usr/local/lib
    >>

    > in /usr/lib/:
    > ln -s libhello.so.1.0 libhello.so.1


    That wouldn't work, since his libs are *not* in /usr/lib.

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

  5. Re: shared objects

    Thanks alot Paul!
    Both for your advise and the link to the sun documentation. First it
    didn't seem to work your way until I found out that /usr/local/lib was
    missing on the ld.so search path.

    best regards,
    Nicolai Waniek

+ Reply to Thread