What can I use besides firmware in a modern POSIX environment?

I'm new to C but am writing a small multi-threaded application. I want to introduce a delay to the stream. I used "usleep" and the behavior is what I want, but it generates warnings on C99.

implicit function declaration 'usleep

This is just a warning, but it bothers me. I got a Googled message for an answer, but all I could find was a loop / timer approach that would seem to be intense.

EDIT:

My included:

#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <time.h> 

And I call the compiler with:

 c99 program.c -Wall -pedantic -W -lpthread 

EDIT # 2:

I created a new file that contains:

 #include <unistd.h> int main(void) { usleep(10); } 

And I'm still getting a warning.

EDIT No. 3: As I said, I updated the question text.

+4
source share
5 answers

The problem is that you are using the standard C99 compiler, but you are trying to access the POSIX extensions. To expose POSIX extensions, you must, for example, define _POSIX_C_SOURCE to 200809L (for the current standard). For example, this program:

 #include <time.h> int main(void) { struct timespec reqtime; reqtime.tv_sec = 1; reqtime.tv_nsec = 500000000; nanosleep(&reqtime, NULL); } 

will compile correctly and wait 1.5 seconds (1 second + 500000000 nanoseconds) with the following compilation command:

 c99 main.c -D _POSIX_C_SOURCE=200809L 

The _POSIX_C_SOURCE macro must be defined with the appropriate value for the available POSIX extensions.

Also the -Wall, -pedantic and -W options are not defined for the POSIX c99 command, they are more like the gcc commands for me (if they work on your system, then this is fine, just keep in mind that they are not portable to other POSIX systems )

+5
source

You are probably in a modern POSIX system:

POSIX.1-2008 removes the usleep () specification.

On my system (linux) there is a detailed explanation of the macros that must be configured to return this function. But you should just follow the advice zvrba already gave, use nanosleep .

+4
source

On the manual page: This feature is deprecated. Use nanosleep instead.

+1
source

implicit function declaration 'usleep

This warning usually means that you are not #include right header file, in this case unistd.h .

0
source

Do you have #include <unistd.h> ?.

And you can use some of these methods: nanosleep () for nanoseconds and sleep () for a few seconds. Or another way might use clock (), but I think it is more CPU consuming.

0
source

All Articles