c - ubuntu 14.04.1 x64 上 Codelite 6.1 中的 Pthreads

标签 c ubuntu pthreads codelite

我试图在带有 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/

相关文章:

c - 段错误/可变大小对象可能未初始化错误 C

javascript - 与 C 中的相同值相比,为什么 Javascript 的 Math.pow() 似乎返回一个舍入值?

C struct timeval timersub() 负值转正

linux -/usr/bin/ld : skipping incompatible/usr/lib/x86_64-linux-gnu/libQtCore. 所以在搜索 -lQtCore 时

c - 可以在调用 pthread_cond_wait() 之前检查互斥锁是否已锁定吗?

c++ - 使用 read() 读取空终止符

java - Ubuntu:如何让 apt-get/synaptic 为自己的应用程序工作

c++ - 获取输入并打印大量整数

根据调用线程的变量值关闭线程

c++ - 如何处理线程函数中的重复代码?