Important: Please read the Qt Code of Conduct -

Global include

  • Hi,

    I have a constant file (constant.h ) and I want to include it in all my files without writing: #include "constant.h" everywhere. Is it possible to do it in Qt Creator, and if yes, how to do it.


  • That's not really possible, as the compiler must be able to see the contents of each include. So you do need to actually #include it for the compiler to see, and use, it.

  • Qt Creator hasn't an option to auto include the constant file in each file?

  • [quote author="readbooks" date="1347296539"]Qt Creator hasn't an option to auto include the constant file in each file?[/quote]

    No, you must include it in every file where you use these constants.
    This is an C++ issue and not Qt (Creator) related.

  • Moderators

    No, Qt Creator does not have such an option. There is a reasons for this:

    We want projects to be usable for people that do not want to use creator for whatever (obviously misguided;-) reasons. So we can not put options that need to be applied for every build into Creator. Those need to go into the build system.

    So why don't we add code into your build system for you? Doing so will limit which compilers can be used to build your project. Which compilers need to be supported is a decision the project-team has to make. We do not want creator to take away decisions from the developers.

    You can do this manually by adding code into the .pro-files (or CMakeLists.txt or whatever you use;-).

    It is pretty straight forward: You just need to check for compilers used and add the appropriate flags (check the documentation for those).

    The following code sets some compiler specific flags for qmake based project (default kind of project in Creator):

    g++:QMAKE_CXXFLAGS += -Wall -Werror
    clang:QMAKE_CXXFLAGS += -Wall -Werror -Wno-something
    icc:QMAKE_CXXFLAGS += -Wall -Werror -Wno-something-else

    (Note: Brain to terminal, untested)

Log in to reply