How do I use Header Units with the Windows Desktop Application default template and resolve conflicts with headers?

CDev-8220 220 Reputation points
2024-12-13T23:41:21.97+00:00

I am trying to use Header Units in a Desktop Application, but am getting problems with headers, and the .ifc file.

  1. Problems with gdi, and DirectX header includes : C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\um\GdiplusEnums.h(27,13): warning C5260: the constant variable 'Gdiplus::FlatnessDefault' has internal linkage in an included header file context, but external linkage in imported header unit context; consider declaring it 'inline' as well if it will be shared across translation units, or 'static' to express intent to use it local to this translation unit; Multiple DirectX header includes with the same error.

I removed these includes, and was able to build the project.

Now, when I use

import "header.h";

I get

Error LNK1000 Internal error during IncrBuildImage

I am using the guide from cpp-build-msvc-170 p. 211-218

I took note of this:

Header units are a step in between header files and C++ 20 modules. They provide some of the benefits of modules. They're more robust because outside macro definitions don't affect them--so you can import them in any order. And the compiler can process them faster than header files. But header units don't have all of the advantages of modules because header units expose the macros defined within them (modules don't). Unlike modules, there's no way to hide private implementation in a header unit. To indicate private implementation with header files, different techniques are employed like adding leading underscores to names, or putting things in an implementation namespace. A module doesn't expose private implementation in any form, so you don't need to do that. Consider replacing your precompiled headers with header units. You get the same speed advantage, but with other code hygiene and flexibility benefits as well.

I tried using namespace, but I am not sure if I am going about it correctly, because when using namespace name in the main .cpp file the program crashes.

What is the correct way to do this, and what other techniques are they beside using underscores?

Thanks.

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,649 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,699 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,805 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Darran Rowe 1,236 Reputation points
    2024-12-15T23:54:54.1366667+00:00

    Unfortunately, this is one of those circumstances where if you have to ask this question then you are not ready to do this.

    If you really wish to try this though, I seriously suggest that digging deep into winnt.h and trying to make a coherant view of that file is the first stop. The reason why I mention trying to work with winnt.h is because it perfectly highlights all of the problems with the Windows headers. If an example of what I mean is required, find the definitions of the CONTEXT structure. For the current Windows headers, CONTEXT will be defined as one of four incompatible definitions based upon the predefined macros.

    But I also feel that just rewriting these headers to be more C++ and module friendly will be a much easier task overall.


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.