【问题标题】:How can i fix errors E0003,C1014 that occurred when i try to include some header files simultaneously?当我尝试同时包含一些头文件时,如何修复错误 E0003、C1014?
【发布时间】:2021-12-30 09:15:55
【问题描述】:

话虽如此,我已经尝试解决网上冲浪的问题,并且我发现了一些与我的问题非常相似的问题,但尽管如此,我没有找到任何解决方案。 我想知道是否有人可以帮助我解决这个问题,解释什么不起作用,而不是将我重定向到另一个博客。

代码如下:

1)文件:list.h

#include "list.c"
#include "element.h"


typedef struct list_element {
    element value;
    struct list_element* next;
} item;
typedef item* list;


list emptyList(void);
boolean empty(list);
element head(list);
list tail(list);
list cons(element, list);
.......
  1. 文件列表.c
    #include "list.h"
    #include <stdio.h>
    #include <stdlib.h>

    list emptyList(void) {
        return NULL;
    }

    boolean empty(list l) {
        if (l == NULL)
            return true; 
        else
            return false;
    }

    list tail(list l) {
        if (empty(l))
            return NULL;
        else
            return l->next;
    }
....

3)文件元素.h

#include "element.c"

#ifndef ELEMENT_H
#define ELEMENT_H

typedef int element;
typedef enum { false, true } boolean;

boolean isLess(element, element);
boolean isEqual(element, element);
element getElement(void);
void printElement(element);
  1. 文件 element.c
#include "element.h"
#include <stdio.h>

boolean isEqual(element e1, element e2) {
    return (e1 == e2); 
}

boolean isLess(element e1, element e2) {
    return (e1 < e2);
}

element getElement() {
    element el;
    scanf(" %d", &el);
    return el;
}

void printElement(element el) {
    printf(" % d", el);
}

然后编译器给了我错误代码:

 E0003(file #include /../../../../../../element.h includes itself)

和错误

code C1014 (too many file of inclusion, depth=1024) for file list.c and element.c```
So I've tried to use the guards (surely in the wrong way) and the error list was almost the same.
I would be grateful if someone could help me out
thank you for the attention.

【问题讨论】:

  • 从不包含 .c 文件
  • 永远不要包含.c 文件。 (好吧,直到你开始认为自己是一个完全了解后果的高级程序员)
  • @EugeneSh。如果我不将它们包含在 header.h 中,我如何使用 list.c 和 element.c 的函数
  • @ric - 您不要在 .h 文件中使用它们。不要在 .h 文件中添加任何代码或数据
  • 有一个非常简单的方法来考虑#include。它只是获取文件并粘贴其内容。如果你这样想,你就会避免这样的混乱。因此,您可以将您的项目视为仅包含 .c 文件并粘贴了标题。

标签: c list header include guard


【解决方案1】:

在您的程序中,您包含 .h 文件,其中包含 .h 文件,其中包含 .c 文件......直到您达到编译器包含深度级别,在您的情况下为 1024。

删除

#include "element.c"

并删除

#include "list.c"

*.h 文件(头文件)不应包含任何数据或函数定义。这些文件应该只有类型和外部数据声明、宏定义、函数原型和(当你更高级时)static inline 函数定义

【讨论】:

  • 如果我删除它们,链接器如何找到 list.c 和 element.c?
  • @ric 链接器不关心您的 .h 文件 您需要告诉链接器您要将哪些目标文件链接在一起。至于您的关卡,请使用 Eclipse 或其他会自动为您完成的 IDE。目前不要让自己超出你的知识水平。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-05
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 2021-10-17
相关资源
最近更新 更多