C Multithreading

Posted on 2003-02-20
Medium Priority
Last Modified: 2010-04-21
Please look at the following code: It simply creates a thread.

#include <stdio.h>
#include <pthread.h>

void* thr(void*);

   pthread_t thread1;
   printf("error creating thead");

void * thr(void *arg)
   printf("Hi from thread");

    This code, when compiled, makes the following complaint:

/tmp/ccq5LR1c.o: In function 'main':
/tmp/ccq5LR1c.o(.text+0x14): undefined reference to 'pthread_create'

   I use linux kernel 2.2(caldera), and type 'cc' to compile the program. Please tell me what to do.
Question by:vijay_rangaraj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 24

Accepted Solution

fridom earned 90 total points
ID: 7992577
It's  a linker problem you do not have linke against libphtread I guess. Check if you have it installed, if not install it than add it to the compile command line
cc -o my_program my_program.c -lphtread

(assuming it is installed in a path where the linker looks int for libraries if  that is not the case use the -L flag to add the dirctory to that library)

See two the C programming FAQ


Expert Comment

ID: 7992633
I agree that it's a linker problem, but on most compilers you're supposed to use the -pthread option (rather than -lpthread) since it sometimes defines other macros to use reentrant functions or whatever (cc -o my_program my_program.c -pthread).  Good luck.

Author Comment

ID: 8007983
Good work,friedrich and mnashadka, both your suggestions worked for me, (ie the programs compiled correctly both with -pthread and -lpthread). Thanks again.


Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question