Another #define Library Naming Conflict

It is difficult to find a suitable title for this problem. Anyway...

I am currently working on a GUI for my games in the SDL . I finished drawing the software and was on my way to start with its part of OpenGL when a strange error occurred. I included the header "SDL / SDL_opengl.h" and compiled. It throws "error C2039:" DrawTextW ": is not a member of" GameLib :: FontHandler ", which is a fairly simple error, but I have nothing called DrawTextW, only FontHandler :: DrawText. I look for DrawTextW and find it in the call # define in the header "WinUser.h"!

//WinUser.h
#define DrawText DrawTextW

It seems to be replacing my DrawText with DrawTextW! How can I stop it from overflowing in my code?

This is a minor change to my function name, but naming such conflicts seems pretty dangerous, and I'd really like to know how to avoid them all together.

Hurrah!

+6
source share
4 answers

You have a couple of options, all of which suck.

  • Add #undef DrawTextto your own code
  • Do not turn on windows.h. If another library includes this for you, do not include it directly. Instead, include it in a separate .cpp file, which can then output your own wrapper functions to your header.
  • Rename your own DrawText.

, . windows.h (, , ++ Microsoft), , . , . .cpp , , .

, / connect.microsoft.com. Windows.h - , Microsoft , () , .

, windows.h - , . , , , , .

+10

#include ing <windows.h>. , Windows 'DrawText() , #undef :

// wherever you #include <windows.h>, or any other windows header
#include <windows.h>
#undef DrawText
+5

There is no general way to avoid this problem - as soon as you # include the header file using the preprocessor, it can override any name that it likes, and you can do nothing about it. You can #undef a name, but that assumes that you know that the name was #defined in the first place.

+4
source

Just #undefcharacters you don't want. But make sure you turn it on windows.hand do it before turning on the SDL:

#include <windows.h>
#undef DrawText

#include <SDL/SDL_opengl.h>
0
source

All Articles