Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. How to enable checking of unused includes ?
Qt 6.11 is out! See what's new in the release blog

How to enable checking of unused includes ?

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
10 Posts 6 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fernandesh
    wrote on last edited by
    #1

    Dear all,

    qtCreator is a great tool, but I am missing some features that are natively enabled in IDEs specialized in Java or Python.

    For instance, in the code below, obviously the header thread is not used, but I couldn't enable this warning.

    #include <iostream>
    #include <thread>
    
    int main()
    {
        std::cout << "Hello World!" << std::endl;
        return 0;
    }
    

    In longer codes that warning can be useful.

    JonBJ 1 Reply Last reply
    0
    • F fernandesh

      Dear all,

      qtCreator is a great tool, but I am missing some features that are natively enabled in IDEs specialized in Java or Python.

      For instance, in the code below, obviously the header thread is not used, but I couldn't enable this warning.

      #include <iostream>
      #include <thread>
      
      int main()
      {
          std::cout << "Hello World!" << std::endl;
          return 0;
      }
      

      In longer codes that warning can be useful.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @fernandesh
      It will be interesting to see if the C++ experts know better, but I don't think this could be done for C++. #include files can contain all sorts of stuff, and preprocessor directives, I would have thought it would be very difficult to be sure that nothing in an included had any effect anywhere. Java or Python are more organized/restrictive on imports. I do not recall seeing any IDE for C++ showing me included files were not needed.

      Now someone will tell me I'm quite wrong.... :)

      Christian EhrlicherC F JoeCFDJ 3 Replies Last reply
      1
      • JonBJ JonB

        @fernandesh
        It will be interesting to see if the C++ experts know better, but I don't think this could be done for C++. #include files can contain all sorts of stuff, and preprocessor directives, I would have thought it would be very difficult to be sure that nothing in an included had any effect anywhere. Java or Python are more organized/restrictive on imports. I do not recall seeing any IDE for C++ showing me included files were not needed.

        Now someone will tell me I'm quite wrong.... :)

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @JonB You're correct.
        There are some external tools like e.g. https://github.com/include-what-you-use/include-what-you-use

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        F 1 Reply Last reply
        2
        • JonBJ JonB

          @fernandesh
          It will be interesting to see if the C++ experts know better, but I don't think this could be done for C++. #include files can contain all sorts of stuff, and preprocessor directives, I would have thought it would be very difficult to be sure that nothing in an included had any effect anywhere. Java or Python are more organized/restrictive on imports. I do not recall seeing any IDE for C++ showing me included files were not needed.

          Now someone will tell me I'm quite wrong.... :)

          F Offline
          F Offline
          fernandesh
          wrote on last edited by
          #4

          @JonB, thanks for your enlightening answer.

          What you pointed was one of my concerns, too: for instance, defines can be anywhere.

          However, in a world of precompiled headers, maybe there's light to this possible (and sometimes probable) mess.

          And someday the C++ modules (defined in C++20) will become a reality and we C++ programmers will no longer be envious of Python and Java.

          1 Reply Last reply
          0
          • JonBJ JonB

            @fernandesh
            It will be interesting to see if the C++ experts know better, but I don't think this could be done for C++. #include files can contain all sorts of stuff, and preprocessor directives, I would have thought it would be very difficult to be sure that nothing in an included had any effect anywhere. Java or Python are more organized/restrictive on imports. I do not recall seeing any IDE for C++ showing me included files were not needed.

            Now someone will tell me I'm quite wrong.... :)

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #5

            @JonB Qt Creator 12 has this feature now. It highlights unused includes, but not all of them are correct.

            JonBJ 1 Reply Last reply
            2
            • JoeCFDJ JoeCFD

              @JonB Qt Creator 12 has this feature now. It highlights unused includes, but not all of them are correct.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @JoeCFD said in How to enable checking of unused includes ?:

              but not all of them are correct.

              Which is my point! I would have thought it might give false positives (in the sense of thinking an include had no effect when it actually does). Might be pathological cases, I don't know.

              JoeCFDJ 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @JonB You're correct.
                There are some external tools like e.g. https://github.com/include-what-you-use/include-what-you-use

                F Offline
                F Offline
                fernandesh
                wrote on last edited by
                #7

                Thanks for your reference @Christian-Ehrlicher !

                include-what-you-want looks promising, and is project constantly updated. I'll give it a try.

                1 Reply Last reply
                0
                • JonBJ JonB

                  @JoeCFD said in How to enable checking of unused includes ?:

                  but not all of them are correct.

                  Which is my point! I would have thought it might give false positives (in the sense of thinking an include had no effect when it actually does). Might be pathological cases, I don't know.

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #8

                  @JonB But good enough. Very easy to test. Nice feature.

                  Chris KawaC 1 Reply Last reply
                  1
                  • JoeCFDJ JoeCFD

                    @JonB But good enough. Very easy to test. Nice feature.

                    Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    It mostly depends on how close to the compiler is the editor's code model. Those have to take some shortcuts for the sake of speed, but they are getting pretty good.

                    I don't know about Qt Creator, but I've used it in Visual Studio and Rider and both work pretty much as expected. The few false positives are usually a simple compilation error because of a missing type, so an easy fix. Of course you can always f up your project if you use a bunch of conditionals in the preprocessor, but that's always a risk even without the feature.

                    aha_1980A 1 Reply Last reply
                    2
                    • Chris KawaC Chris Kawa

                      It mostly depends on how close to the compiler is the editor's code model. Those have to take some shortcuts for the sake of speed, but they are getting pretty good.

                      I don't know about Qt Creator, but I've used it in Visual Studio and Rider and both work pretty much as expected. The few false positives are usually a simple compilation error because of a missing type, so an easy fix. Of course you can always f up your project if you use a bunch of conditionals in the preprocessor, but that's always a risk even without the feature.

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      As @Chris-Kawa and @JoeCFD already said, Creator now has this feature.

                      It is provided by Clangd, and therefore as good as Clangd understands your project.

                      In header files, I got some false positives also, but in source files it works quite good.

                      Regards

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved