Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-21-2006, 01:21 AM   #1
Senior Member
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 62
Pthread Problem

The program below uses POSIX pthreads, and the child prints out 5 and the parent prints out 0. On Mac OS X, it compiles and runs correctly. On Linux I get the following error:
/tmp/ccCWW53M.o: In function `main':
q3.c:(.text+0x49): undefined reference to `pthread_create'
q3.c:(.text+0x5a): undefined reference to `pthread_join'
collect2: ld returned 1 exit status
Can anyone tell me what the problem is. I assume that it is a problem with pthread.h, but the problem is the same on Kubuntu, Fedora and Slackware systems that I have tried. pthread.h exists in all cases, and other programs (not using pthreads, but using other system calls) compile and work correctly.

Here is the program:
#include <pthread.h>
#include <stdio.h>

int value = 0;
void *runner(void *param);      /* The Thread*/

int main( int argc, char *argv[])
  int pid;
  pthread_t tid;
  pthread_attr_t attr;

  pid = fork();

  if (pid == 0)         /* Child Process */
    pthread_create(&tid, &attr, runner, NULL);

    printf("CHILD: Value = %d\n",value);        /* Line C*/
  else if (pid > 0)     /* Parent Process */
    printf("PARENT: value = %d\n",value);       /* LINE P*/

void *runner(void *param)
  value = 5;
Thanks in advance for any help
Old 04-21-2006, 01:32 AM   #2
Registered: Nov 2004
Location: Turkey
Distribution: Slackware
Posts: 145

Rep: Reputation: 16
compile with -lpthread option.

gcc x.c -lpthread
Old 04-21-2006, 02:05 AM   #3
Senior Member
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Original Poster
Rep: Reputation: 62
Excellent. Thankyou very much.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
pthread problem with C++. Please Help allomeen Programming 20 12-16-2005 03:37 PM
pthread problem on redhat9 wizardjack Programming 4 02-02-2005 08:54 AM
pthread problem robnob Programming 16 11-12-2003 07:49 PM
pthread signal problem xinwang00 Programming 12 11-11-2003 02:30 PM
pthread signal problem xinwang00 Linux - Software 2 11-07-2003 12:05 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:35 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration