Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can you include header file into another header file but when including the second header file in .c file, not automatically include the first one?

If I had a header file named "Header1.h" and another header file named "Header2.h", and include the first header file into the second, Header1 -> Header2, I would have access to Header1’s code from Header2 . (For example, if I had defined a struct inside Header1, I could now make variables of that struct type in Header2).

Now if I had a .c file named "Main.c" and if I include "Header2.h" in "Main.c", Header2 -> Main, I would be able to access Header2’s code from Main, but I would also be able to access Header1’s code from Main as if I have included "Header1.h" inside "Main.c" specifically as well, Header1 -> Main. (For example, I could make a variable of type struct [the one defined in "Header1.h"] inside "Main.c").

Is there a way for me to restrict such access to "Header1.h" from "Main.c" and allow only "Header2.h" to be able to access information from "Header2.h", and perform similar "privatization" of a header file?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Transcribing a comment into the question:

Header1.h:

void printLinkedListImpl(Node* head)
{
    while (head) {
        printf("%d ", head->data);
        head = head->next;
    }
    printf("\n");
}

Header2.h:

typedef struct {
    Node* head;
    int size;
    void (*printLinkedList)(Node*);
} LinkedList;

LinkedList* LinkedListConstructorImpl() {
    LinkedList* linkedList = (LinkedList*)malloc(sizeof(LinkedList));
    linkedList->head = NULL;
    linkedList->size = 0;
    linkedList->printLinkedList = &printLinkedListImpl;
}

Main.c:

void runner() {
    LinkedList* linkedList = LinkedListConstructorImpl();
}

>Solution :

I see two main options:

  1. Header2.h needs the contents of Header1.h, in which case you can’t sensibly want to omit the contents of Header1.h.
  2. Header2.h does not need the contents of Header1.h, in which case Header2.h should not include Header1.h.

Write the code using the headers according to which option applies.

You would need a good reason not to decide on one or the other of those two options. However, it might be possible to define the header guard for Header1.h before including Header2.h:

Header1.h

#ifndef HEADER1_H
#define HEADER1_H
…
#endif

Header2.h

#ifndef HEADER2_H
#define HEADER2_H

#include "Header1.h"

…

#endif

Main.c

…
#define HEADER1_H
#include "Header2.h"
…

But, as I stated at the outset, either Header2.h needs the information from Header1.h to compile (in which case this will cause a compilation failure), or it doesn’t (in which case Header2.h should not include Header1.h).

After transcribing the comment into the question:

Header files should not define general functions like it appears Header1.h does. A header should declare functions, types, enumerations, macros — and may perhaps define static inline functions — but should not define variables or functions. (See also How do I use extern to share variables between source files?). If you define functions in a header, the header can only be used by one source file, but the whole point of a header is to share information between source files.

If you’re not sure how to build programs from multiple source files, that is a different question — one which has been answered many times. For example, Compiling multiple C files in a program, and there are numerous questions and answers for different IDEs that can be found by searching on SO with [c] compile multiple files program.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading