我试图在带有 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);
}
最佳答案
在左侧 TreeView 中的项目图标上,然后选择设置 -> 通用设置 -> 链接器 -> 链接器选项
然后添加 -pthreads
顺便说一句,粘贴构建日志可以帮助其他人更快地帮助您。
艾兰
关于c - ubuntu 14.04.1 x64 上 Codelite 6.1 中的 Pthreads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25484912/