宏定义冲突

发布于 2024-11-30 01:22:11 字数 452 浏览 4 评论 0原文

我遇到了这个问题。这与宏函数无关,只是简单的字符串值宏替换。

我有两个头文件

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三岁铭 2024-12-07 01:22:11

您可以使用 #ifdef 预处理器指令来确定是否已针对您的特定情况定义了 TEST。或者先#undef

#undef TEST
#define TEST 123

将其放入您希望 TEST 为 123 而不是 456 的头文件中。此外,它需要位于 test1.h 之前。

You can use the #ifdef pre-processor directive to determine if TEST is defined already for your particular case. Or just #undef it first.

#undef TEST
#define TEST 123

Put that in your header file where you want TEST to be 123 and not 456. Also, this needs to be before test1.h.

伪心 2024-12-07 01:22:11

#undef TEST 在包含 test2.h 之后且包含 test1.h 之前。不过,这有点麻烦,因为您无法修复宏名称。

#undef TEST after the include of test2.h and before the include of test1.h. This is a bit of a hack though since you can't fix the macro names.

命比纸薄 2024-12-07 01:22:11

如果您将两个标头都包含到文件中,则可以取消定义它:

//yourfile.cpp

#include "test2.h"  //include this before test1.h

#undef TEST   //this undefines the macro defined in test2.h 

#include "test1.h"  //now this defines a macro called TEST which you need

You can undefine it if you include both headers to your file as:

//yourfile.cpp

#include "test2.h"  //include this before test1.h

#undef TEST   //this undefines the macro defined in test2.h 

#include "test1.h"  //now this defines a macro called TEST which you need
烟雨凡馨 2024-12-07 01:22:11
#ifdef TEST
#undef TEST
#define TEST 123
#endif
#ifdef TEST
#undef TEST
#define TEST 123
#endif
懒猫 2024-12-07 01:22:11

试试这个:

#include "test2.h"
#undef TEST
#include "test1.h"

首先包含 test2,丢弃其 TEST,然后包含 test1。

Try this:

#include "test2.h"
#undef TEST
#include "test1.h"

This first includes test2, discards its TEST and then includes test1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文