Other's library #define naming conflict

前端 未结 4 1906
忘掉有多难
忘掉有多难 2020-12-03 23:14

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 m

相关标签:
4条回答
  • 2020-12-03 23:45

    There is no general way of avoiding this problem - once you #include a header file using the preprocessor it can redefine any name it likes, and there is nothing you can do about it. You can #undef the name, but that assumes you know the name was #defined in the first place.

    0 讨论(0)
  • 2020-12-03 23:53

    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.

    0 讨论(0)
  • 2020-12-03 23:54

    It's an unfortunate side effect of #includeing <windows.h>. Assuming you're not actually using Windows' DrawText() anywhere in your program, it's perfectly safe to #undef it immediately after:

    // wherever you #include <windows.h>, or any other windows header
    #include <windows.h>
    #undef DrawText
    
    0 讨论(0)
  • 2020-12-03 23:55

    Just #undef the symbols you don't want. But Make sure that you include windows.h and do this before you include SDL:

    #include <windows.h>
    #undef DrawText
    
    #include <SDL/SDL_opengl.h>
    
    0 讨论(0)
提交回复
热议问题