pthread in ubuntu 10.10

May 17, 2012 at 3:44am
I wrote lines in ubuntu including the function pthread.An error raised:no referrence to the pthread_create;

Then I googled it ,install the glibc-doc but no change in the result.I can't still man the pages about the pthread family function. Someone knows what is the matter?Please help me.Thank you in advance!!
May 17, 2012 at 3:56am
Compile using the -pthreads flag. That may sort it out.

eg.

g++ -o myprog -pthreads myprog.cpp
Last edited on May 17, 2012 at 3:57am
May 17, 2012 at 3:58am
I tried it. It didn't work.
May 17, 2012 at 4:10am
Can you provide the exact error message please?
May 17, 2012 at 4:20am
ok,“undefined reference to `pthread_create'” and I have included the header in it.
May 17, 2012 at 10:14am
Undefined reference indicates that your linker cannot find the library containing the actual function body. The header will generally contain the function prototype; the library contains the actual code that does the actual work.

You need to link to the threading library. On the command line, probably something like

-pthread

going by the man page ( http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_create.3.html )
May 17, 2012 at 2:17pm
You need to link the pthread library, so it's -lpthread.
-pthread(s) implies -lpthread for the linker.
Last edited on May 17, 2012 at 3:29pm
May 17, 2012 at 2:48pm
No, just -pthread (or -pthreads, depending on your system).

That will link to pthreads and may cause other side effects as well. Just remember to use -pthread at the compiling AND at the linking stage.
Last edited on May 17, 2012 at 2:48pm
Topic archived. No new replies allowed.