I trying to compile a simple C program with Pthreads in Ubuntu with codelite.
I have chosen GCC as my compiler and have added the "-pthread" into the C compiler options in the project settings. But it throws an error "undefined reference to pthread_create"
on compile time.
If I call GCC via command line manually it builds and runs.
But when I comment out the offending line it compiles with no errors in codelite.
So I suspect that codelite is not adding the -pthread compiler flag when compiling.
Any help is appreciated.
Here is an excerpt from my code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//function prototypes
void * monitor_thread (void* data);
int main(int argc, char *argv[])
{
// exit program if no arguments are supplied
if(argc < 3)
{
printf("Not enough arguments passed \n");
return 0;
}
// parse arguments
int arrivalProbability = atoi(argv[1]);
int departureProbability = atoi(argv[2]);
// exit the program in entered values are not valid
if (arrivalProbability >=90 || departureProbability >=90)
{
printf("Argument(s) exceed a probability of 90%% \n");
return 0;
}
else if (arrivalProbability == 0 || departureProbability == 0)
{
printf("Argument(s)entered are zero or not intergers \n");
return 0;
}
// create the threads
int thr_id; /* thread ID for the newly created thread */
pthread_t p_thread; /* thread's structure */
int monitor = 1; /* monitor thread identifying number */
int arrival = 2; /* arrival thread identifying number */
int departure = 3; /* departure thread identifying number */
// monitor thead
// "undefined reference to pthread_create"
thr_id = pthread_create(&p_thread, NULL, monitor_thread, (void*)&monitor);
return 0;
}
void * monitor_thread (void* data)
{
int monitorThreadID = *((int*)data); /* thread identifying number */
printf("monitor thread");
sleep(1);
}
Right on the project icon on the left tree view and select settings -> common settings -> linker -> linker options
And add there -pthreads
Btw, pasting the build log could have help other people to help you faster.
Eran