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

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

问题描述

背景:

让假设我有两个头文件A.H和b.h。

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

A.H包括:

#define VAR 1

b.h包括:

#define VAR 2

请注意:无论是宏的名称是一样的。
让说我有一些文件myfile.c文件,其中包括两个头文件,即A.H和b.h。

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.

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

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

为了解决这个问题,我插在两个A.H和b.h文件的#ifndef VAR语句prevent此错误。
A.H文件将成为

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文件成为

b.h file becomes

#ifndef VAR
  #define VAR 2
#endif

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

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

问题:

假设A.H和b.h文件是从第三方库获得。这些文件不包含的#ifndef VAR声明。

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.

我能解决它采用VAR宏观myfile.c文件或文件MYFILE.CPP宏VAR重新定义错误?

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

我知道我和#undef VAR可用于取消定义一个宏VAR。我怎样才能选择包括VAR后来在我的计划?即在myfile.c文件code的第10行我应该能够指从文件啊VAR的定义,在我的code的第15行,我应该能够从BH文件和行18再次提及VAR我应该能够从文件啊参考VAR。

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.

总之,我能够做的宏多态?
由于头文件的名字,它应该是指宏定义present在该文件中。

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.

我试图定义两个命名空间。首先命名空间包含的#include A.H和第二命名空间包含B.H.然而,命名空间招不与宏观调控工作。当我试图访问firstns :: VAR,编译器报告的错误消息。

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?

推荐答案

宏扩展发生在的 preprocessor ​​水平,是不受命名空间的使用。

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

如果是这样,那么你可以尝试类似以下弥合preprocessor ​​/编译差距,并保留这两个 A 接入和 VAR 等的定义,如果它适合您的情况:

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天全站免登陆