Связывание нескольких заголовочных файлов в C - PullRequest
2 голосов
/ 22 апреля 2019

Я работаю над проектом, который имеет 4 файла: main.c lists.c hash.c structs.c с соответствующими файлами .h.Проблема в том, что в lists.c мне нужно связать structs.h.И это дает мне ошибку, говоря, что функции в structs.c конфликтуют с объявлением этой функции в файле structs.h.

В lists.h я делаю #include "structs.h и в lists.cЯ делаю #include "lists.h" и не получаю никаких ошибок. В stucts.c я делаю #include "structs.h"

lists.h:

#include "structs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct list{
   Instr head;
   struct list *tail;
} *ILIST;

ILIST mkList(Instr, ILIST);

списков.c:

#include "lists.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

ILIST mkList(Instr n, ILIST l1) {
  ILIST l = malloc(sizeof(struct list));
  l->head = n;
  l->tail = l1;
  return l;
}

structs.h:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef enum  {START, QUIT, ATRIB, ADD, SUB, MUL, PRINT, READ, IF, GOTO, LABEL} OpKind;
typedef enum {INT_CONST, STRING, EMPTY} ElemKind;

typedef struct{
  ElemKind kind;
  union
  {
    int val;
    char* name;
  }content;
} Elem;

structs.c:

#include "structs.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

Я получаю такие ошибки

In file included from lists.h:1:0,
                 from main.c:3:
structs.h:5:16: error: redeclaration of enumerator ‘START’
 typedef enum  {START, QUIT, ATRIB, ADD, SUB, MUL, PRINT, READ, IF, GOTO, LABEL} OpKind;
                ^~~~~

In file included from main.c:1:0:
structs.h:5:16: note: previous definition of ‘START’ was here
 typedef enum  {START, QUIT, ATRIB, ADD, SUB, MUL, PRINT, READ, IF, GOTO, LABEL} OpKind;
                ^~~~~

1 Ответ

4 голосов
/ 22 апреля 2019

Заголовки

включают в себя только заголовки:


#ifndef __GUARD_STRUCTS__
#define__GUARD_STRUCTS__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef enum  {START, QUIT, ATRIB, ADD, SUB, MUL, PRINT, READ, IF, GOTO, LABEL} OpKind;
typedef enum {INT_CONST, STRING, EMPTY} ElemKind;

typedef struct{
  ElemKind kind;
  union
  {
    int val;
    char* name;
  }content;
} Elem;

#endif
...