我试图用codelite在Ubuntu中用Pthreads编译一个简单的C程序。
我选择了 GCC 作为我的编译器,并在项目设置的 C 编译器选项中添加了“-pthread”。但它会在编译时引发错误"undefined reference to pthread_create"
。
如果我通过命令行手动调用 GCC,它会构建并运行。
但是,当我注释掉有问题的行时,它在 codelite 中编译时没有错误。
所以我怀疑codelite在编译时没有添加-pthread编译器标志。
任何帮助表示赞赏。
这是我的代码的摘录:
#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);
}