c - 对 pthread_create 的 undefined reference

标签 c pthreads

我有这个代码:

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

void* cuoco(void* arg)
{
    fprintf(stderr,"Inizio codice cuoco\n");
    fprintf(stderr,"Fine codice cuoco\n");
    return NULL;
}

void* cameriere(void* arg)
{
    fprintf(stderr,"Inizio codice cameriere\n");
    fprintf(stderr,"Fine codice cameriere\n");
    return NULL;
}

void* cliente(void* arg)
{
    fprintf(stderr,"Inizio codice cliente\n");
    fprintf(stderr,"Fine codice cliente\n");
    return NULL;
}

int main(int argc, char* argv[])
{
    void* (*routine)(void*);
    routine=cuoco;
    pthread_t thread_cuoco,thread_cameriere,thread_cliente;
    pthread_create(&thread_cuoco,NULL,routine,NULL);
    return 0;
}

然后在编译器选项中我插入 -lpthread
但它说:
“对 pthread_create 的 undefined reference ”
我使用的是ubuntu 10.10,所以我已经安装了pthread库,我不知道这个错误的原因。

最佳答案

使用 -lpthread 作为最后一个编译器标志。

例子: gcc -o 示例 sample.c -lpthread

关于c - 对 pthread_create 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803145/

相关文章:

c - 使用共享内存平铺技术在 CUDA 中进行矩阵相乘

c - 每次都在 C 中工作最后一个消费者(生产者消费者问题)

c - 如何在另一个线程完成后执行另一个线程?

Linux, pthreads -- 发送停止条件

c - 使用 sleep 时的 pthread 调度问题

c++ - C/C++ 中 Memcopy 的性能成本

c - char[] 如何表示 UTF-8 字符串?

c - linux内核哈希表实现中双指针的使用

c - 无法使用通过 LD_PRELOAD 加载的动态库中的函数

创建 1 个输出 "new thread"任意次数的线程