宏定义冲突
我遇到了这个问题。这与宏函数无关,只是简单的字符串值宏替换。
我有两个头文件
test1.h
#define TEST 123
test2.h
#define TEST 456
现在我有一个程序包含这两个头文件,但我希望我的实际 TEST 为 123。 如何避免将 TEST 定义为 456?
你可能会认为我疯了,不简单地改变宏,但情况是:我有一个第三方解码器,它有这个宏(在test1.h中定义),并且还有另一个WINAPI宏(在test2.h中定义) )。这两个文件都是被别人控制的;我不应该改变他们中的任何一个。 我根本不需要 test2.h,但我猜它已隐式包含在其他一些 WINAPI 标头中。
那么,有人可以告诉我如何解决这个问题吗?用我的第三方宏覆盖 WINAPI 宏?或者如何在我自己的代码中取消 WINAPI 标头中的定义?有没有办法指定我不想包含哪个标头。
I'm running into this issue. This is not about macro functions, just simple string-value macro replacement.
I have two header files
test1.h
#define TEST 123
test2.h
#define TEST 456
Now I have a program included both these two headers, but I want my actually TEST to be 123.
How can I avoid defining TEST as 456?
You might think I'm crazy not to simply change the macro, but the situation is: I have a third-party decoder, which has this macro (defined in test1.h), and there's another WINAPI macro (defined in test2.h). Both of these files are controlled by others; I should not change either of them.
I don't need the test2.h at all, but I guess it's implicitly included by some other WINAPI header.
So, could anyone please tell me how to work around this issue? To overwrite the WINAPI macro with my third-party macro? Or how to nullify the definition from the WINAPI header in my own code? Is there a way to specify which header I don't want to include.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
#ifdef
预处理器指令来确定是否已针对您的特定情况定义了TEST
。或者先#undef
。将其放入您希望
TEST
为 123 而不是 456 的头文件中。此外,它需要位于test1.h
之前。You can use the
#ifdef
pre-processor directive to determine ifTEST
is defined already for your particular case. Or just#undef
it first.Put that in your header file where you want
TEST
to be 123 and not 456. Also, this needs to be beforetest1.h
.#undef TEST
在包含test2.h
之后且包含test1.h
之前。不过,这有点麻烦,因为您无法修复宏名称。#undef TEST
after the include oftest2.h
and before the include oftest1.h
. This is a bit of a hack though since you can't fix the macro names.如果您将两个标头都包含到文件中,则可以取消定义它:
You can undefine it if you include both headers to your file as:
试试这个:
首先包含 test2,丢弃其
TEST
,然后包含 test1。Try this:
This first includes test2, discards its
TEST
and then includes test1.