Qt World Summit: Register Today!

setting up precompiled header

  • Hi

    please clarify me with precompiled headers for latest Qtc and msvc2013 toolchain.
    my understanding this from Visual Studio is:

    1. set option use precompiled headers for a project and specify .h file for this
    2. mark .cpp stub file to generate pch (usually this is stdafx.cpp) - create pch option on this file
    3. set : use pch on all other cpp files in the project.
      Typically we do not do all the steps manually , VS does this itself at project generating time

    in Qtc , I see a few options in the documentation and a few opinions in a forums:

    1. use
      PRECOMPILED_HEADER = project_pch.h
      or directly
      CONFIG += project_pch.h
      do I understand this line from doc correct?
      "CONFIG += precompile_header"
      "precompile_header" here seems not a file name but a file definition?
    2. undocumented option PRECOMPILED_HEADER - what is it? is it required? looks like an option to specify "create PCH" file in MSVC
      PRECOMPILED_HEADER = $${SOURCECODE_DIR}/precompiled_header.h
      win32-msvc* {
      PRECOMPILED_SOURCE = $${SOURCECODE_DIR}/precompiled_header.cpp
      why all sources use win32-msvc* {... ???
      does this work for msvc only? and does not for mingw???

    So please clarify the questions or just explain right way which is really working

  • Moderators

    Assuming your precompiled header file is called stdafx.h all you need to do for MSVC is include it in your .cpp files and add this to the .pro:


    You don't need the stdafx.cpp file. You don't need it in Visual Studio either as you can set create on any of your cpp files.
    PRECOMPILED_HEADER is not undocumented. It's described in the qmake variables documentation.
    The undocumented one is PRECOMPILED_SOURCE. This is an equivalent of explicitly marking a cpp file that builds the precompiled header (e.g. stdafx.cpp). But, as stated above, you don't usually need it.

    As stated in this doc PRECOMPILED_HEADER is supported only on selected platforms, including MSVC. If you're planning to port to some unsupported platform surround it with win32-msvc* { ... } like in your example to disable it on all other compilers.
    It says it works on Unix gcc, and so by extension it also works with MinGW (which is Windows port of gcc).

    I'm using it with both MSVC and MinGW and it works fine.

  • thank you !

    you are right, that was typo: PRECOMPILED_HEADER
    I've mean PRECOMPILED_SOURCE of course.

    that sounds oddly for me that stdafx.cpp is not required,
    but ok, I'll try to build

    thanks again!

Log in to reply