This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Problems with shared library linking and pthreads
- From: Erland Lewin <erl at voxi dot com>
- To: binutils at sources dot redhat dot com, bug-glibc at sources dot redhat dot com
- Date: Thu, 08 Aug 2002 15:14:21 +0200
- Subject: Problems with shared library linking and pthreads
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