Search code examples
clinuxdelay

fatal error: linux/delay.h: No such file or directory


The function mdelay() is used in my program, but when compiling, GCC gave me an error:

fatal error: linux/delay.h: No such file or directory

I tried to solve this problem, but nothing worked.

What I've tried:

  • copy the file, delay.h, into the folder /usr/linux, but it'll show up another header file (asm/delay.h) missing.

  • copy the file, delay.h (different from the above), into the folder /usr/asm, but it'll show up many header files missing.

How to solve this problem?

Envir: Ubuntu 12.04 LTS


Solution

  • As shown in the Linux Cross Reference, mdelay() is a macro within the linux kernel source code.

    #ifndef mdelay
    #define mdelay(n) (\
            (__builtin_constant_p(n) && (n)<=MAX_UDELAY_MS) ? udelay((n)*1000) : \
            ({unsigned long __ms=(n); while (__ms--) udelay(1000);}))
    #endif
    

    Thus, you cannot simply use it. But there exist multiple alternatives, e.g.:

    • sleep() in unistd.h for sleeping N seconds.
    • usleep() in unistd.h for sleeping U microseconds
    • nanosleep() in time.h for making a thread sleep some nanoseconds.

    As I understand, you are searching for a function to specify a microseconds sleep interval. Thus, use usleep(). Note however that the function is marked as deprecated with POSIX.1-2001. The manpage for usleep() advices to use nanosleep() instead.