对pthread_create的未定义引用

Ram*_*uri 7 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;
}
Run Code Online (Sandbox Code Playgroud)

在编译器选项中我插入-lpthread
但是它说:
"未定义引用pthread_create"
我使用的是ubuntu 10.10,所以我已经安装了pthread库,我无法弄清楚这个错误的原因.

小智 29

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

例: gcc -o sample sample.c -lpthread

  • @RamyAlZuhouri:那不对.它更像是一个链接器设置.这样做:settings-> compiler and debugger-> linker settings(tab).选择"链接库"部分下的"添加".添加pthread库的路径(很可能是/usr/lib/libpthread.so).然后尝试建设 (3认同)
  • 注意,最好使用-pthread,因为-lplp将无法在仅安装了libpthread.a的系统上链接。 (2认同)

hmj*_*mjd 12

没有看到编译器命令,我怀疑-lpthread是不是结束了.库需要放在编译器命令的末尾:

gcc main.c -lpthread

然而,使用-pthread的代替-lpthread,如-pthread可添加其他设置(例如定义宏_REENTRANT例如).


Abd*_*ola 5

使用以下命令:

gcc -pthread -o main main.c