Results 1 to 4 of 4

Thread: how to read .so file in ubuntu

  1. #1
    Join Date
    Sep 2009
    Beans
    13

    how to read .so file in ubuntu

    hi every one,
    I am again face a problem. According your suggestion i can convert a .c file to .so file and tell me in which way i can open that .so file i.e. i want to see what will be the code after creating my .so file. I will be highly obliged if any one can give me more information .so file related.

  2. #2
    Join Date
    Apr 2009
    Location
    Hong Kong
    Beans
    91
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: how to read .so file in ubuntu

    .so file is a shared library. It is not directly usable. It is fed into the linker when linking your programs.

    Example:

    foo.c:
    Code:
    #include<stdio.h>
    #include"foo.h"
    
    void foo(void){
     puts("foo");
    }
    foo.h:
    Code:
    #ifndef __foo_h
    # define __foo_h
    # ifdef __cplusplus
    extern "C"{
    # endif
    void foo(void);
    # ifdef __cplusplus
    }
    # endif
    #endif
    Build the library:
    Code:
    $ cc -std=c99 -fPIC -c foo.c -o foo.o # preprocess, compile, assemble foo.c to yield object file foo.o
    $ cc -shared -Wl,-soname,libfoo.so.1 foo.o -o libfoo.so.1.0.0 # link foo.o to yield shared object libfoo.so.1.0.0
    $ ln -s libfoo.so.1.0.0 libfoo.so.1
    $ ln -s libfoo.so.1 libfoo.so # libfoo.so.1 is needed for run-time linking, libfoo.so is needed for compile-time linking
    Install the library:
    Code:
    # cp foo.h /usr/local/include
    # cp -a libfoo.so* /usr/local/lib
    # chown root:root /usr/local/lib/libfoo.so* /usr/local/include/foo.h
    # ldconfig
    To see what functions are inside the library
    Code:
    $ nm /usr/local/lib/libfoo.so | grep ' T '
    Output:
    Code:
    0000000000000648 T _fini
    00000000000004e8 T _init
    00000000000005fc T foo
    The first 2 are added by the linker, the last one is the one that comes from the object code.

    To build an application against this library:

    test.c
    Code:
    #include<foo.h> // note that foo.h is now a standard header
    int main(void){
     foo();
    }
    Code:
    $ cc -std=c99 test.c -lfoo -o test # /usr/local/lib/libfoo.so is used
    $ ./test # /usr/local/lib/libfoo.so.1 is used
    output:
    Code:
    foo
    To verify that the shared library is really used:
    Code:
    $ ldd test
    output:
    Code:
            linux-vdso.so.1 =>  (0x00007fffbc9d9000)
            libfoo.so.1 => /usr/local/lib/libfoo.so.1 (0x00002b82fa537000)
            libc.so.6 => /lib/libc.so.6 (0x00002b82fa739000)
            /lib64/ld-linux-x86-64.so.2 (0x00002b82fa317000)
    To see which functions in test come from the shared libraries:
    Code:
    $ nm test | grep ' U '
    Output:
    Code:
                     U __libc_start_main@@GLIBC_2.2.5
                     U foo
    In this place, __libc_start_main@@GLIBC_2.2.5 is a symbol defined by the C library, foo is a symbol defined by our shared library. It is completed by loading /usr/local/lib/libfoo.so.1 at runtime.
    Last edited by miklcct; September 3rd, 2009 at 10:16 AM.
    My site about computing:
    http://miklcct.csproject.org/
    Be sure to see my photos!

  3. #3
    Join Date
    Sep 2009
    Beans
    13

    Re: how to read .so file in ubuntu

    Quote Originally Posted by miklcct View Post
    .so file is a shared library. It is not directly usable. It is fed into the linker when linking your programs.

    Example:

    foo.c:
    Code:
    #include<stdio.h>
    #include"foo.h"
    
    void foo(void){
     puts("foo");
    }
    foo.h:
    Code:
    #ifndef __foo_h
    # define __foo_h
    # ifdef __cplusplus
    extern "C"{
    # endif
    void foo(void);
    # ifdef __cplusplus
    }
    # endif
    #endif
    Build the library:
    Code:
    $ cc -std=c99 -fPIC -c foo.c -o foo.o # preprocess, compile, assemble foo.c to yield object file foo.o
    $ cc -shared -Wl,-soname,libfoo.so.1 foo.o -o libfoo.so.1.0.0 # link foo.o to yield shared object libfoo.so.1.0.0
    $ ln -s libfoo.so.1.0.0 libfoo.so.1
    $ ln -s libfoo.so.1 libfoo.so # libfoo.so.1 is needed for run-time linking, libfoo.so is needed for compile-time linking
    Install the library:
    Code:
    # cp foo.h /usr/local/include
    # cp -a libfoo.so* /usr/local/lib
    # chown root:root /usr/local/lib/libfoo.so* /usr/local/include/foo.h
    # ldconfig
    To see what functions are inside the library
    Code:
    $ nm /usr/local/lib/libfoo.so | grep ' T '
    Output:
    Code:
    0000000000000648 T _fini
    00000000000004e8 T _init
    00000000000005fc T foo
    The first 2 are added by the linker, the last one is the one that comes from the object code.

    To build an application against this library:

    test.c
    Code:
    #include<foo.h> // note that foo.h is now a standard header
    int main(void){
     foo();
    }
    Code:
    $ cc -std=c99 test.c -lfoo -o test # /usr/local/lib/libfoo.so is used
    $ ./test # /usr/local/lib/libfoo.so.1 is used
    output:
    Code:
    foo
    To verify that the shared library is really used:
    Code:
    $ ldd test
    output:
    Code:
            linux-vdso.so.1 =>  (0x00007fffbc9d9000)
            libfoo.so.1 => /usr/local/lib/libfoo.so.1 (0x00002b82fa537000)
            libc.so.6 => /lib/libc.so.6 (0x00002b82fa739000)
            /lib64/ld-linux-x86-64.so.2 (0x00002b82fa317000)
    To see which functions in test come from the shared libraries:
    Code:
    $ nm test | grep ' U '
    Output:
    Code:
                     U __libc_start_main@@GLIBC_2.2.5
                     U foo
    In this place, __libc_start_main@@GLIBC_2.2.5 is a symbol defined by the C library, foo is a symbol defined by our shared library. It is completed by loading /usr/local/lib/libfoo.so.1 at runtime.
    Hi

    Thanks for your help. Since i am new in ubuntu so there arise few questions. These are as follows:

    1) In your comments you wrote foo.h. Now my question is whether it is (filename.h) necessary or not.

    2) If you know any URL link on .so file related please post here for developing my knowledge on Ubuntu and .so file related.

  4. #4
    Join Date
    Apr 2009
    Location
    Hong Kong
    Beans
    91
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: how to read .so file in ubuntu

    Here's a HOWTO for libraries:

    http://tldp.org/HOWTO/Program-Library-HOWTO/

    The source codes contain .c and .h files. To convert a .c file to a .so file, the compiler actually performs 4 steps:

    1. Run cpp to generate .i file from .c file (Preprocessing)
    2. Run cc1 to generate .s file from .i file (Compiling)
    3. Run as to generate .o file from .s file (Assembling)
    4. Run ld to generate .so file from .o file(s) (Linking) (A shared object can contain more than 1 .o files)


    The .h file is needed in the cpp stage. When you distribute a binary image of the library, only the .so.a and .so.a.b.c (where .a.b.c is the version number) is needed. When you distribute the development files of the library, the .so and .h files is needed. Normally, .so.a and .so.a.b.c is placed in /usr/lib, for essential libraries (needed for programs in /bin), they are placed in /lib. The .so file is placed in /usr/lib and the .h file is placed in /usr/include. There may be some additional files (like libtool, pkg-config) but they are not relevant to the topic. Local-installed libraries by hand (out of package management) are normally installed in /usr/local/{lib,include}.
    My site about computing:
    http://miklcct.csproject.org/
    Be sure to see my photos!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •