Journal Articles

Overload Journal #149 - February 2019 + Programming Topics
Browse in : All > Journals > Overload > o149 (8)
All > Topics > Programming (877)
Any of these categories - All of these categories

Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.

Title: QM Bites: Understand Windows Operating-System Identification Preprocessor Macros

Author: Bob Schmidt

Date: 04 February 2019 17:12:30 +00:00 or Mon, 04 February 2019 17:12:30 +00:00

Summary: Quality matters and bite sized articles help. Matthew Wilson returns with a QM Bites.

Body: 

TL;DR

Compiler defines _WIN32 and _WIN64; you define WIN32 or WIN64. Carefully discriminate.

Bite

When compiling for Windows 32 and 64-bit architectures, there are four main preprocessor object-like macro definitions for discriminating operating system (not architecture) that one may encounter:

You must take care that you understand the origins and meanings of these.

_WIN32 and _WIN64

The symbol _WIN32 is defined by the compiler to indicate that this is a (32-bit) Windows compilation. Unfortunately, for historical reasons, it is also defined for 64-bit compilation.

The symbol _WIN64 is defined by the compiler to indicate that this is a 64-bit Windows compilation.

Thus:

To identify unambiguously whether the compilation is 64-bit Windows one tests only _WIN64 as in:

  #if defined(_WIN64)
  /* Is Windows 64-bit */
  #else
  /* Is not Windows 64-bit */
  #endif

To identify unambiguously whether the compilation is 32-bit Windows one tests both _WIN32 and _WIN64 as in:

  #if defined(_WIN32) && \
     !defined(_WIN64)
  /* Is Windows 32-bit */
  #else
  /* Is not Windows 32-bit */
  #endif

To identify unambiguously whether the compilation is one or the other form of Windows one tests both _WIN32 and _WIN64 as in:

  #if defined(_WIN64)
  /* Is Windows 64-bit */
  #elif defined(_WIN32)
  /* Is Windows 32-bit */
  #else
  /* Not Windows */
  #endif

WIN32 and WIN64

The symbol WIN32 is defined by the user to indicate whatever the user chooses it to indicate. By convention, the definition of this symbol indicates a 32-bit Windows compilation, and nothing else! Microsoft (and other) tools generate projects with this symbol defined.

The symbol WIN64 is defined by the user to indicate whatever the user choose it to indicate. By convention, the definition of this symbol indicates a 64-bit Windows compilation, and nothing else!

When properly defined, these symbols can be used to indicate unambiguously the 32- and 64-bit Windows compilation contexts.

Caution with WIN32 / WIN64

Unfortunately, when duplicating a Win32 project to x64, the Microsoft Visual Studio wizards do not translate WIN32 to WIN64. You must remember to do this yourself, in order for the inferences given above to hold. Do not add a separate WIN64 to the x64 configuration settings – replace the existing WIN32 with WIN64. (All of this can be dealt with much better by use of props files, but that’s a long article …)

Why bother with WIN32 / WIN64 (and not simply rely on _WIN32 / _WIN64)?

There are doubtless many reasons. The reasons I adhere strictly to this are:

References

[Raymond03] Eric S. Raymond (2003) The Art of UNIX Programming Addison-Wesley, 2003

[UNIXem] UNIXem is a simple, limited UNIX-API emulation library for Windows. See http://synesis.com.au/software/unixem.html

Matthew Wilson Matthew is a software development consultant and trainer for Synesis Software who helps clients to build high-performance software that does not break, and an author of articles and books that attempt to do the same.

Notes: 

More fields may be available via dynamicdata ..