3

I got a problem with my C-Code in Eclipse. To be specific my sleep-method does produce an error in the line where the timespec is stated. May you guys can tell me what I did wrong? Here's the code:

void sleep(double time) {
    nanosleep(
        (struct timespec[]) { {time,((time -((time_t)time)) * 1000000000)}},
        NULL);
}
lalibi
  • 3,057
  • 3
  • 33
  • 41

1 Answers1

5

You need to include the header file which defines the type timespec. Either:

  • You forgot to include the header file or
  • You merely forward declared the type.

Second seems the most likely cause of error. Since you are creating an array, the compiler needs to know the definition of timespec as it needs to allocate that much memory for the array.


The problem is that struct timespec and nanosleep() are not defined in the C standard. They are provided by POSIX standard. It seems you are compiling with -std=c99 or so which makes your compiler to strictly adhere to the C99 standard and hence report errors. To be able to compile these POSIX constructs you will have to explicitly enable them.

Compilation with std=c99

Compilation after enabling POSIX definitions:

#if __STDC_VERSION__ >= 199901L
# define _XOPEN_SOURCE 600
#else
# define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */

#include <time.h>

int main()
{
    double time = 0.1;

    nanosleep((struct timespec[]) { {time, ((time - ((time_t)time)) *
               1000000000)}}, NULL);

    return 0;
}  

__STDC_VERSION__ checks if the compiler you are using is c99 & depending on the compiler it enables the POSIX definitions.
_XOPEN_SOURCE defines which version of POSIX you want to refer to. Select the definition as per the POSIX version you use. 600 refers to POSIX 2004 while 500 refers to POSIX 1995.

pevik
  • 4,523
  • 3
  • 33
  • 44
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • mhhh....ok.....is it possible you show me a short example so that I understand what do you mean? – WolfgangRabenstein Jan 19 '13 at 14:47
  • @user1993028: Where is `struct timespec` defined? (possibly)in some header. Have you included that header file in your source(`.c`) file.I am putting it in as easy words as possible, it can't get any simpler than that.If you are trying to learn c through a forum its a bad idea.You need a [good book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to learn.Forums are to ask questions when you are stuck with specific problems(*coincidently you have in this case*), – Alok Save Jan 19 '13 at 14:52
  • @user1993028: We can help you with your specific problem but you need to understand the language atleast enough to decipher what we suggest you. – Alok Save Jan 19 '13 at 14:54
  • My struct timespec is nowhere defined, because normally it could be used in the nanosleep function as far as I known from the websites. However my header looks like this: `#include #include #include #include #include #include #ifdef TEST #define runLoop for(int i=0; i<=9; i++) #else #define runLoop while(true) #endif` – WolfgangRabenstein Jan 19 '13 at 14:54
  • Yes I'm trying to call nanosleep but what I want to do is to let this operations be done. However I'm stuck into it because when I build the workspace I got the error "array type has incomplete element type" as I wrote in the subject – WolfgangRabenstein Jan 19 '13 at 15:04
  • @WolfgangPansi: Updated the answer with the root cause and the solution.Hth. – Alok Save Jan 19 '13 at 15:41
  • 1
    Thank you very much for your help. Yes I've forgot to tell that I'm using -std=c99 and now everything is working fine. – WolfgangRabenstein Jan 19 '13 at 15:51
  • @WolfgangPansi: Good for you :) – Alok Save Jan 19 '13 at 15:55
  • The new standard C11 has in fact `struct timespec` and a function called `thrd_sleep` that has the same interface and semantics as POSIX' `nanosleep`. – Jens Gustedt Jan 19 '13 at 16:44
  • `struct timespec` is defined in [``](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html#tag_13_76) when you are working with POSIX definitions enabled, as you would discover if you looked at the definition of [`nanosleep()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html). Note that you are treading on very thin ice by redefining [`sleep()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sleep.html) which is declared by ``. Choose a different name for your `sleep()` function (`double_sleep()`, perhaps, or `dsleep()`?). – Jonathan Leffler Jan 19 '13 at 19:00