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: ...
- 05-16-2008 #1Just 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
- 05-16-2008 #2Linux Newbie
- Join Date
- Mar 2008
- Location
- Hyderabad
- Posts
- 109
Have you included the header
<pthread.h>
- 05-16-2008 #3
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
- 05-16-2008 #4Just Joined!
- Join Date
- May 2008
- Posts
- 16
thanks a lot the -lpthread option worked. can you please elaborate on the working of -lpthread.
Thanks
- 05-16-2008 #5
Quoth digvijay.gahlot:
He may or may not have. But that isn't his main problem. We know this because the errorHave you included the header
<pthread.h>
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.Code:In function `main': threading.c.text+0x6b): undefined reference to `pthread_create' collect2: ld returned 1 exit status
Six thoughts here.
First, martin910, you can fix your error by adding this option:
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 overCode:-pthread
Second, digvijay.gahlot is correct in that you should addCode:-lpthread
to your source file if you haven't already.Code:#include <pthread.h>
Third, if you are not already doing this, you should add
to your compiling options. It will catch many things that could otherwise give you trouble without you knowing exactly why.Code:-Wall
Fourth, this is dangerous:
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.i would liked to be briefed regarding multithreading.
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.
- 05-16-2008 #6Linux Newbie
- Join Date
- Mar 2008
- Location
- Hyderabad
- Posts
- 109
We know this because the error
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.Code:In function `main': threading.c.text+0x6b): undefined reference to `pthread_create' collect2: ld returned 1 exit status
Hey thanks for enlightment.
- 05-16-2008 #7Just 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.
- 05-16-2008 #8
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.


Reply With Quote