Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idea for resolving implicit includes in C, C++

Tags:

c++

c

I've just came up with an idea to solve a problem and wanted to share it. Sorry if too banal.

So, a big C++ project I am reviewing, contains many includes which relies on symbols from other includes but which don't include the required includes. Any slight change in build-procedure results in "missing symbol" failures.

So in order to check all includes for self-containment at once, I search for all *.h, create on the fly a cpp-file which only contains #include -statement with this .h file and try to compile it. At the end I obtain a list of "good" and "bad" include files. Cool isn't it :-) Or is there an easier solution to do it?

like image 774
Valentin H Avatar asked Nov 19 '10 11:11

Valentin H


2 Answers

If every .cpp file includes it's own header-file first, and then other headers, you'll get this check without using external tools.

This is actually part of Googles C++ Style Guide for the same reason:

The preferred ordering reduces hidden dependencies. We want every header file to be compilable on its own. The easiest way to achieve this is to make sure that every one of them is the first .h file #included in some .cc.

If you have any header files that does not have a corresponding .cpp-file, you'd have to do something special for them.

like image 117
Kleist Avatar answered Sep 19 '22 23:09

Kleist


It's been a good number of years, but I believe Lint does this for you. And I seem to remember that Lint also tells you when a file is included when a forward declaration would do.

like image 42
graham.reeds Avatar answered Sep 21 '22 23:09

graham.reeds