在C中链接多个头文件

雨果·迪亚斯(Hugo Dias)

我正在一个有4个文件的项目中:main.c list.c hash.c structs.c和相应的.h文件。问题是在list.c中,我需要链接structs.h。这给我一个错误,说structs.c中的函数与structs.h文件中的该函数的声明冲突。

在lists.h中,我执行#include“ structs.h”;在lists.c中,我执行#include“ lists.h”,但没有得到任何错误。在stucts.c中,我执行#include“ structs.h”

清单.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);

list.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;
                ^~~~~
1v3分钟

标头护卫

只包括标题警卫:


#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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在C ++中将多个头文件导出为单个头文件?

多个类在多个头文件中相互引用

为什么多个头文件和源文件中的 C++ 继承不起作用?

如何使用在多个头文件中声明的函数

如何在另一个头文件中包含c头文件?

如何在 C 编程中将两个头文件和 3 个 c 文件链接到一个可执行文件中?

C++ 友元函数不起作用(有多个头文件和源文件)

单个头文件中的命名空间和类及其方法定义如何在多个头文件或cpp文件中使用呢?

您可以在一个库中实现多个头文件吗?

为什么 C++ 标准将容器类拆分为多个头文件?

AWK - 将多个头文件拆分为单独的文件?

C++ - 在另一个头文件中包含一个头文件以使全局变量可用于数组声明

在C ++中使用另一个头文件中的抽象类

C++ 在两个头文件中包含一个类

两个头文件中定义的相同类型

您可以为DLL创建多个头文件吗?您可以合并头文件吗?

来自第二个头文件中的一个头文件的Typedef用法

在另一个头文件中包括头文件中的typedef枚举

C头文件包含在另一个头文件中,并且都包含在ac文件中

在 C 或 C++ 中,如何防止头文件中先前的 #define 影响以后包含的另一个头文件?

Makefile | 对头文件中包含的另一个头文件的依赖

如何在 C 中包含另一个头文件中定义的结构数组?

在 VS 2017 中为“便携式”项目包含一个头文件(C++)

C编程如何在另一个头文件中打印出结构的元素?

为什么在/ usr / include中有多个头文件副本?

从多个头文件的接口中声明重写的函数时,如何避免重复的代码?

在C ++中链接结构和函数而无需头文件

为什么我不能将头文件导入一个头文件中的库中?

我必须包含哪个头文件才能在内核源文件中获取printk()?