5 Replies Latest reply on Feb 28, 2003 2:07 AM by 807578

    pthread library and static linking

    807578
      Hi All,

      I'm trying to link static this simple test application:
      ----------------------------------------------------------------------------------------
      #include <stdio.h>
      #include <pthread.h>

      void mythread (void );
      void mythreadexit (void *);

      int main (int argc, char *argv)
      {
      int status;
      pthread_t t1;

      status = pthread_create (&t1, NULL, mythread, NULL);
      if (status)
      {
      printf ("pthread_create() return %d\n", status);
      exit (status);
      }

      status = pthread_join (t1, NULL);
      if (status)
      {
      printf ("pthread_join() return %d\n", status);
      exit (status);
      }

      return (0);
      }

      void mythread (void arg)
      {
      pthread_cleanup_push (mythreadexit, NULL);

      sleep (10);

      pthread_cleanup_pop (0);
      }

      void mythreadexit (void *arg)
      {
      pthread_exit (NULL);
      }
      ----------------------------------------------------------------------------------------
      [roberto@ultrasparc10 roberto]$ gcc -static -o t1 t1.c
      Undefined first referenced
      symbol in file
      _getfp                              /var/tmp//cc0ZQVGL.o
      ld: fatal: Symbol referencing errors. No output written to t1
      collect2: ld returned 1 exit status

      As you can see the _getfp() function is called on
      /usr/include/pthread.h:pthread_cleanup_push() macro.
      I've searched on /usr/lib for some missed libs but trying to nm
      inside I got only one library where is defined this function on
      libpthread.so as shown below:

      [roberto@ultrasparc10 roberto]$ cd /usr/lib
      [roberto@ultrasparc10 lib]$ nm libpthread.so|grep _getfp
      0000000000003ca4 T _getfp
      [roberto@ultrasparc10 lib]$ nm libc.so|grep _getfp
      [roberto@ultrasparc10 lib]$ nm libc.a|grep _getfp
      [roberto@ultrasparc10 lib]$ nm libpthread.a|grep _getfp
      nm: libpthread.a: No such file or directory
      [roberto@ultrasparc10 lib]$ nm -o * 2>&1|grep _getfp
      libproc.so:000000000000cecc T Plwp_getfpregs
      libproc.so.1:000000000000cecc T Plwp_getfpregs
      libpthread.so:0000000000003ca4 T _getfp
      libpthread.so.1:0000000000003ca4 T _getfp
      libthread.so:0000000000015e50 T _getfp
      libthread.so.1:0000000000015e50 T _getfp
      libthread_db.so:00000000000036b4 T __td_thr_getfpregs
      libthread_db.so:00000000000036b4 W td_thr_getfpregs
      libthread_db.so.1:00000000000036b4 T __td_thr_getfpregs
      libthread_db.so.1:00000000000036b4 W td_thr_getfpregs

      My question is: how can I link static above pthread's program ?
      Removing pthread_cleanup_push() is too simple I need it ;-)!

      Thanks in advance.

      Roberto Fichera.
        • 1. Re: pthread library and static linking
          807578
          I guess you are not linking with the pthread library.
          try suplying library name at gcc command line.
          • 2. Re: pthread library and static linking
            807578
            Since you do not see libthread.a or libpthread.a in /usr/lib you could assume
            that you can't link a threaded program statically on Solaris.

            This is the case, as far as I know.

            You can link statically with all other libraries (if you want) but you still
            need to link dynamically with the threads library.

            Why do you need static linking?
            • 3. Re: pthread library and static linking
              807578
              I want to be independent by the system libraries.
              After I've tested my application is working as aspected
              I prefer to link it as full static to avoid bug due to
              changes on the sys libraries. Yes I know this is a
              little Mbit ;-) pessimistic but for a realtime I prefer
              a full bloat binary ... so why introduce or expect
              some abnormal program execution if this could be
              avoided by a "good" static link ?
              • 4. Re: pthread library and static linking
                807578
                  I have heard that Sun is intent on discontinuing support for static system libraries in Solaris 10. So, as a workaround for your situation, why don't you just copy libpthread.so.1 to your own private lib directory in your distribution of your binary, and make a shell script to start your program that does:

                ====
                #!/bin/sh

                LD_LIBRARY_PATH=/path/to/your/programs/directory/lib
                export LD_LIBRARY_PATH

                ./yourbinary
                ====

                    /kristofer
                • 5. Re: pthread library and static linking
                  807578
                    If I have misunderstood your question, which is quite possible now that I reread it, maybe this is what you are looking for. I have the impression that you also are trying to have your functions called first. If that is the case, read the following article: http://supportforum.sun.com/freesolaris/techfaqs.html?techfaqs_3075

                    If not, my first reply should suffice.

                      /kristofer