Question

I'm trying to compile this assignment I'm doing and I'm running into errors I'm not sure how to fix. Esepcially for Here's my command line argument:

gcc HW3.c semaphore.o buffer.c -L. -lst -o test

The problem is that the semaphor.h file was given to us so there isn't anything inherently wrong in the class so it shouldn't be complaining there I don't think. I'm not sure how to reconcile the struct errors either. I'm not particularly proficient in C. Here's the error list:

    In file included from buffer.c:11:
semaphore.h:4: error: expected specifier-qualifier-list before ‘st_cond_t’
In file included from buffer.h:2,
                 from buffer.c:12:
semaphore.h:4: error: expected specifier-qualifier-list before ‘st_cond_t’
semaphore.h:5: error: conflicting types for ‘semaphore’
semaphore.h:5: note: previous declaration of ‘semaphore’ was here
semaphore.h:7: error: conflicting types for ‘down’
semaphore.h:7: note: previous declaration of ‘down’ was here
semaphore.h:8: error: conflicting types for ‘up’
semaphore.h:8: note: previous declaration of ‘up’ was here
semaphore.h:9: error: conflicting types for ‘createSem’
semaphore.h:9: note: previous declaration of ‘createSem’ was here
buffer.c: In function ‘init_buffer’:
buffer.c:20: error: incompatible type for argument 1 of ‘createSem’
semaphore.h:9: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’
buffer.c:21: error: incompatible types when assigning to type ‘semaphore’ from type ‘void *’
buffer.c:23: error: incompatible type for argument 1 of ‘createSem’
semaphore.h:9: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’
buffer.c:24: error: incompatible types when assigning to type ‘semaphore’ from type ‘void *’
buffer.c: In function ‘c_deposit’:
buffer.c:38: error: incompatible type for argument 1 of ‘down’
semaphore.h:7: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’
buffer.c:41: error: incompatible type for argument 1 of ‘up’
semaphore.h:8: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’
buffer.c: In function ‘c_remove’:
buffer.c:46: error: incompatible type for argument 1 of ‘down’
semaphore.h:7: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’
buffer.c:49: error: incompatible type for argument 1 of ‘up’
semaphore.h:8: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’

buffer.c:

#include <stdio.h>
#include <stdlib.h>
#include "semaphore.h"
#include "buffer.h"

buffer *init_buffer(int size)
{

    buffer *new_Buffer;
    new_Buffer=malloc((sizeof(buffer)));

    createSem(new_Buffer->emptyBuffer, size);
    new_Buffer->emptyBuffer=malloc(sizeof(semaphore));

    createSem(new_Buffer->fullBuffer, 0);
    new_Buffer->fullBuffer=malloc(sizeof(semaphore));

    new_Buffer->chars=malloc(sizeof(char)*size);

    new_Buffer->size=size;

    new_Buffer->nextIn=0;
    new_Buffer->nextOut=0;

    return new_Buffer;
}

void c_deposit(buffer *buffer, char c)
{
    down(buffer->emptyBuffer);
    buffer->chars[buffer->nextIn]=c;
    buffer->nextIn=(buffer->nextIn+1)%buffer->size;
    up(buffer->fullBuffer);
}
int c_remove(buffer *buffer)
{
    int c;
    down(buffer->fullBuffer);
    c=buffer->chars[buffer->nextOut];
    buffer->nextOut=(buffer->nextOut+1)%buffer->size;
    up(buffer->emptyBuffer);
    return c;
}

buffer.h:

#include <stdio.h>
#include "semaphore.h"

typedef struct{
    semaphore emptyBuffer;
    semaphore fullBuffer;
    int nextIn;
    int nextOut;
    int size;
    char *chars;
}buffer;

void c_deposit(buffer *buffer, char c);
int c_remove(buffer *buffer);

buffer *init_buffer(int size);

For good measure here's semaphore.h as well:

typedef struct
{
  int value;
  st_cond_t sem_queue;
} semaphore;

void down(semaphore *s);
void up(semaphore *s);
void createSem(semaphore *s, int value);
Was it helpful?

Solution

Seems like semaphore.h needs to do this:

#include <st.h>

Also, you are including semaphore.h twice which is confusing the compiler.

Try putting this in semaphore.h as well:

#pragma once    

See: http://en.wikipedia.org/wiki/Pragma_once

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top