December 16th, 2024

Why do we have header files <pshpackN.h> and <poppack.h> instead of just issuing the pragma directly?

When Windows header files need to adjust structure packing, they use the <pshpackN.h> and <poppack.h> header files. But why bother? Why not just issue the #pragma pack(push, N) and #pragma pack(pop) directly?

The #pragma pack directive is a Microsoft compiler extension. It is not part of the C or C++ standard. The <pshpackN.h> and <poppack.h> headers are used so that compiler vendors can provide substitute headers which generate directives that work for their compilers.

For example, here is one example of a replacement header file that records the structure packing stack in preprocessor symbols.

Related reading: What are anonymous structs, and more importantly, how do I tell windows.h to stop using them?

Topics
Other

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments