What is the difference if any...?

CDev-8220 425 Reputation points
2025-07-31T00:37:23.93+00:00

I normally leave the framework headers arranged by the default template, and add all other headers after.

For example, #include <vector> #include <algorithm> #include <iterator> follows #include "targetver.h" #define WIN32_LEAN_AND_MEAN #include <windows.h> etc.

Then #include <unknwn.h> followed by the winrt headers.

I noticed that this arrangement of the headers is specific and deliberate.

I'm just wondering why, and if it really matters, since the developers of this code certainly know what they are doing.

Thanks for taking the time to answer, if this is not very important.

Developer technologies | C++
{count} votes

Accepted answer
  1. Susmitha T (INFOSYS LIMITED) 160 Reputation points Microsoft External Staff
    2025-07-31T08:59:42.5466667+00:00

    Thank you for reaching out. Please find the answer below.

     

    Yes, the order of header inclusion is deliberate and important, especially when working with Windows SDK and C++/WinRT.

     

    For instance:

    1. Including targetver.h first sets the version of Windows you want to target, which could affect how other headers behave.

    2.WIN32_LEAN_AND_MEAN reduces the size of the Windows headers, potentially speeding up compilation.

    3.Ordering standard headers like <vector>, <algorithm>, and <iterator> after includes that are specific to your application might help ensure that any local definitions or customizations take precedence.

    4.Avoiding Compilation Errors: Including headers in the wrong order—especially windows.h before NT or COM headers like unknwn.h—can lead to compilation or linker errors. For example, if windows.h is included before NT headers, it can define macros or types that conflict with those in nt.h, causing subtle and hard-to-debug issues.

    1. C++/WinRT and COM Compatibility: When using C++/WinRT, it's essential to include unknwn.h before any WinRT headers. This ensures that the classic COM interfaces are correctly defined and compatible with the WinRT projection. If you skip this or include it after, you may encounter issues with winrt::implements and other COM interop features.

     

    6.Use of Helper Headers: Microsoft recommends using helper headers like wil/cppwinrt.h which automatically include unknwn.h in the correct order and set up exception handling and error code translation between WIL and C++/WinRT.

     

    If issue still persist after following all the steps, we’ll be happy to assist further if needed." Kindly mark the answer as accepted if the issue resolved".


2 additional answers

Sort by: Most helpful
  1. Darran Rowe 2,171 Reputation points
    2025-07-31T14:23:07.3+00:00

    Interestingly, for the example linked, the order of inclusions is bad because of unkwn.h being included so early.

    As a COM header file generated from MIDL, unkwn.h automatically includes Windows.h. This means that the versioning coming from sdkddkver.h and WIN32_LEAN_AND_MEAN have no effect. The first inclusion of Windows.h is in unkwn.h and this is the one that is used. The ordering of includes is also enough of a problem that the /showincludes compiler option exists, which lists the order that files are included. The output is also indented to show which header includes what files too.

    To give a little counter example to the linked sample, this is the precompiled header that I am using for one of my projects.

    #pragma once
    
    #include <Windows.h>
    #include <combaseapi.h>
    
    #include <string>
    #include <string_view>
    
    #include <cstdint>
    
    #include <dxgi1_6.h>
    #include <d3d11_4.h>
    #include <d2d1_3.h>
    #include <wincodec.h>
    #include <wincodecsdk.h>
    #include <dwrite_3.h>
    
    #undef GetCurrentTime
    
    #include <winrt/Windows.Foundation.h>
    #include <winrt/Windows.Foundation.Collections.h>
    #include <winrt/Windows.System.h>
    #include <winrt/Windows.UI.Composition.h>
    #include <winrt/Windows.UI.Composition.Desktop.h>
    
    #include <wil/cppwinrt.h>
    #include <wil/resource.h>
    #include <wil/result.h>
    

    First, notice how there is no definitions like WIN32_LEAN_AND_MEAN.

    enter image description here

    These generally better suited as being part of the compiler command line. This guarantees that they will take effect regardless of the inclusion order.

    Secondly, notice that there is no explicit #include <unknwn.h>, this is included as part of combaseapi.h. This technically has a potential issue in that headers included in other headers can change, but this is a case where it is fine because combaseapi.h declares the base COM functions that depend on IUnknown, like CoCreateInstance.

    Unfortunately, in general, experience is really important.


  2. Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
    2025-07-31T19:43:02.4166667+00:00

    C/C++ is a forward reference only language. this means a reference must be defined in the file before it's referenced. this is true also of the code in include files. one include file may require a symbol exported by an earlier include. also an include may define a symbol if not previously defined. this may cause interaction issues between 2 include files.

    so the order of include files can be very important. switching the order may break the compile (best case) or cause a run-time error (difficult to track down).

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.