27

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?

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?

staffan
  • 5,641
  • 3
  • 32
  • 28
  • 1
    This is another question which directly relates to this: http://stackoverflow.com/questions/74326/how-should-i-detect-unnecessary-include-files-in-a-large-c-project. – Richard Corden Jul 02 '09 at 09:34

5 Answers5

4

You can use PC-Lint/FlexeLint to do that.

Unusually there isn't a free OS version of the tool available.

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.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
graham.reeds
  • 16,230
  • 17
  • 74
  • 137
  • I couldn't find any lint documentation that said it could do this. Do you have a pointer? – staffan Sep 11 '08 at 06:41
  • Unfortunately, I too couldn't find it freely available. I have a PC-Lint license, and I can vouch that it's in there. Perhaps you should contact Gimpel directly? They may send you the excerpt from the manual. – JXG Oct 23 '08 at 13:42
  • +e766 // Include of header file FileName not used in module String – graham.reeds Oct 23 '08 at 15:05
3

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)

Andy Brice
  • 2,297
  • 1
  • 21
  • 28
2

This article 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.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Steve Gury
  • 15,158
  • 6
  • 38
  • 42
2

There is an Eclipse plugin called includator which helps to manage include dependencies in C/C++ projects

http://includator.com/

0

Here is 'brute force' VC6 macro which works on single .cpp or .h file opened in editor by commenting include by include and running compile:

Sub RemoveNotUsedIncludes()

'Check if already processed; Exit if so
ActiveDocument.Selection.FindText "//INCLUDE NOT USED", dsMatchFromStart
IF ActiveDocument.Selection <> "" THEN
    ActiveDocument.Selection.SetBookmark
    MsgBox "Already checked"
    ActiveDocument.Selection.ClearBookmark
    EXIT SUB
END IF

'Find first #include; Exit if not found
ActiveDocument.Selection.FindText "#include", dsMatchFromStart
IF ActiveDocument.Selection = "" THEN
    MsgBox "No #include found"
    EXIT SUB
END IF

Dim FirstIncludeLine
FirstIncludeLine = ActiveDocument.Selection.CurrentLine

FOR i=1 TO 200

    'Test build
    ActiveDocument.Selection.SetBookmark
    ActiveDocument.Selection = "//CHECKING... #include"
    Build
    ActiveDocument.Undo
    ActiveDocument.Selection.ClearBookmark

    IF Errors = 0 THEN
        'If build failed add comment
        ActiveDocument.Selection.EndOfLine
        ActiveDocument.Selection = " //INCLUDE NOT USED"
    END IF

    'Find next include
    ActiveDocument.Selection.EndOfLine
    ActiveDocument.Selection.FindText "#include"

    'If all includes tested exit
    IF ActiveDocument.Selection.CurrentLine = FirstIncludeLine THEN EXIT FOR

NEXT

End Sub

Of case it could be improved to work on whole project.

Kjuly
  • 34,476
  • 22
  • 104
  • 118