【Linux】error: implicit declaration of function ‘sleep’ [-Werror=implicit-function-declaration]

This error message is because sleepthe function , but unistd.hthe header file is not included, so the compiler cannot recognize the declaration of sleepthe function .

sleepfunction is a standard library function used to cause the program to pause for a period of time. Its declaration is in the unistd.hheader file. Therefore, if you want to use sleepthe function , you need to include unistd.hthe header file in your code. For example:

#include <unistd.h>

int main() {
    
    
    sleep(1);
    return 0;
}

In this example, we include unistd.hthe header so we can use sleepthe function.

It should be noted that if -Werrorthe option , the compiler will treat all warnings as errors, resulting in compilation failure. If you don't want this warning to be an error, you can use -Wno-error=implicit-function-declarationthe option to disable this warning. For example:

gcc -o program program.c -Wno-error=implicit-function-declaration

In this way, compilation will not fail even if implicit declaration of function ‘sleep’there warnings.

Guess you like

Origin blog.csdn.net/qq_37286579/article/details/130596262