在C中链接多个头文件
Linking multiple header files in C
我正在处理一个包含 4 个文件的项目:main.c lists.c hash.c structs.c 以及相应的 .h 文件。
问题是在 lists.c 中我需要 link structs.h。
它给我一个错误,说 structs.c 中的函数与 structs.h 文件中该函数的声明冲突。
在lists.h中我做#include "structs.h and in lists.c I do #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);
lists.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;
^~~~~
只包括像这样的头球后卫:
#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
我正在处理一个包含 4 个文件的项目:main.c lists.c hash.c structs.c 以及相应的 .h 文件。 问题是在 lists.c 中我需要 link structs.h。 它给我一个错误,说 structs.c 中的函数与 structs.h 文件中该函数的声明冲突。
在lists.h中我做#include "structs.h and in lists.c I do #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);
lists.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;
^~~~~
只包括像这样的头球后卫:
#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