gcc -lpthread a.c causes problem; gcc a.c -lpthread doesn't

Asked by Jian H. L.

$ cat a.c
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NUM_THREADS 5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }

   /* Last thing that main() should do */
   pthread_exit(NULL);
}
$
$ gcc a.c -lpthread
$
$ gcc -lpthread a.c
/tmp/cc30vqGC.o: In function `main':
a.c:(.text+0x81): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
$
$ gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 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.

$

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu gcc-defaults Edit question
Assignee:
No assignee Edit question
Solved by:
mycae
Solved:
Last query:
Last reply:
Revision history for this message
Jian H. L. (jhl111016) said :
#1

1. ubuntu sucks
the link position matters?
gcc -lpthread *a.c* causes problem;
gcc *a.c* -lpthread doesn't

$ gcc a.c -lpthread
$
$ gcc -lpthread a.c
/tmp/cc30vqGC.o: In function `main':
a.c:(.text+0x81): undefined reference to `pthread_create'
collect2: ld returned 1 exit status
$
$ gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 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.
$

2. no probem on bsd
$ gcc a.c -lpthread
$ gcc -lpthread a.c
$ gcc --version
gcc (GCC) 4.2.1 20070719
Copyright (C) 2007 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.
$ uname -a
OpenBSD grex.org 5.0 GENERIC#43 i386
$

On Sat, Dec 3, 2011 at 2:41 PM, Jian H. L. <
<email address hidden>> wrote:

> New question #180788 on Ubuntu:
> https://answers.launchpad.net/ubuntu/+question/180788
>
> $ cat a.c
> #include <pthread.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> #define NUM_THREADS 5
>
> void *PrintHello(void *threadid)
> {
> long tid;
> tid = (long)threadid;
> printf("Hello World! It's me, thread #%ld!\n", tid);
> pthread_exit(NULL);
> }
>
> int main (int argc, char *argv[])
> {
> pthread_t threads[NUM_THREADS];
> int rc;
> long t;
> for(t=0; t<NUM_THREADS; t++){
> printf("In main: creating thread %ld\n", t);
> rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
> if (rc){
> printf("ERROR; return code from pthread_create() is %d\n", rc);
> exit(-1);
> }
> }
>
> /* Last thing that main() should do */
> pthread_exit(NULL);
> }
> $
> $ gcc a.c -lpthread
> $
> $ gcc -lpthread a.c
> /tmp/cc30vqGC.o: In function `main':
> a.c:(.text+0x81): undefined reference to `pthread_create'
> collect2: ld returned 1 exit status
> $
> $ gcc --version
> gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
> Copyright (C) 2011 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.
>
> $
>
>
> --
> You received this question notification because you asked the question.
>

Revision history for this message
Best mycae (mycae) said :
#2

Yes, the link position matters. This is not ubuntu's fault, and is a "feature" of gcc.

If I recall correctly, (i may not), the problem is the computational complexity of writing cycle finding and resolving algorithms. It is significantly cheaper to disallow cycles by enforcing a branching structure in your object dependency list.
https://secure.wikimedia.org/wikipedia/en/wiki/Floyd%27s_cycle-finding_algorithm

I believe the guy who was writing the "gold" (google linker d) linker has extensive technical discussion on the topic.

http://www.airs.com/blog/archives/38

Revision history for this message
Jian H. L. (jhl111016) said :
#3

thanks, i learned a lot from you post

Revision history for this message
Jian H. L. (jhl111016) said :
#4

it's how it works on fedora 16. does it mean that those guys on fedora port
are all wrong?

$ gcc -lpthread a.c
$ gcc a.c -lpthread
$ gcc --version
gcc (GCC) 4.6.2 20111027 (Red Hat 4.6.2-1)
Copyright (C) 2011 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.

$

On Sun, Dec 4, 2011 at 11:30 AM, Jian H. L. <
<email address hidden>> wrote:

> Your question #180788 on gcc-defaults in Ubuntu changed:
> https://answers.launchpad.net/ubuntu/+source/gcc-defaults/+question/180788
>
> Status: Answered => Solved
>
> You confirmed that the question is solved:
> thanks, i learned a lot from you post
>
> --
> You received this question notification because you asked the question.
>