0

if I have two header files

a.h and b.h

can I include "a.h" in b.h

and also include "b.h" in "a.h" ?

2
  • It's normally best to have a dependency in only one direction. Commented May 16, 2021 at 16:42
  • 1
    You could try to add #pragma once Commented May 16, 2021 at 17:45

2 Answers 2

1

You can, but it's not a very good idea. If you really must, you can prevent recursion with the use of include guards (which are a good idea regardless).

In a.h:

#ifndef A_H #define A_H #include "b.h" #endif 

and b.h

#ifndef B_H #define B_H #include "a.h" #endif 
Sign up to request clarification or add additional context in comments.

Comments

0

No that won't work. The preprocessor just replaces your #include"xyz.h" with the actual file, so this would end in an endless recursion.

1 Comment

If the header files are guarded, as they normally are, then it would not. The real problem is making the dependencies seen by the C compiler work in either direction.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.