Find the answer to your Linux question:
Results 1 to 8 of 8
hi, i am developed a number of problems while implementing a program that creates threads. int status=pthread_create (&x2[i],x1,startMain,(void*)i); Error shown: In function `main': threading.c .text+0x6b): undefined reference to `pthread_create' collect2: ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    16

    Threading

    hi,
    i am developed a number of problems while implementing a program that creates threads.

    int status=pthread_create (&x2[i],x1,startMain,(void*)i);

    Error shown:
    In function `main':
    threading.c.text+0x6b): undefined reference to `pthread_create'
    collect2: ld returned 1 exit status

    Can anybody help me? i would liked to be briefed regarding multithreading.

    Thanks

  2. #2
    Linux Newbie
    Join Date
    Mar 2008
    Location
    Hyderabad
    Posts
    109
    Have you included the header
    <pthread.h>

  3. #3
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    You need to provide the compiler option -lpthread to let the compiler know where to find the pthread object code.
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  4. #4
    Just Joined!
    Join Date
    May 2008
    Posts
    16
    thanks a lot the -lpthread option worked. can you please elaborate on the working of -lpthread.

    Thanks

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Quoth digvijay.gahlot:
    Have you included the header
    <pthread.h>
    He may or may not have. But that isn't his main problem. We know this because the error
    Code:
    In function `main':
    threading.c.text+0x6b): undefined reference to `pthread_create'
    collect2: ld returned 1 exit status
    is produced by the linker, not the compiler. It is the compiler, not the linker, that would complain if a header file were not included.

    Six thoughts here.

    First, martin910, you can fix your error by adding this option:
    Code:
    -pthread
    to your compiling/linking command. That will include the proper library at link time, thus removing the error message which you report to us. It is slightly preferable over

    Code:
    -lpthread
    Second, digvijay.gahlot is correct in that you should add
    Code:
    #include <pthread.h>
    to your source file if you haven't already.

    Third, if you are not already doing this, you should add
    Code:
    -Wall
    to your compiling options. It will catch many things that could otherwise give you trouble without you knowing exactly why.

    Fourth, this is dangerous:
    i would liked to be briefed regarding multithreading.
    The reason that this is dangerous is that programming using POSIX threads is rather complex. It's not exactly rocket science, but there are many traps for the unwary, and there are many things you need to know that it will not occur to you to ask questions about.

    So run (do not walk) to your nearest bookseller and order the Addison Wesley book Programming with POSIX Threads, by David R. Butenhof. Then settle down and read most of it.

    O'Reilly publishes a similar book. But my copy of it, while it seems to present all the required material, isn't anywhere near as thorough in discussing the traps you can get into.

    Fifth, even if you know everything about POSIX threads programming, it can still be nasty. The reason is that all threads share the same heap space. If the code for one thread contains a bug which causes memory to be stomped on, it might be a completely different thread which blows up because of this. It might take much detective work (I have seen it take weeks) to determine just where the fault lies.

    Moral: Keep your POSIX threads programs as absolutely simple as possible. And don't use POSIX threads unless running speed is absolutely crucial and the communication between threads is frequent enough to even make this an issue. Don't use POSIX threads if all it will do is increase your running speed by, say, two percent. POSIX threads can drive your program maintenance costs through the roof, all for the sake of optimizing your code a tiny bit.

    Sixth, two rules for programmers.

    1. Do not optimize your code.

    And for expert programmers:

    2. Do not optimize your code yet.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Linux Newbie
    Join Date
    Mar 2008
    Location
    Hyderabad
    Posts
    109
    We know this because the error
    Code:
    In function `main':
    threading.c.text+0x6b): undefined reference to `pthread_create'
    collect2: ld returned 1 exit status
    is produced by the linker, not the compiler. It is the compiler, not the linker, that would complain if a header file were not included.


    Hey thanks for enlightment.

  7. #7
    Just Joined!
    Join Date
    May 2008
    Posts
    16
    thanks wje_lb

    but let me know how did you find that the error was provided by the linker and not the compiler. By the error 'undefined reference' it is pretty obvious, but it has sort off shaken my concepts about the various tasks of a compiler. Please, can you explicitly define the jobs of a compiler.

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    The compiler you're using translates your C source code into machine language. But that isn't enough. You also need to include code written and compiled by others and stored in libraries. These libraries are installed on your system at the same time that your C compiler is installed. These libraries do many things. One of these libraries implements POSIX threads for you.

    Once the compiler has translated your code, it is the job of the linker to pull together your code and all necessary libraries to produce a program that can run under Linux.

    The command "cc" or "gcc" normally runs the compiler and the linker as needed.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

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