代码之家  ›  专栏  ›  技术社区  ›  staffan

如何自动查找未使用的#include指令?

c
  •  27
  • staffan  · 技术社区  · 16 年前

    通常,在编写新代码时,您会发现缺少一个#include,因为该文件无法编译。很简单,您可以添加所需的#include。但是后来你以某种方式重构了代码,现在不再需要几个#include指令了。我如何发现哪些不再需要?

    5 回复  |  直到 16 年前
        1
  •  4
  •   Drew Dormann    12 年前

    你可以用 PC-Lint/FlexeLint

    不同寻常的是,该工具没有免费的操作系统版本。

    您可以通过引用而不是通过值和前向声明来删除#includes。这是因为编译器不需要在编译时知道对象的大小。然而,这将需要代表您进行大量的手动工作。好的方面是它将减少编译时间。

        2
  •  3
  •   Andy Brice    16 年前

    您只需编写一个“暴力”命令行工具,逐条注释#includes并测试编译是否仍然有效。你有工作的时候告诉我;0)

        3
  •  2
  •   Juanjo Marin    14 年前

    有一个名为includator的Eclipse插件,可以帮助管理C/C++项目中的include依赖项

    http://includator.com/

        4
  •  2
  •   Drew Dormann    12 年前
        5
  •  0
  •   Kjuly    12 年前

    这是一个“暴力”VC6宏,它可以在单个 在编辑器中通过注释include by include并运行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
    

    端接头