<p>Typically when writing new code you discover that you are missing a #include because the file doesn't compile. Simple enough, you add the required #include. But later you refactor the code somehow and now a couple of #include directives are no longer needed. How do I discover which ones are no longer needed? </p>

<p>Of course I can manually remove some or all #include lines and add them back until the file compiles again, but this isn't really feasible in a large project with thousands of files. Are there any tools available that will help automating task?</p>

[{'answer_id': 51566, 'author': 'graham.reeds', 'author_id': 342, 'author_profile': 'https://Stackoverflow.com/users/342', 'pm_score': 3, 'selected': True, 'text': '<p>You can use <a href="http://www.gimpel.com/" rel="nofollow noreferrer">PC-Lint/FlexeLint</a> to do that.</p>\n\n<p>Unusually there isn\'t a free OS version of the tool available.</p>\n\n<p>You can remove #includes by passing by reference instead of passing by value and forward declaring. This is because the compiler doesn\'t need to know the size of the object at compile time. This will require a large amount of manual work on your behalf however. The good thing is it will reduce your compile times.</p>\n'}, {'answer_id': 51639, 'author': 'Andy Brice', 'author_id': 455552, 'author_profile': 'https://Stackoverflow.com/users/455552', 'pm_score': 2, 'selected': False, 'text': "<p>You could just write a 'brute force' command line tool that comments out the #includes one by one and tests whether the compile still works. Let me know when you've got it to work. ;0)</p>\n"}, {'answer_id': 51677, 'author': 'Steve Gury', 'author_id': 1578, 'author_profile': 'https://Stackoverflow.com/users/1578', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://gamesfromwithin.com/physical-structure-and-c-part-1-a-first-look" rel="nofollow noreferrer">This article</a> explains a technique of #include removing by using the parsing of Doxygen. That\'s just a perl script, so it\'s quite easy to use.</p>\n'}, {'answer_id': 5560096, 'author': 'Juanjo Marin', 'author_id': 211242, 'author_profile': 'https://Stackoverflow.com/users/211242', 'pm_score': 2, 'selected': False, 'text': '<p>There is an Eclipse plugin called includator which helps to manage include dependencies in C/C++ projects</p>\n\n<p><a href="http://includator.com/" rel="nofollow">http://includator.com/</a></p>\n'}, {'answer_id': 8667351, 'author': 'Gediminas', 'author_id': 1121061, 'author_profile': 'https://Stackoverflow.com/users/1121061', 'pm_score': 0, 'selected': False, 'text': '<p>Here is \'brute force\' VC6 macro which works on single <strong><em>.cpp</strong> or <strong></em>.h</strong> file opened in editor by commenting include by include and running compile:</p>\n\n<pre><code>Sub RemoveNotUsedIncludes()\n\n\'Check if already processed; Exit if so\nActiveDocument.Selection.FindText "//INCLUDE NOT USED", dsMatchFromStart\nIF ActiveDocument.Selection &lt;&gt; "" THEN\n    ActiveDocument.Selection.SetBookmark\n    MsgBox "Already checked"\n    ActiveDocument.Selection.ClearBookmark\n    EXIT SUB\nEND IF\n\n\'Find first #include; Exit if not found\nActiveDocument.Selection.FindText "#include", dsMatchFromStart\nIF ActiveDocument.Selection = "" THEN\n    MsgBox "No #include found"\n    EXIT SUB\nEND IF\n\nDim FirstIncludeLine\nFirstIncludeLine = ActiveDocument.Selection.CurrentLine\n\nFOR i=1 TO 200\n\n    \'Test build\n    ActiveDocument.Selection.SetBookmark\n    ActiveDocument.Selection = "//CHECKING... #include"\n    Build\n    ActiveDocument.Undo\n    ActiveDocument.Selection.ClearBookmark\n\n    IF Errors = 0 THEN\n        \'If build failed add comment\n        ActiveDocument.Selection.EndOfLine\n        ActiveDocument.Selection = " //INCLUDE NOT USED"\n    END IF\n\n    \'Find next include\n    ActiveDocument.Selection.EndOfLine\n    ActiveDocument.Selection.FindText "#include"\n\n    \'If all includes tested exit\n    IF ActiveDocument.Selection.CurrentLine = FirstIncludeLine THEN EXIT FOR\n\nNEXT\n</code></pre>\n\n<p>End Sub</p>\n\n<p>Of case it could be improved to work on whole project.</p>\n'}]
['https://Stackoverflow.com/questions/51561', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/988/']