Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

A tool to remove unused header files?



  • As the title says... is anyone aware of a tool (ideally, as part of Qt Creator) to remove unused header files in a CPP file?

    Cheers, Alan.


  • Moderators

    Qt Creator does not have such a tool integrated.

    There is the "iwyu clang extension":https://code.google.com/p/include-what-you-use/ that is supposed to do it, but so far I had little success with it as its output needs some changing to work well with Qt headers. It only reports headers that it recommends to be added/removed, so you will need to do the editing.

    PS: You can use the tasklist plugin of Qt Creator with a bit of scripting to make that editing a bit less painful.



  • Ok, I have just had a quick look (https://code.google.com/p/include-what-you-use/wiki/InstructionsForUsers) and this is still a beta tool and requires quite a few things to get it off the ground (LLVM+Clang). So, it seems to me that I might be wasting more time than anything trying to use it.


  • Moderators

    I did not find it too hard to test (I tend to have a recent build of clang around;-), but the results were not really useful to me: It suggested to remove all the "QSomeThing" headers and then wanted to have a lot of some/path/qsomething.h" headers added. That caused a lot of noise which made it really hard to find the real changes.

    A bit of scripting should help here. Maybe the tool could get fixed to not complain about headers that only include others. I should look into that at some point... if I find the time for some recreational hacking that is:-)



  • Actually, in the general case, I think a tool like this should catch such "forward" headers. They are often used as general "always include this header" kind of things in projects, and often contain a lot of stuff you don't really need. However, in Qt's case, most of them are actually useful. It is nice to be able to write
    @
    #include <QString>
    @
    instead of
    @
    #include "QtCore/qstring.h"
    @
    or something like that. However, I think that the tool should suggest to replace
    @
    #include <QtCore>
    @
    with the include for QString only, if that is all you use.



  • @tobias-hunger excuse me , i 'm trying to use the "include-what-you-use" with a qt project, but i failed to do that. And i didn't find any tutorial, coud u tell me how to config it please? Thank u.


Log in to reply