From: Jan-Benedict Glaw <jbglaw@lug-owl.de>
To: linux-c-programming@vger.kernel.org
Subject: Re: compiling programs which use pthreads
Date: Sun, 25 Jan 2004 10:56:53 +0100 [thread overview]
Message-ID: <20040125095653.GV8401@lug-owl.de> (raw)
In-Reply-To: <1075023389.27269.12.camel@qa.intotoind.com>
[-- 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 --]
prev parent reply other threads:[~2004-01-25 9:56 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-01-25 9:36 compiling programs which use pthreads syedk
2004-01-25 9:23 ` David Erman
2004-01-25 9:44 ` Cesar Rincon
2004-01-25 9:56 ` Jan-Benedict Glaw [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20040125095653.GV8401@lug-owl.de \
--to=jbglaw@lug-owl.de \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).