• Advertisement

Archived

This topic is now archived and is closed to further replies.

Detecting Structure Padding

This topic is 5458 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m creating a header file (to be included in DLL projects) that allows DLLs to communicate with the larger, main application. The header file declares a struct, and I''ve figured that the structure must be packed to ensure that different compilers don''t insert padding in different places, leading to a situation in which the application and the DLLs (built with different compilers) have separate views of the same struct definition. However, different compilers have varying ways of indicating that a structure should be packed (VC++''s pragma directive as opposed to MingW''s __attribute__); is there a way to detect which compiler is being used so the correct directive may be invoked? RapscallionGL - arriving soon.

Share this post


Link to post
Share on other sites
Advertisement
You could use the #ifdef directive. Many (if not all) compilers have a preprocessor variable that stores the version of the compiler. For example, MSVC defines _MSC_VER. You can thus assume that if _MSC_VER is defined, the compiler is VC++. If you wanted, you could even compare the number to something to do different things on different versions. You just need to figure out what variable each compiler defines and make a huge block of #ifdef s

Share this post


Link to post
Share on other sites
Thanks. If anyone''s interested, the preprocessor variables/packing directives I found are:

Microsoft VC++
_MSC_VER
#pragma pack(1) / #pragma pack(pop)

Borland C++
_BORLANDC_
#pragma pack(1) / #pragma pack()

GNU C++
__GNUC__
__attribute__((packed))

Might be a useful thread when the search comes back online.

RapscallionGL - arriving soon.

Share this post


Link to post
Share on other sites

  • Advertisement