【问题标题】:Linking multiple header files to c files将多个头文件链接到c文件
【发布时间】:2016-09-28 21:52:59
【问题描述】:

如何将两个相互依赖的头文件与它们的 c 文件链接起来?

例如,我有一个文件stack.h,它依赖于linkedlist.h 中声明的结构,文件“stack.c”调用来自linkedlist.c 的函数,这些函数依赖于两个头文件。 main.c 依赖于两个头文件

链表.h

#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
struct listNode
{
    int nodeValue;
    struct listNode * next;
};

typedef struct listNode listNode;

stack.h

 #include "linkedList.h"
    typedef struct stack {
        listNode *list;
    }stack;

【问题讨论】:

  • 我不相信 linkedlist.h 需要来自 stack.h任何东西,仅基于您在此处显示的内容。
  • 我声明 listNode *list。也许我也应该在那里 typedef struct 它,而不是尝试包含linkedlist.h
  • 正如我所说,linkedlist.h 中的任何内容似乎都没有引用或要求来自stack.h任何内容。相反显然不是这样; stack.h 显然需要 linkedlist.h 包括在内。但是从您发布的内容来看,linkedlist.h 中的#include "stack.h" 完全没有意义。
  • 哦,抱歉,请重新阅读您的评论,我有一个名为 printContent 的函数,它在linkedlist.h void printContent(stack *userStack); 中接受一个堆栈作为参数
  • 这就是为什么发布minimal, complete, and verifiable example 的说明如此重要的原因。否则会浪费时间和精力。无论如何,如果printContent 需要stack 地址作为参数,要么在stack.h 中声明原型,要么在linkedlist.h 中至少向前声明stack 类型,后一个选项是least 首选。

标签: c header definition cyclic-dependency


【解决方案1】:

我将如何链接两个相互依赖的头文件

不要。您永远不应该遇到这样的情况,否则您的程序设计会被破坏。

对于您的具体示例,您似乎尝试使用链表来实现堆栈 ADT。如果是这样,堆栈应该包括链表 ADT,就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多