简体   繁体   English

函数'pthread_mutex_init'的隐式声明在C99中无效

[英]Implicit declaration of function 'pthread_mutex_init' is invalid in C99

I am trying to lock a method with a mutec from this article here it states create a member variable of the class as such 我试图用这篇文章中的mutec锁定一个方法,它在这里声明创建一个类的成员变量

pthread_mutex_t mutex;

Then initialize it as such 然后将其初始化

 pthread_mutex_init(&mutex, NULL);

Then use it as such 然后就这样使用它

void MyLockingFunction()
{
    pthread_mutex_lock(&mutex);
    // Do work.
    pthread_mutex_unlock(&mutex);
}

I am getting the following warning at step 2 when I initialize it. 初始化时,我在步骤2收到以下警告。

Implicit declaration of function 'pthread_mutex_init' is invalid in C99

What does that mean ? 那是什么意思 ? Should I ignore it ? 我应该忽略它吗?

It means you have not included the header file which declares the function, so the compiler doesn't know anything about it at the point that you use it. 这意味着您没有包含声明该函数的头文件,因此编译器在您使用它时不知道任何关于它的信息。 You're trying to implicitly declare it by using it, which is invalid. 你试图通过使用它来隐式声明它,这是无效的。

If you check the man page for pthread_mutex_init() , it tells you that you should use the following line to import the declaration: 如果检查pthread_mutex_init()手册页 ,它会告诉您应该使用以下行来导入声明:

#include <pthread.h>

If you put that near the top of your source file, the warning will go away. 如果将其放在源文件的顶部附近,则警告将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM