1

How would I go about linking 2 header files that depend on each other with their c files?

For instance I have a file stack.h that depends on a struct declared in linkedlist.h, and the file "stack.c" calls on functions from linkedlist.c which depend on both header files. main.c depends on both header files

linkedlist.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;
alk
  • 69,737
  • 10
  • 105
  • 255
FreeStyle4
  • 272
  • 6
  • 17
  • 1
    I'm not convinced `linkedlist.h` needs *anything* from `stack.h`, based solely on what you've shown here. – WhozCraig May 31 '16 at 03:39
  • I declare listNode *list. Maybe I should just typedef struct it there too instead of trying to include linkedlist.h – FreeStyle4 May 31 '16 at 03:40
  • 2
    As I said, nothing in `linkedlist.h` appears to refer to, nor require, *anything* from `stack.h`. The opposite is obviously not the case; `stack.h` clearly needs `linkedlist.h` included. But from what you've posted, `#include "stack.h"` in `linkedlist.h` is completely pointless. – WhozCraig May 31 '16 at 03:41
  • Oh sorry, just reread your comment, I have a function called printContent which takes in a stack as a parameter in linkedlist.h void printContent(stack *userStack); – FreeStyle4 May 31 '16 at 03:42
  • That would be why the instruction for posting a [**minimal, *complete*, and verifiable example**](https://stackoverflow.com/help/mcve) are so important to follow. Otherwise it wastes time and effort. Regardless, if `printContent` requires a `stack` address as a parameter, either declare the prototype in `stack.h` or at-least forward declare the `stack` type in `linkedlist.h`, the latter option being the *least* preferred. – WhozCraig May 31 '16 at 03:46
  • If `printContent` prints content of a `stack`, why is it exported by `linkedlist.h`? – LPs May 31 '16 at 06:25
  • OT: header files aren't "*linked*" but "included" by the pre-processor prior to compilation Linking is something different done by the linker after compilation. – alk May 31 '16 at 08:51

1 Answers1

1

How would I go about linking 2 header files that depend on each other

Don't. You should never have such a scenario or your program design is broken.

For your specific example, it appears that you try to implement a stack ADT by using a linked list. If so the stack should include the linked list ADT and that's it.

Lundin
  • 195,001
  • 40
  • 254
  • 396