其他的库#define命名冲突 [英] Other's library #define naming conflict

查看:731
本文介绍了其他的库#define命名冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很难想出这个问题的正确标题。无论如何...

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

我目前正在使用 GUI for my games in SDL 。我完成了软件绘图,并在我的方式开始在OpenGL的一部分,当一个奇怪的错误出现了。我包括SDL / SDL_opengl.h头并编译。它抛出错误C2039:'DrawTextW':不是'GameLib :: FontHandler'的成员,这是一个足够简单的错误,但我没有什么叫DrawTextW,只有FontHandler :: DrawText。我搜索DrawTextW,并在标题WinUser.h中的#define调用中找到它。

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

显然,它用DrawTextW替换了我的DrawText!如何阻止它溢出到我的代码?

这是一个小的改变我自己的函数的名字,但命名冲突这样看起来很漂亮危险,我真的想知道如何避免他们在一起。

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.

干杯!

推荐答案

您有几个选项,所有这些都可以。

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


  • 添加 #undef c>

  • 不要包含 windows.h 。如果另一个库包含它,不要直接包含它。

  • 重命名您自己的 DrawText

  • 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.

如果可能,我通常选择中间选项。 windows.h 在无数其他方式表现不佳(例如,它实际上不编译,除非您启用Microsoft的专有C ++扩展),所以我只是避免它像瘟疫。它不包括在我的文件,如果我可以帮助它。相反,我写一个单独的.cpp文件来包含它,并暴露我需要的功能。

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.

此外,随时提交它作为一个bug和/ .microsoft.com。 Windows.h是一个犯罪性设计错误的标题,如果人们吸引微软的注意,有一个(苗条)的机会,他们可能有一天修复它。

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.

消息是 windows.h 是表现不佳的 only 标题。其他头文件通常尝试用一些库特定的名称前缀他们的宏,以避免名称冲突,他们试图避免为常用名称创建宏,并且他们尽量避免使用更多的宏。

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.

这篇关于其他的库#define命名冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆