-3

I am struggling with recursively cpp header including problem. To solve this problem, I have to draw the including path by hand to see where recursively including happen.

I wonder whether there are some good ways to solve this problem like a recursively including detector or a header include path painter, not a good coding style to prevent this problem(I know this is more important).

yixing
  • 154
  • 6
  • 6
    i think you may mean "cyclical" not "recursive", and if so the answer is forward declarations – Neil Butterworth Oct 18 '22 at 13:37
  • 1
    Are you talking about this: [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Oct 18 '22 at 13:37
  • Please show a [mre], and decide which language you use; C and C++ are different. BTW, code styles help avoid such problems... – the busybee Oct 18 '22 at 14:24
  • 5
    **Header Guards** and **Forward Declarations** are designed to solve issues related to circular includes. – Remy Lebeau Oct 18 '22 at 14:58
  • And perhaps look at `#pragma once`? – cs1349459 Oct 18 '22 at 16:02
  • So you are asking for a *tool* that may help in preventing the issue and to graph the dependencies, rather then a solution like the proposed dupe. Have you already read https://stackoverflow.com/questions/42308/tool-to-track-include-dependencies or https://stackoverflow.com/questions/7904651/how-to-generate-program-dependence-graph-for-c-program ? – Bob__ Oct 21 '22 at 18:03

1 Answers1

1

Put #pragma once in the beginning of each header file, this will ensure that the header file is only included once.

quesswho
  • 19
  • 4
  • 3
    Per [Wikipedia](https://en.wikipedia.org/wiki/Pragma_once): "In the C and C++ programming languages, `#pragma once` is a **non-standard**...". The portable way is to use [header guards](https://en.wikipedia.org/wiki/Include_guard). – Andrew Henle Oct 21 '22 at 17:59