Problems with shared library linking and pthreads

Erland Lewin erl@voxi.com
Thu Aug 8 06:14:00 GMT 2002


Hello,

I have problems with pthread-programs dumping core on thread 
initialization depending on how linking is done.
  I would appreciate help on what the correct way for linking libraries is.
  See the files at the end of this E-mail. If they are compiled as follows:

cc -g   -c -o testlib.o testlib.c
ld -shared testlib.o -lpthread -o libtestlib.so
cc -g   -c -o testapp.o testapp.c
cc -L. testapp.o -ltestlib -lpthread -o testapp

and testapp is run, it will crash/hang in pthread_create. Why?
  However, if only one of testlib and testapp are linked with -lpthread, 
then the resulting program will execute correctly.
  From what I have previously understood, shared libraries must be 
linked with the shared libraries that they use in order for libc 
versioning to work properly (that is, it would be incorrect for testlib 
not to be linked with -lpthread, because then the knowledge about which 
version of -lpthread was used is lost. Is this wrong?
  I'm using gcc 3.0.2, binutils 2.12.1, gnu libc 2.2.2.
  I'd be most grateful if someone could clarify the situation, and 
explain the correct way of doing things. And maybe even point me to the 
relevant docs. Please cc: any replies to the list to me.

Thanks,

  Erland


---file start: testlib.c
/*
 * testlib.c
 *
 * Test library (for testing shared library linking problems)
 */

#include <pthread.h>

int testlib_pthread_create( pthread_t *thread, pthread_attr_t *attr,
                                  void * (*start_routine)(void *), void 
*arg )
{
  return pthread_create( thread, attr, start_routine, arg );
}

---file end

---file start: testlib.h
/*
  testlib.h
*/

#include <pthread.h>

int testlib_pthread_create( pthread_t *thread, pthread_attr_t *attr,
                            void * (*start_routine)(void *), void *arg );
---file end

---file start: testapp.c
/*
 * testapp.c
 *
 * To test behaviour with linking of pthread
 */

#include <assert.h>
#include <stdio.h>
#include <pthread.h>
#include "testlib.h"

static void *threadFunc( void *args );

int main( int argc, char **argv )
{
  int err;
  pthread_t thread;
 
  fprintf( stderr, "creating thread.\n" );
 
#if 1
  err = testlib_pthread_create( &thread, NULL, threadFunc, NULL );
#else
  err = pthread_create( &thread, NULL, threadFunc, NULL );
#endif
  assert( err == 0 );
 
  fprintf( stderr, "thread created, joining...\n" );
 
  pthread_join( thread, NULL );
 
  fprintf( stderr, "thread joined, finished.\n" );
 
  return 0;
}

static void *threadFunc( void *args )
{
  return args;
}

---file end





More information about the Binutils mailing list