【问题标题】:Confused about compiler errors received using gcc对使用 gcc 收到的编译器错误感到困惑
【发布时间】:2013-10-09 03:49:55
【问题描述】:

我正在尝试编译我正在做的这个作业,但我遇到了我不知道如何修复的错误。尤其是我的命令行参数:

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

【问题讨论】:

  • st_cond_t 定义在哪里?
  • 可能是我的main.c 中包含的st.h 库的一部分我没有发布main.c,因为没有任何错误引用它
  • 考虑你的包含依赖:main.c #includes st.h; buffer.c #includes buffer.h,其中#includes semaphore.h。但是 buffer.c 没有任何地方包含 st.h。所以在链的某个地方,buffer.c 需要包含 st.h。我建议您使用 semaphore.h #include st.h,因为它是直接需要 st.h 内容的文件
  • semaphore.h 的内容也被有效地#included 两次,因此之前的声明警告。好的做法是将您的标头设置为once-only headers,这样它们就可以被包含多次而不会出现问题
  • 好的,谢谢!不幸的是,将 st.h 添加到 semaphore.h 并没有帮助

标签: c gcc gcc-warning


【解决方案1】:

似乎 semaphore.h 需要这样做:

#include <st.h>

另外,您将 semaphore.h 包含了两次,这让编译器感到困惑。

尝试将其也放入 semaphore.h:

#pragma once    

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

【讨论】:

  • 在我的main.c 文件和我的buffer.c 中都有#include "buffer.h" 是否可以,或者是不好的风格?
  • 样式值得商榷,但该头文件中也需要#pragma once
  • 如果你的 main.c 中的内容需要 buffer.h 的内容,那么无论是直接还是间接,都需要以某种方式包含它
猜你喜欢
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 2013-05-01
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
相关资源
最近更新 更多