I have the following test program.
#include <iostream>
#include <cstdlib>
using namespace std;
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
int main(int argc, char *argv[])
{
int iret;
iret = pthread_mutex_trylock( & mymutex );
cout << "Test2 !!! " << endl;
pthread_mutex_unlock( & mymutex );
return EXIT_SUCCESS;
}
If I compile it without adding the pthread library I get the error for unresolved error for pthread_mutex_trylock , but only for function pthread_mutex_trylock.
If I replace pthread_mutex_trylock with pthread_mutex_trylock the program is compiled and runnig well also without the -lpthread* option.
If I add the -lpthraed option to the compile commands all runs well
this run well : $ g++ test2.c -o test2 -lpthread
this warn unresolved : $ g++ test2.c -o test2
Example error output:
$ g++ test2.c -o test2
/tmp/ccU1bBdU.o: In function main':pthread_mutex_trylock’
test2.c:(.text+0x11): undefined reference to
collect2: ld returned 1 exit status
If I replace the instruction iret = pthread_mutex_trylock( & mymutex );
with iret = pthread_mutex_lock( & mymutex ); the program compiles and run without error also if didn’t add the pthread libarry to the compile command
I know that is right to have the unresolved error if I didn’t use the -lpthread option , but why I have not the same unresolved error also for other pthread_ function ?
I’m using gcc 4.4.2 on fedora 12
$ g++ --version
g++ (GCC) 4.4.2 20091222 (Red Hat 4.4.2-20)
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Do some have some suggestion about the meaning of this unreference only for pthread_mutex_trylock ?
thanks for the help, Enzo
If you use pthread functions you should link your object files with
-lpthreadand not worry about whether symbols are included inlibc.The rationale behind this is said to be such: some time ago the stubs in libc were used when application that used threads was run on a system without threading support. On such system,
pthread_*functions became linked tolibcstubs that returned errors showing that there’s no threading functionality. While on “threaded” systems they were linked topthreadlibrary and worked correctly.Obviously,
pthread_mutex_trylockfunction appeared after the policy changed to linking with-lpthread. So there’s no stub for it.