对pthread_create的未定义引用

对pthread_create的未定义引用,c,pthreads,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

我有以下代码:

#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;
}
#包括
#包括
void*cuoco(void*arg)
{
fprintf(标准,“日化密码库科”);
fprintf(标准,“细尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾;
返回NULL;
}
void*cameriere(void*arg)
{
fprintf(标准,“内部化密码\n”);
fprintf(标准,“细尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾尾);
返回NULL;
}
void*客户(void*arg)
{
fprintf(标准,“客户信息化”;
fprintf(标准,“优质codice客户”);
返回NULL;
}
int main(int argc,char*argv[])
{
无效*(*例行)(无效*);
常规=库科;
pthread_t thread_cuoco、thread_camerie、thread_cliente;
pthread_create(&thread_cuoco,NULL,routine,NULL);
返回0;
}
在编译器选项中插入-lpthread
但是上面写着:
“对pthread_create的未定义引用”

我使用Ubuntu10.10,所以我已经安装了pthread库,我无法找出这个错误的原因。

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

例如:
gcc-o sample sample.c-lpthread
如果没有看到编译器命令,我怀疑
-lpthread
没有结束。库需要放在编译器命令的末尾:

gcc main.c-lpthread


但是,请使用
-pthread
而不是
-lpthread
,因为
-pthread
可能会添加其他设置(例如定义宏
\u可重入
)。

使用以下命令:

gcc-pthread-omain.c
找到了解决方案:D 只需转到
设置>>编译器>>链接器选项卡>>添加库

转到drive并转到lib文件夹,找到
x86\u 64\u linux\u gnu
并找到
pthread

享受:)

在Eclipse中,您应该添加字符串pthread

Project->Properties->C/C++构建->设置->工具设置->GCC链接器->库->库(-l)->添加->pthread


在此之后,您可以构建您的项目。

向我们展示您是如何编译程序的。您是如何添加链接器选项的&where?代码::Blocks的哪个版本?可能与我用代码::Blocks编译它的版本相同,在C::B选项中,现在我用-pthread更改了-lpthread,但没有任何更改。这是我在设置中仅有的选项。我不熟悉code::blocks,但你能尝试完全重建源代码吗?@RamyAlZuhouri不,你不能。你可能把代码块设置搞砸了+1来对抗错误的否决票。是的,我把它放在了设置->编译器和调试器->(编译器设置选项卡)->其他选项上。@RamyAlZuhouri:那不对。它更像是一个链接器设置。这样做:设置->编译器和调试器->链接器设置(选项卡)。在“链接库”部分下选择“添加”。将路径添加到pthread库(很可能是/usr/lib/libpthread.so)。试着构建然后,如果你尝试了,你不应该投否决票,但是它不起作用,但是你也没有告诉我们……注意,
-pthread
的使用更可取,因为
-lpthread
将无法链接到只安装了
libpthread.a
的系统。但是OP在评论中说他使用的是code::Blocks,不是日食。