Combating macro pollution

Published April 30, 2008
Advertisement
So one of the classic problems with Win32 is the macro pollution.

As some of you may remember, near and far used to have significance as keywords on the segmented x86 architecture. They remain in as empty macros.

Enter: me. I want to make a frustum class with near and far members. Unfortunately, when is in view, my members get hit by the preprocessor. What do I do?

One option is to change the member names. If the members are public, this results in a frustum class with a non-obvious interface.

GCC provides an alternate solution: #include_next. It works like this:
  1. Create a special header in a subdirectory of your project tree. I use "src/extern/windows.h".

    #pragma GCC system_header#ifndef page_extern_windows_inc#define page_extern_windows_inc#include_next #undef far#undef near#endif

  2. Add -Isrc/extern to your CFLAGS.
When you include , you'll get "src/extern/windows.h" first, which will itself include the real and undefine the offending macros.
Previous Entry Dynamic AABB
Next Entry Phun with Loo-ah
0 likes 2 comments

Comments

sprite_hound
Yeah. I got hit by this too. Your solution's pretty neat.

I don't actually include windows.h myself, so it must be something from SDL. Ugh.
May 01, 2008 04:50 PM
dcosborn
I did a quick test, and the solution still works when the header is included indirectly. Therefore, I should have overridden <windef.h> instead of <windows.h>. I suggest you give it a try and see.
May 01, 2008 05:59 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

Advertisement