Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a clean way to prevent windows.h from creating a near & far macro?

Tags:

c++

c

winapi

dos

Deep down in WinDef.h there's this relic from the segmented memory era:

#define far
#define near

This obviously causes problems if you attempt to use near or far as variable names. Any clean workarounds? Other then renaming my variables?

like image 656
hyperlogic Avatar asked Sep 23 '08 02:09

hyperlogic


2 Answers

You can safely undefine them, contrary to claims from others. The reason is that they're just macros's. They only affect the preprocessor between their definition and their undefinition. In your case, that will be from early in windows.h to the last line of windows.h. If you need extra windows headers, you'd include them after windows.h and before the #undef. In your code, the preprocessor will simply leave the symbols unchanged, as intended.

The comment about older code is irrelevant. That code will be in a separate library, compiled independently. Only at link time will these be connected, when macros are long gone.

like image 178
MSalters Avatar answered Nov 12 '22 23:11

MSalters


Undefine any macros you don't want after including windows.h:

#include <windows.h>
#undef near
#undef far
like image 9
John Millikin Avatar answered Nov 13 '22 01:11

John Millikin