【问题标题】:error: incomplete type for struct sembuf错误:struct sembuf 的类型不完整
【发布时间】:2016-10-16 21:39:17
【问题描述】:

我正在编写一个程序,该程序使用信号量和信号量缓冲区在并行编程中实现互斥。这是导致错误的代码以及使用 sembuf 指针的信号和等待函数。

#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
#define NSEMS 1


// semaphore buffers
static struct sembuf OP = {0,-1,0};
static struct sembuf OV = {0,1,0};
struct sembuf *P =&OP;
struct sembuf *V =&OV;


// Wait() function for semaphore
int POP()
{   
int status;
status = semop(sem_id, P,1);
return status;
}

// Signal() function for semaphore
int VOP()
{   
int status;
status = semop(sem_id, V,1);
return status;

这是我收到的错误:

sem.c:17:15: error: variable ‘OP’ has initializer but incomplete type
static struct sembuf OP = {0,-1,0};
           ^
sem.c:17:15: warning: excess elements in struct initializer [enabled by   default]
sem.c:17:15: warning: (near initialization for ‘OP’) [enabled by default]
sem.c:17:15: warning: excess elements in struct initializer [enabled by default]
sem.c:17:15: warning: (near initialization for ‘OP’) [enabled by default]
sem.c:17:15: warning: excess elements in struct initializer [enabled by default]
sem.c:17:15: warning: (near initialization for ‘OP’) [enabled by default]

我收到 OV 结构的相同错误,但我不明白为什么。非常感谢任何帮助。

【问题讨论】:

  • 请添加struct sembuf的定义。
  • 请发帖minimal reproducible example。如果您不向我们提供完整的图片,我们无法真正评论可能丢失或可能不会丢失的内容。特别是我们需要查看您包含的标题。

标签: c struct semaphore


【解决方案1】:

您需要在使用类型之前包含必要的标题...

在你的情况下:

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

【讨论】:

  • 谢谢,我好像错过了第三个标题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多