FW: GCC 2.95.3 Cross-Compler : Error "undefined reference to `pthread_create'"

Rocha Iran-QIR001 Iran.Rocha@motorola.com
Fri Sep 28 10:33:00 GMT 2001


Hello,

We found out that GCC is not parsing correctly the input parameters. See the e-mail below
for more info. We thought that we were making something wrong and we first submitted this
problem to gcc-help. Since we found out that the bug was the reason of the problem, we are
forwarding the report to gcc-bugs.

Thanks,
Rocha

-----Original Message-----
From: Rocha Iran-QIR001 
Sent: Friday, September 28, 2001 11:52 AM
To: 'gcc-help@gcc.gnu.org'
Subject: RE: GCC 2.95.3 Cross-Compler : Error "undefined reference to
`pthread_create'" 


Hello,

We found out what was the reason of the problem. Apparently, gcc is not parsing correctly
the input parameters. See below:

mach(kuser)[310]> which gcc
/usr/misc/GNU/gcc-2.95.3/bin/gcc
mach(kuser)[311]> which ld
/usr/misc/GNU/binutils-2.11/i686-pc-linux-gnu/bin/ld
mach(kuser)[312]> which i686-pc-linux-gnu-ld
/usr/misc/GNU/binutils-2.11/bin/i686-pc-linux-gnu-ld

Now, let's compile the source code:

mach(kuser)[314]> gcc -bi686-pc-linux-gnu -lpthread main.c
/var/tmp/ccy0gerj.o: In function `main':
/var/tmp/ccy0gerj.o(.text+0x1d): undefined reference to `pthread_create'
collect2: ld returned 1 exit status

The case above fails because "-lpthread" is the third parameter to be passed. Now, if we
move it to the tail of the command line ...

mach(kuser)[315]> gcc -bi686-pc-linux-gnu main.c -lpthread
mach(kuser)[316]> ls -l a.out
-rwxr-xr-x   1 kuser   suin  1175107 Sep 28 11:43 a.out*

Voila! Everything works fine.

I thought GCC would be able to parse the parameters correctly even if they are not 
following an specific input order. I am going to submit this case to the bugs mailing
list.

Best Regards,
Rocha

-----Original Message-----
From: Rocha Iran-QIR001 
Sent: Thursday, September 27, 2001 3:31 PM
To: 'gcc-help@gcc.gnu.org'
Subject: GCC 2.95.3 Cross-Compler : Error "undefined reference to
`pthread_create'" 


Hello,

We are having problems to compile a C program that uses threads with GCC configured as a cross-compiler. The cross-compiler is hosted at a Sparc Solaris 2.6 machine and its target is Intel-PC running Linux. The C program is shown below:

	#include<stdio.h>
	#include<pthread.h>

	int workfunc(void);

	int main(void)
	{
	   int status;
	   pthread_t thread;
	   pthread_attr_t *thread_attr=NULL;
	   status=pthread_create(&thread,thread_attr,(void *)workfunc,0);
	   printf("pthread status %i\n",status);
	   exit(0);
	}
 
	/* Linux API to return the errno after a failure */
	int workfunc(void)
	{
	   printf("Thread spawned successfully");   
	}

This C program compiles and runs fine if I do not try to use the cross-compiler. If I try to use the cross-compiler, I get the following error message:

	usr@es0 % which gcc
	/usr/pd/GNU/gcc-2.95.3/bin/gcc
	usr@es0 % which as
	/usr/pd/GNU/binutils-2.11/i686-pc-linux-gnu/bin/as
	usr@es0 % which ld
	/usr/pd/GNU/binutils-2.11/i686-pc-linux-gnu/bin/ld
	usr@es0 % gcc -bi686-pc-linux-gnu main.c
	/var/tmp/ccUnrthq.o: In function `main':
	/var/tmp/ccUnrthq.o(.text+0x1d): undefined reference to `pthread_create'
	collect2: ld returned 1 exit status

This installation of GCC as a cross-compiler works fine with other source codes but always fails on this case.

GCC is configured as follows:

GCC version     = 2.95.3
Host            = sparc-sun-solaris2.6
Target          = i686-pc-linux-gnu

We configured it to use the GNU Binutils v2.11 cross-assembler and linker (--with-gnu-as and --with-gnu-ld).

We also configured it with the options "--with-headers" and "--with-libs" to copy the libs and include files located at the native GCC installed in the target machine. 

We have also tried four different ways to configured it:

1. Without the "--enable-threads" option: 
      We are able to build GCC but it fails to compile the code shown above
      displaying same error message.

2. With "--enable-threads" but without specifying the type of thread:
      We are able to build GCC but it fails to compile the code shown above
      displaying same error message.
      
3. With "--enable-threads=pthreads":
      The make process is interrupted with the following error message:

	done
	_eh
	In file included from ../../gcc-2.95.3/gcc/gthr.h:98,
	                 from ../../gcc-2.95.3/gcc/libgcc2.c:3034:
	gthr-default.h:1: gthr-pthreads.h: No such file or directory
	make[1]: *** [libgcc2.a] Error 1
	make[1]: Leaving directory `/home/usr/temp/objdir/gcc'
	make: *** [all-gcc] Error 2
	
4. With "--enable-threads=posix":
      We are able to build GCC but it fails to compile the code shown above
      displaying same error message.

You might need some information about the target machine where we got the include and lib files from. The machine is running Redhat 6.2. The previous kernel version was 2.2.13 (out of the box 6.2 kernel), all packages were upgraded to 2.2.19 as per the RedHat updates 
( ftp://updates.redhat.com/6.2/en/os/i386/ ). We are running GCC 2.95.3 and GNU Binutils 2.10.1 at the target machine. We can compile the same source code shown above in the target machine using the native GCC and Binutils installed there.

There is one more information that might be helpful. We already tried to play with the "-L" GCC option to make sure that GCC is getting the right libraries. Furthermore, if you use the cross-compiled version of the binutils "ar", you can dump the library contents and verify that pthread_create is in the library libpthread.a. We have also verified that if you specify a "bad" library when attempting to cross-compile, it will give you an error. Therefore, the fact that it accepts the libpthread.a library without error means it is found and it is in acceptable format, but for some reason it can't link to the pthread_create 
function call. 

Please, would you be able to help us? Has anyone faced a similar problem before?

Any help would be appreciated.

Best Regards,
Rocha



More information about the Gcc-bugs mailing list