* Re: compiling programs which use pthreads
2004-01-25 9:36 compiling programs which use pthreads syedk
2004-01-25 9:23 ` David Erman
@ 2004-01-25 9:56 ` Jan-Benedict Glaw
1 sibling, 0 replies; 4+ messages in thread
From: Jan-Benedict Glaw @ 2004-01-25 9:56 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 2850 bytes --]
On Sun, 2004-01-25 15:06:29 +0530, syedk <syedk@intotoinc.com>
wrote in message <1075023389.27269.12.camel@qa.intotoind.com>:
> 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]
>
> 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.
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:-(
> 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?
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).
> 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 -> -lpthread
math.h -> -lm
gtk/gtk.h, gdk/gdk.h -> `pkg-config --cflags gtk-2.0`
or `gtk-config --cflags`
...
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +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));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread