Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Other's library #define naming conflict

Tags:

c++

opengl

sdl

Hard to come up with a proper title for this problem. Anyway...

I'm currently working on a GUI for my games in SDL. I've finished the software drawing and was on my way to start on the OpenGL part of it when a weird error came up. I included the "SDL/SDL_opengl.h" header and compile. It throws "error C2039: 'DrawTextW' : is not a member of 'GameLib::FontHandler'", which is a simple enough error, but I don't have anything called DrawTextW, only FontHandler::DrawText. I search for DrawTextW and find it in a #define call in the header "WinUser.h"!

//WinUser.h
#define DrawText DrawTextW

Apparently it replaces my DrawText with DrawTextW! How can I stop it from spilling over into my code like that?

It's a minor thing changing my own function's name, but naming conflicts like this seem pretty dangerous and I would really like to know how to avoid them all together.

Cheers!

like image 448
Zoomulator Avatar asked Jul 03 '09 19:07

Zoomulator


People also ask

Can the public enter Republic Poly?

The Library is open to all users including members of the Public.

Can you return NLB books at a different library?

Hi Elaine, yes you may return your books borrowed from Central Public Library to any of our public libraries.

How many books can I borrow from NLB?

Commonly Asked Questions. How many eBooks / Audiobooks can I borrow? Each member can borrow up to 16 eBooks / Audiobooks. You can check your quota by selecting 'Account', then 'eBook Loans' in the app.

How do I borrow books from the National Library?

How can I borrow physical items with the NLB Mobile app? First, sign in with your myLibrary ID. Next, tap on the camera icon at the bottom of the app to launch the “Scan-to-Borrow” function. Use the in-app camera to scan the NLB barcode on the library item.


1 Answers

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

  • Add #undef DrawText in your own code
  • Don't include windows.h. If another library includes it for you, don't include that directly. Instead, include it in a separate .cpp file, which can then expose your own wrapper functions in its header.
  • Rename your own DrawText.

When possible, I usually go for the middle option. windows.h behaves badly in countless other ways (for example, it doesn't actually compile unless you enable Microsoft's proprietary C++ extensions), so I simply avoid it like the plague. It doesn't get included in my files if I can help it. Instead, I write a separate .cpp file to contain it and expose the functionality I need.

Also, feel free to submit it as a bug and/or feedback on connect.microsoft.com. Windows.h is a criminally badly designed header, and if people draw Microsoft's attention to it, there's a (slim) chance that they might one day fix it.

The good news is that windows.h is the only header that behaves this badly. Other headers generally try to prefix their macros with some library-specific name to avoid name collisions, they try to avoid creating macros for common names, and they try avoid using more macros than necessary.

like image 66
jalf Avatar answered Sep 20 '22 12:09

jalf