无法在另一个结构中包含timespec

时间:2017-08-07 03:06:53

标签: c struct

我已经阅读了很多这方面的帖子,我可以告诉你(我读过的每个帖子都做了这套假设,所以让我们尽早把它弄出来):

  • 我已适当地包括time.h
  • 我已经指定了POSIX常量和-std = gnu99

代码:

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

#include <linux/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#include <string.h>
#include <stdlib.h>
#include <time.h>
#define uint unsigned int

struct KEYDATA 
{
   struct timestruct duration; 
} ;

// output/display function
int main(void) 
{
   struct KEYDATA keyData[20]; 

   keyData.duration.tv_nsec = 999;   

   return 0;
}

在编译时:

pi@raspberrypi:~/src/midi-timing $ gcc tmp.c -O2 -Wall -pedantic -o tmp -std=gnu99 -lrt
tmp.c:19:22: error: field ‘duration’ has incomplete type
    struct timestruct duration; 
                      ^
tmp.c: In function ‘main’:
tmp.c:27:11: error: request for member ‘duration’ in something not a structure or union
    keyData.duration.tv_nsec = 999;   
           ^
tmp.c:25:19: warning: variable ‘keyData’ set but not used [-Wunused-but-set-variable]
    struct KEYDATA keyData[20]; 
                   ^
pi@raspberrypi:~/src/midi-timing $

我会承认我的C编程有点生疏,但我必须在这里看到一些我没看到的东西。如果您看到错误,请告诉我。感谢。

2 个答案:

答案 0 :(得分:1)

您已将duration的类型标识为struct timestruct而非struct timespec。简单地解决这个拼写错误,我相信你应该没事。

答案 1 :(得分:1)

您需要将timestruct替换为timespec

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

#include <linux/soundcard.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

#include <string.h>
#include <stdlib.h>
#include <time.h>
#define uint unsigned int

struct KEYDATA 
{
       //struct timestruct duration; 
      struct timespec duration;
} ;

// output/display function
int main(void) 
{
   struct KEYDATA keyData[20]; 

   //keyData.duration.tv_nsec = 999; 
   keyData->duration.tv_nsec = 999;   

   return 0;
}