[Bug 899629] Re: gcc -lpthread a.c causes problem; gcc a.c -lpthread doesn't

Julian Taylor jtaylor.debian at googlemail.com
Sat Dec 3 17:36:04 UTC 2011


this is caused by the linker flag  --as-needed now default from 11.10 onwards.
This flag requires objects to be placed before libraries providing the symbols they need.
It can be disabled with -Wl,--no-as-needed but its generaly preferable to just order the commandline correctly.

** Changed in: gcc-defaults (Ubuntu)
       Status: New => Invalid

-- 
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to gcc-defaults in Ubuntu.
https://bugs.launchpad.net/bugs/899629

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

Status in “gcc-defaults” package in Ubuntu:
  Invalid

Bug description:
  $ 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.

  $ 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);
  }
  $

  ### no probem on bsd with same code
  $ 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
  $

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/gcc-defaults/+bug/899629/+subscriptions




More information about the foundations-bugs mailing list