compiling programs which use pthreads
Hi all,
I am getting strange results while compiling programs which use
pthreads.
I am working on a RedHat Linux on a 2.4 kernel.
to start with i wrote a simple program as follows:
#include <pthread.h>
#include <stdio.h>
void * hello(void *);main()
{
pthread_t tid;
pthread_create(&tid,NULL,hello,NULL);
}
void *hello(void *arg)
{
printf("Hello World/n");
}but when i tried to compile using gcc it is giving linktime error:
undefined reference to pthread_create.
but why is it giving this error when the header file pthread.h is present.
when i tried to do cat /proc/ksyms | grep pthread_create no matches were found. Does that mean that pthread library is not installed in my system?
Can anyone suggest how to hack this and compile my program apart from downloading some freely available user-level libraries(like gnu pthreads).
Thanx
Syed SK
You need to link with -lpthread. I.e.
gcc foo.c -lpthread
Quoted 6 lines · syedk
Hi all,
I am getting strange results while compiling programs which use
pthreads.
I am working on a RedHat Linux on a 2.4 kernel.
to start with i wrote a simple program as follows:
/ Dave
Ipsissima verba David Erman:
Quoted 4 lines
You need to link with -lpthread. I.e.
gcc
foo.c-lpthread
You should probably define _REENTRANT as well, e.g.
gcc -D_REENTRANT -o foo foo.c -lpthread
so you get a per-thread errno and thread-safe "*_r" functions (getlogin_r(), gethostbyaddr_r(), gethostbyname_r(), and many others). See the "Feature Test Macros" section of the glibc manual, and of course the "POSIX Threads" section for all the gory details.
-CR
Signature · 1 line
Ceterum censeo: SCO delenda est.
On Sun, 2004-01-25 15:06:29 +0530, syedk <[email protected]> wrote in message <[email protected]>:
Quoted 5 lines
Hi all,
I am getting strange results while compiling programs which use
pthreads.
I am working on a RedHat Linux on a 2.4 kernel.
to start with i wrote a simple program as follows:
[program snipped]
Quoted 5 lines
but when i tried to compile using gcc it is giving linktime error:
undefined reference topthread_create.
but why is it giving this error when the header filepthread.his present.
Header files describe functions and data types. So if you include a header file, your program gets knowledge about some details. But that's all.
If you actually want to *use* these external functions and variables, you also need to tell the compiler (well, actually the linker) to actually merge these into your program (or to let your program reference some other, external object file called "dynamic library). This is done my the -lXXXXX parameter. In your case (you want to use parts of the pthreads package, you need to supply "-lpthread".
It's the same with eg. mathematical functions. Including "math.h" only gives you some names. To be able to really use them, you need to "-lm".
Unfortunately, you do not need to supply the name of the normal C library, libc. This confuses newbies quite often:-(
Quoted 3 lines
when i tried to do cat
/proc/ksyms| greppthread_createno matches were found. Does that mean that pthread library is not installed in my system?
Most probably, it is. You actually name it "pthread library", which it is. It is *not* a part of the linux kernel, so you won't find parts of in in /proc/ksyms.
You do find things like "open", "close" and the like there, but this really is more an accident. There's an "open" function inside the kernel (which you don't use, at least not directly) and another one in the C library (libc). The latter is what you use (without the need to add "-lc" to the compiler/linker call).
Quoted 3 lines
Can anyone suggest how to hack this and compile my program apart from downloading some freely available user-level libraries(like gnu pthreads).
$ gcc -o pthread_example pthread_example.c -lpthread
However, I admit that it isn't really easy for newbies to figure out what they need to link:
stdio.h, stdlib.h, unistd.h -> -lc (implicitely done)pthread.h -> -lpthreadmath.h -> -lmgtk/gtk.h, gdk/gdk.h -> pkg-config --cflags gtk-2.0
or `gtk-config --cflags`
...
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));