linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* 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:44   ` Cesar Rincon
  2004-01-25  9:56 ` Jan-Benedict Glaw
  1 sibling, 1 reply; 4+ messages in thread
From: David Erman @ 2004-01-25  9:23 UTC (permalink / raw)
  To: syedk; +Cc: linux-c-programming


You need to link with  -lpthread. I.e.

gcc foo.c -lpthread

syedk wrote:
> 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* compiling programs which use pthreads
@ 2004-01-25  9:36 syedk
  2004-01-25  9:23 ` David Erman
  2004-01-25  9:56 ` Jan-Benedict Glaw
  0 siblings, 2 replies; 4+ messages in thread
From: syedk @ 2004-01-25  9:36 UTC (permalink / raw)
  To: linux-c-programming

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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: compiling programs which use pthreads
  2004-01-25  9:23 ` David Erman
@ 2004-01-25  9:44   ` Cesar Rincon
  0 siblings, 0 replies; 4+ messages in thread
From: Cesar Rincon @ 2004-01-25  9:44 UTC (permalink / raw)
  To: linux-c-programming

Ipsissima verba David Erman:
> 
> 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

-- 
Ceterum censeo: SCO delenda est.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* 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

end of thread, other threads:[~2004-01-25  9:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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).