Handmade Hero»Forums»Code
22 posts
Setting up and using D3D11 in C
Edited by Bits Please on

When the level of warnings to be generated by the compiler is set to 4 (/W4), I'm getting multiple C4201 compiler warnings in the d3d11.h header file when compiling the following code:

#include <windows.h>
#include <d3d11.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	return 0;
}

I know that there are other ways to get rid of the warnings like lowering the warning level or suppressing that particular warning. But it just feels wrong to make these compromises for that single header file I want to include in my code. Especially because this header file was written by Microsoft who they, themselves, recommend to use /W4!

By the way, I've accidentally noticed that when I include the audioclient.h header file above the d3d11.h, all the warnings are gone and the code compiles perfectly fine:

#include <windows.h>
#include <audioclient.h>
#include <d3d11.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	return 0;
}

So it seems that audioclient.h contains something in it that eliminates these warnings, I just can't figure out what :(

18 posts
Setting up and using D3D11 in C

You can use #pragma warning to disable warnings for a specific section of code instead of changing it globally.

#pragma warning(push, 3) // Set warning level to 3
#include <d3d11.h>
#pragma warning(pop) // Restore previous warning level

You can also change individual warnings, and those can be used with push/pop as well.

#pragma warning(push)
#pragma warning(disable : 4201)
#include <d3d11.h>
#pragma warning(pop)

https://learn.microsoft.com/en-us/cpp/preprocessor/warning?view=msvc-170

Simon Anciaux
1340 posts
Setting up and using D3D11 in C

I was intrigued and I looked why audioclient.h disabled the warning. As William said, you can use #pragma warning(disable:4201) to disable the warning and that's what is happening.

Most of the headers use #pragma warning(push) and #pragma warning(pop) to avoid leaking the change to other headers. But audioclient.h includes two headers that don't do the push and pop:

  • ks.h C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\shared\ks.h
  • ksmedia.h C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\shared\ksmedia.h