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);
}
You need to include the header file which defines the type
timespec. Either:Second seems the most likely cause of error. Since you are creating an array, the compiler needs to know the definition of
timespecas it needs to allocate that much memory for the array.The problem is that
struct timespecandnanosleep()are not defined in the C standard. They are provided by POSIX standard. It seems you are compiling with-std=c99or 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=c99Compilation after enabling POSIX definitions:
__STDC_VERSION__checks if the compiler you are using is c99 & depending on the compiler it enables the POSIX definitions._XOPEN_SOURCEdefines which version of POSIX you want to refer to. Select the definition as per the POSIX version you use.600refers toPOSIX 2004while500refers toPOSIX 1995.