2013-10-09 48 views
0

我在編譯我正在做的任務,並且遇到了錯誤,我不知道如何解決。 Esepcially這裏是我的命令行參數:關於使用gcc收到的編譯器錯誤感到困惑

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

的問題是,semaphor.h文件給我們,所以沒有什麼本質上錯了類,所以它不應該被抱怨那裏,我不認爲。我不知道如何協調結構錯誤。我不是特別精通C.這裏的錯誤列表:

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

良好的措施這裏是semaphore.h以及:

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

void down(semaphore *s); 
void up(semaphore *s); 
void createSem(semaphore *s, int value); 
+0

凡定義'st_cond_t'? –

+0

可能是'main.c'中包含的'st.h'庫的一部分我沒有發佈'main.c',因爲沒有錯誤引用它 – sreya

+0

考慮你的包含依賴項:main.C#includes st 。H; buffer.C#includes buffer.h,其中#包含semaphore.h。但沒有任何地方buffer.c包含st.h.因此,在鏈中的某處,buffer.c需要包含st.h.我建議你有semaphore.h #include st.h,因爲它是直接需要st.h的內容的文件 –

回答

1

好像semaphore.h中需要做到這一點:

#include <st.h> 

此外,要包括semaphore.h中兩次被混淆編譯器。

嘗試把這個在semaphore.h中還有:

#pragma once  

參見:http://en.wikipedia.org/wiki/Pragma_once

+0

是否可以同時使用#include「buffer.h」在我的main.c文件和我的''buffer.c'中,或者是不好的樣式? – sreya

+0

風格是有爭議的,但是'#pragma once'在這個頭文件中也是需要的。 – canhazbits

+0

如果您的main.c中的內容需要buffer.h的內容,那麼它需要以某種方式包含在內,無論是直接還是間接 –