Results 1 to 4 of 4

Thread: cant use pthreads,

  1. #1
    Join Date
    Nov 2008
    Beans
    13

    cant use pthreads,

    Hello,
    I am getting trouble to use the pthread.h.

    i got inside my code:
    #include <pthread.h>

    Code:
     file /usr/include/pthread.h
    /usr/include/pthread.h: ASCII C program text
    (it is there)

    when i try to compile it:

    Code:
    techmago@LuisSony ~ $ gcc -lpthread teste.c 
    /tmp/ccQo11xG.o: In function `main':
    teste.c:(.text+0x39): undefined reference to `pthread_create'
    teste.c:(.text+0x61): undefined reference to `pthread_create'
    teste.c:(.text+0x89): undefined reference to `pthread_create'
    teste.c:(.text+0xb1): undefined reference to `pthread_create'
    teste.c:(.text+0xc9): undefined reference to `pthread_join'
    teste.c:(.text+0xdd): undefined reference to `pthread_join'
    teste.c:(.text+0xf1): undefined reference to `pthread_join'
    teste.c:(.text+0x105): undefined reference to `pthread_join'
    collect2: ld returned 1 exit status
    i am with the last ubuntu (11.10) Which packtage i am missing?
    with the ubuntu 10.4 is the same.

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: cant use pthreads,

    it should be:
    Code:
    gcc -pthread teste.c
    if you don't want the general pthread variable (which makes a couple other things threadsafe, so its recommended) but only need libpthread you need to place it behind the object/source file like with static libraries:

    Code:
    gcc  teste.c -lpthread
    this is due to the linker flag --as-needed beeing enabled by default in ubuntu 11.10, see man ld

  3. #3
    Join Date
    Nov 2008
    Beans
    13

    Re: cant use pthreads,

    ok, thank you, this solve the issue. but why?

    whats the big difference between

    Code:
    gcc teste.c -lpthread (which work)
    gcc -lpthread teste.c (which have linking problems)
    And why, in some system both work?
    I got no issues with that order in my college computer (some ubuntu, not the latest) and in my server (centOS 5.7)

  4. #4
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: cant use pthreads,

    gcc/ld scan the command line left to right.
    it finds libpthread, does not know of anything that needs it so it forgets about it.
    Then it finds the source file, compiles it, registers the symbols its need and continues, but no libpthread is following to provide the symbols and the one before it has forgotten so you get an error.
    It will only forget unneeded libraries when the ld flag --as-needed is used. This is only default on ubuntu >= 11.10 (and I think mandriva). No other distros I know of uses that flag.

    I think the reason for this dumb behavior is performance, it has always done the same this static libraries.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •