Skip to content

Commit

Permalink
Compilation-fix for VS 2019
Browse files Browse the repository at this point in the history
Windows assumes default alignment in their SDKs, so pacing needs to be done after `#include <windows.h>` or defining `WINDOWS_IGNORE_PACKING_MISMATCH`.
  • Loading branch information
Convery committed Jun 14, 2019
1 parent cef094d commit 5ccb00a
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions disasm-lib/cpu.h
Expand Up @@ -4,11 +4,13 @@
#ifdef __cplusplus
extern "C" {
#endif
#pragma pack(push,1)

#include <windows.h>
#include "misc.h"

// Windows SDK assumes default alignment.
#pragma pack(push,1)

////////////////////////////////////////////////////////
// System descriptors
////////////////////////////////////////////////////////
Expand Down Expand Up @@ -274,4 +276,4 @@ BYTE *GetAbsoluteAddressFromSelector(WORD Selector, DWORD Offset);
#ifdef __cplusplus
}
#endif
#endif // CPU_H
#endif // CPU_H

4 comments on commit 5ccb00a

@Brainor
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have no idea how much this git comment has helped me.
The compiler showed error C2118: negative subscript message at line 2487 in winnt.h with Windows 10 SDK 10.0.18362.0.
Moving the line #pragma pack(push,1) solved the problem.

@jrfonseca
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. BTW, putting includes inside extern "C" { ... } can easily create troubles too. It's best to avoid it.

@SergiusTheBest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrfonseca I think extern "C" can cause problems only for C++ headers, wrapping C headers in extern "C" is totally fine.

@jrfonseca
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True to some extent. The issue is that the distinction between C and C++ headers is not clear cut: one can find C++ code on C headers like math.h and windows headers. Nowadays system headers seem to enclose that code in extern C++ {..} precisely to avoid this issue, but in the past this would definitely cause troubles.

Please sign in to comment.