Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implicit declaration of function usleep

Tags:

c

usleep

gcc (GCC) 4.6.3 c89 

I am trying to use usleep. However, I keep getting the following warning:

implicit declaration of function usleep

I have included the unistd.h header file.

The man pages mentions something about this. But I am not sure I understand by it:

usleep():    Since glibc 2.12:        _BSD_SOURCE ||            (_XOPEN_SOURCE >= 500 ||                _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) &&            !(_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)    Before glibc 2.12:        _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED 

But not sure what I a to do with the above?

like image 765
ant2009 Avatar asked Apr 07 '12 10:04

ant2009


People also ask

What is implicit declaration of function?

If a name appears in a program and is not explicitly declared, it is implicitly declared. The scope of an implicit declaration is determined as if the name were declared in a DECLARE statement immediately following the PROCEDURE statement of the external procedure in which the name is used.

What does Usleep in C do?

The function usleep() is a C API that suspends the current process for the number of microseconds passed to it. It can be used for delaying a job.


2 Answers

That list is the pre-conditions for having usleep defined. It's basically a C-like expression involving #define variables which has to be true before including the header file.

The header file itself will only define usleep inside what is usually a massive nest of #ifdef statements and the developers have taken the time to tell you what you need to do so that you don't have to spend hours trying to figure it out yourself :-)

Assuming you're using a glibc 2.12 or better, it means you either have to:

  • declare _BSD_SOURCE; or
  • declare a complicated combination of three other things, which I won't bother to decode.

Probably the easiest fix is to simply compile with gcc -D _BSD_SOURCE or put:

#define _BSD_SOURCE 

in the code before you include the header file that gives you usleep.

You'll probably want to define these before any includes in case there are dependencies between the various header files.

like image 118
paxdiablo Avatar answered Sep 23 '22 22:09

paxdiablo


This may work: Add -std=gnu99 when compiling with gcc on Linux.

Example:

arm-linux-gcc -lpthread -std=gnu99  -o test ArmLinuxDataPipe1.2.1.c 
like image 30
ioilala Avatar answered Sep 21 '22 22:09

ioilala