处理宏重新定义而不修改.h文件... C / C ++语言 [英] Handling macro redefinition without modifying .h files ... C / C++ language

查看:155
本文介绍了处理宏重新定义而不修改.h文件... C / C ++语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:



假设我有两个头文件啊和bh



/ p>

  #define VAR 1 


b $ b

bh中包含:

  #define VAR 2 
pre>

注意:两个宏的名称相同。
假设我有一些文件myFile.c包括头文件ieah和bh



当我尝试访问VAR时,我得到一个重定义错误的VAR。



为了解决这个问题,我在ah和bh文件中插入#ifndef VAR语句以防止此错误。
ah文件变成

  #ifndef VAR 
#define VAR 1
#endif

bh文件变成

  #ifndef VAR 
#define VAR 2
#endif

注意:头文件可以包含多个宏,而不只是一个宏。



问题:



ah和bh文件均从第三方库获取。这些文件不包含#ifndef VAR语句。



我不允许更改其头文件。



我可以在使用VAR宏的myFile.c或myFile.cpp文件中解析宏'VAR'重新定义错误吗?



我知道我#undef VAR可以用来取消定义宏VAR。如何在我的程序中选择性地包括VAR?即在myFile.c代码的第10行,我应该能够引用来自ah文件的VAR定义,在我的代码的第15行,我应该能够从bh文件引用VAR,并且在第18行,我应该能够引用to a VAR from ah file。



总之,我能做宏观多态性吗?
给定一个头文件名,它应该引用该文件中的宏定义。



我想到使用命名空间技巧来解决问题。定义命名空间第一个头文件和命名空间第二个头文件。



我试过定义两个命名空间。第一个命名空间包含#include a.h,第二个命名空间包含b.h.然而,命名空间技巧不能与宏工作。当我试图访问firstns :: VAR时,编译器报告一个错误消息。



你能建议一些方法吗?

解决方案

宏扩展发生在预处理器级别,是不允许命名空间使用。



您要使用所有宏,使用简单的常量,不在 token concatenation 等等。



如果是这样,那么你可以尝试类似下面的东西桥接预处理器/编译器间隙并保留对 VAR 定义的 A B code>等,如果它适合您的情况:

  // ab_wrapper.h 
#include< ; a.h>
static const int A_VAR1 = VAR1;
static const char * A_VAR2 = VAR2;
#undef VAR1
#undef VAR2

#include< b.h>
static const int B_VAR1 = VAR1;
static const char * B_VAR2 = VAR2;

// code.c
#include< ab_wrapper.h>
...
int x = A_VAR1;
int y = B_VAR1;
...


Background:

Let assume that I have two header files a.h and b.h.

a.h contains:

#define VAR 1

b.h contains:

#define VAR 2

Note: The name of both of the macro is same. Let say I have some file myFile.c which includes both of the header files i.e. a.h and b.h.

When I try to access VAR, I get a redefinition error of VAR.

In order to resolve this problem, I inserted #ifndef VAR statement in both a.h and b.h files to prevent this error. a.h file becomes

#ifndef VAR
  #define VAR 1
#endif

b.h file becomes

#ifndef VAR
  #define VAR 2
#endif

Note: The header file can contain multiple macros, not just one macro.

Problem:

Let's assume that a.h and b.h files are obtained from third party library. These files don't contain #ifndef VAR statement.

I am not allowed to change their header files.

Can I resolve macro 'VAR' redefinition error in myFile.c or myFile.cpp file which uses VAR macro?

I know I #undef VAR can be used to undefine a macro VAR. How can I selectively include VAR later in my program? i.e. on line 10 of myFile.c code I should be able to refer to VAR definition from a.h file, on line 15 of my code I should be able to refer to VAR from b.h file and on line 18 again I should be able to refer to VAR from a.h file.

In short, am I able to do macro polymorphism ? Given a name of header file, it should refer to macro definition present in that file.

I thought of using namespace trick to resolve an issue. Define first header file in namespace first and second header file in namespace second.

I tried defining two namespaces. First namespace contains #include a.h and second namespace contains b.h. However, namespace trick does not work with macro. When I tried to access firstns::VAR, compiler reports an error message.

Can you please suggest some way?

解决方案

Macro expansion happens at the preprocessor level and is impervious to namespace use.

Are all macros that you want to use simple constants, not used in token concatenation etc.?

If so, then you can try something like the following to bridge the preprocessor/compiler gap and retain access to both the A and B definitions of VAR etc., if it suits your situation:

// ab_wrapper.h
#include <a.h>
static const int   A_VAR1 = VAR1;
static const char* A_VAR2 = VAR2;
#undef VAR1
#undef VAR2

#include <b.h>
static const int   B_VAR1 = VAR1;
static const char* B_VAR2 = VAR2;

// code.c
#include <ab_wrapper.h>
...
int x = A_VAR1;
int y = B_VAR1;
...

这篇关于处理宏重新定义而不修改.h文件... C / C ++语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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