C代码 - Pthread不编译
我在编译我的代码时遇到问题,它使用互斥锁(所以使用pthread锁和条件)。我试过包括头文件,用-pthread或-lpthread编译,但我仍然收到错误。帮助将不胜感激。C代码 - Pthread不编译
这是误差输出:
的函数“的pthread_mutex_lock” [-Wimplicit函数声明] 的pthread_mutex_lock(&锁)隐式声明; //锁定 ^ /tmp/cchVS47i.o:在功能getMessage1': hw3.c:(.text+0x22): undefined reference to
的pthread_mutex_lock”。 hw3.c :(文本+为0x50):未定义参照Pthread_mutex_lock' /tmp/cchVS47i.o: In function
getMessage2' : hw3.c :(文本+ 0x13e):未定义参考`调用pthread_mutex_consistent_np” collect2:错误:LD返回1退出状态
,这里是我的代码的有关章节(编辑为清楚起见):
#define _GNU_SOURCE
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
char message[1001];
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
void *getMessage1()
{
Pthread_mutex_lock(&lock); //locked
....
}
int main(void)
{
pthread_t id1;
pthread_t id2;
pthread_create((&id1), NULL, getMessage1, NULL);
pthread_create((&id2), NULL, getMessage2, NULL);
...
return 0;
}
这是你在
有大写的PPthread_mutex_lock(&lock); //locked
在函数getmessage1()的开头。
您的编译器抱怨在编译阶段没有看到该函数的声明。它也在连接阶段抱怨。您正在包含所有正确的库,因为它不会抱怨任何其他正确输入的函数。
函数的正确名称是pthread_mutex_lock()。
尽管编译器对它们发出警告,但可以使用没有声明的函数。在更现代的C(99)版本中,这已被弃用。
谢谢,但我实际上仍然遇到错误。当我删除大写字母p并使用gcc -Wall -Wextra -pedantic -pthread hw3.c -lpthread 编译时,我得到:warning:隐式声明函数'thread_mutex_lock'[-Wimplicit-function-declaration] thread_mutex_lock(&lock); //锁定 /tmp/ccGZwoMv.o:在函数'getMessage2'中: (.text + 0x134):未定义引用'thread_mutex_lock' collect2:错误:ld返回1退出状态 – Selena
将其更改为小p,不要这个函数的正确名字是pthread_mutex_lock()。 –
是的,我的意思是我把它改成了一个小的p – Selena
你有没有给过库作为参数 –
你是什么意思的论据?我试过编译gcc -Wall -Wextra -pedantic -pthread hw3.c -lpthread – Selena
的变体,我以为你没有包括库 –