Flower Flower - 1 month ago 12
C Question

Why do I get an incomplete type error?

I am writing a program that uses semaphores and semaphore buffers for mutual exclusion in parallel programming. Here is the bit of code that is causing the error as well as the signal and wait functions that use the sembuf pointers.

#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;


Here are the errors i receive:

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]


I receive the same error for the OV struct and I am not understanding why. Any help is greatly appreciated.

Answer

You need to include the necessary headers before using a type...

In your case:

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