Looking for a Makefile to create shared lib in Linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tvnaidu
    Contributor
    • Oct 2009
    • 365

    #1

    Looking for a Makefile to create shared lib in Linux

    Looking for simple Makefile to make shared lib (.so) in Linux (I have two C files file1.c and file2.c, also two static libs slib1.a and slib2.a), can I get Makefile to compile all of them to crate final.so shared lib?.


    I wrote this to compile using GNU compiler in Linux, this is not working, thanks in advance.

    CC = gcc
    AR = ar rc
    CFLAGS = -Wall -g -O2 -I. -Iinclude -I../common/include -fPIC
    OBJECTS = file1.o file2.o
    SHARED_LIB = final.so
    LIBS = -lslib1 -lslib2 -lpthread

    $(SHARED_LIB): $(OBJECTS)
    $(CC) $(SHARED_LIB) $(OBJECTS) $(LIBS)

    %.o : %.c
    $(CC) $(CFLAGS) -c $<

    clean:
    $(RM) $(OBJECTS)
    $(RM) $(SHARED_LIB)
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    I think you need some -Wl flags so the linking is correct. Here's a good reference
    http://www.linux.org/docs/ldp/howto/...libraries.html

    Comment

    • tvnaidu
      Contributor
      • Oct 2009
      • 365

      #3
      Thanks.
      I have two static libs slib1.a and slib2.a in other directory, but I copied locally to create .so lib first, but I am getting error when it tries to create .so lib. any idea?
      $ gcc -Wall -g -O2 -I. -Iinclude -fPIC -c file1.c
      $ gcc -Wall -g -O2 -I. -Iinclude -fPIC -c file2.c


      $ gcc -shared -lslib1 -lslib2 -Wl,-soname,libfinal .so.1 -o libfinal.so.1.0 .1 .o file1.0 file2.o -lpthread
      /usr/bin/ld: cannot find -lslib1
      collect2: ld returned 1 exit status
      $

      Comment

      • mac11
        Contributor
        • Apr 2007
        • 256

        #4
        When you do -lslib1 ld is looking for "libslib.a" and can't find it. (see your gcc manpage or google about the -l option). Maybe you can just rename your slib1.a to libslib1.a?

        Comment

        • tvnaidu
          Contributor
          • Oct 2009
          • 365

          #5
          I renamed it, it didn't help. I can see lot of questions on google, but no answers at all. I am scratching my head what to do.

          Comment

          • tvnaidu
            Contributor
            • Oct 2009
            • 365

            #6
            I need to link static libs to create a shared lib, any expert can help?

            Comment

            • tvnaidu
              Contributor
              • Oct 2009
              • 365

              #7
              I made shared lib called libmanager.so by linking static lib libHashTable and pthread, when I do "ldd libmanager.so", It won't show that hashtable?.

              $gcc -Wall -g -O2 -I. -Iinclude -c -fPIC manager.c
              $gcc -shared -Wl,-soname,libmanag er.so.1 -o libmanager.so.1 .0 -L/opt/lib -lHashTable -lpthread manager.o
              $mv libmanager.so.1 .0 /opt/lib
              $ln -sf /opt/lib/libmanager.so.1 .0 /opt/lib/libmanager.so
              $ln -sf /opt/lib/libmanager.so.1 .0 /opt/lib/libmanager.so.1



              localhost lib]# ls
              libHashTable.a
              libmanager.so -> /opt/lib/libmanager.so.1 .0
              libmanager.so.1 -> /opt/lib/libmanager.so.1 .0
              libscoreMgr.so. 1.0
              localhost lib]# ldd libmanager.so
              libpthread.so.0 => /lib/tls/libpthread.so.0 (0xb75d9000)
              libc.so.6 => /lib/tls/libc.so.6 (0xb74a2000)
              /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)
              localhost lib]#

              Comment

              • RRick
                Recognized Expert Contributor
                • Feb 2007
                • 463

                #8
                It looks like you have a couple of issues still outstanding.

                It sounds like you were able to link in the static libraries to the shared library. Like Mac11 said, if you want to use the -l option, then the library must have a specific format. With the -l option, you also use the -L option to define the directory path.

                You can also specify the library directly (dirPath/slib1.a) without the -l option. This is sometimes handy.

                Ldd only shows the shared libraries used by the program or shared library. Since libHashTable is a static library, only the object code needed by the shared library is extracted from the static library. If you want to see what was pulled in from libHashTable.a, try "nm yourSharedLib.s o". The nm command will list out all of the object code routines, but only if the symbol information was compiled into the static library. That's why you want some level of debug info (i.e. -g option) when compiling, even if it is just level 1 (i.e. -g1 option).

                Comment

                • tvnaidu
                  Contributor
                  • Oct 2009
                  • 365

                  #9
                  Thansk Rick I did nm xyz.so, I can see the symbol of Hash. I can use nm and ldd commands for shared libs to findout dependencies also linked files. I am refering Linux docs, incase if any useful commands to debug shared libs, can I get it. thanks in advance.

                  Comment

                  • RRick
                    Recognized Expert Contributor
                    • Feb 2007
                    • 463

                    #10
                    Nm and ldd are the main commands for viewing program internals.

                    strace is real handy when running a program and seeing what is happening at the system or io level.

                    objdump displays all kinds of things for object code files and libraries.

                    This article has more information. http://www.linuxjournal.com/article/7330. The strings command looks interesting.

                    Comment

                    • tvnaidu
                      Contributor
                      • Oct 2009
                      • 365

                      #11
                      Thanks, appreciated. link is very useful.

                      Comment

                      • tvnaidu
                        Contributor
                        • Oct 2009
                        • 365

                        #12
                        when I do nm on one of the shared lib below, I can see like this all HastTable functions (ht_...), but when I do final linking, I am getting "undefined reference" error, does nm means it doesn't link actual functions, may be nm shows just calling functions only, when I do nm on shared lib, I can see those function calls with "U", means undefined right?

                        localhost lib]$ nm libmanager.so | grep ht_
                        U ht_create
                        U ht_delete
                        U ht_destroy
                        U ht_find
                        U ht_insert
                        $

                        final linking error:

                        /opt/lib/libmanager.so: undefined reference to `ht_delete'
                        /opt/lib/libmanager.so: undefined reference to `ht_destroy'
                        /opt/lib/libmanager.so: undefined reference to `ht_insert'

                        Comment

                        • tvnaidu
                          Contributor
                          • Oct 2009
                          • 365

                          #13
                          I did nm and redirected output to a file, then I seached for ht_ function, I found each one at one place only:

                          000025f4 A _GLOBAL_OFFSET_ TABLE_
                          w __gmon_start__
                          U htCalloc
                          U ht_create
                          U ht_delete
                          U ht_destroy
                          U ht_find
                          U ht_insert
                          00000af4 T _init

                          Comment

                          • tvnaidu
                            Contributor
                            • Oct 2009
                            • 365

                            #14
                            Looklike when shared lib gets created, it is not linking static lib which I passing with -l option below (I have static lib file in /opt/lib directory), when I do nm on shared lib, it doesn't know where the code is:

                            $gcc -shared -Wl,-soname,libmanag er.so.1 -o libmanager.so.1 .0 -L/opt/lib -lHashTable -lpthread manager.o

                            Comment

                            • tvnaidu
                              Contributor
                              • Oct 2009
                              • 365

                              #15
                              I was referring this IBM link about shared libs, when I issue ldd command, it show all libraries info, the above ldd command doesn't show hashtable, but according to below link ldd command, it show show.

                              Comment

                              Working...