ç的typedef名称冲突 [英] C typedef name conflict

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

问题描述

我已经得到了2头文件,我包括两者都有的typedef同名。

假设aaa.h和bbb.h.他们来自两个不相关的第三方的包,我没有这2个文件的控制权,但必须使用它们。我不直接包括文件,但他们是相当子包括在顶层头文件。 (即我有A.H和b.h分别包括他们)

我的程序不会编译时,它们都包括在内,因为名称冲突。

所以,我临时的解决方案是我复制到aaa.h我的源代码树只的typedef去掉,叫myaaa.h。在文件的顶部我守的#ifndef AAA_H和#define AAA_H包装完好,所以当我有myaaa.h,aaa.h将不包括因为AAA_H标志已经被定义,间接地取出的typedef冲突。

有一个聪明的办法做到这一点不增加这样的文件(我不得不版本控制吧)我的源?

谢谢!


解决方案

 的#define conflicting_typedef aaa_conflicting_typedef
#包括aaa.h
和#undef conflicting_typedef
#定义conflicting_typedef bbb_conflicting_typedef
#包括bbb.h
和#undef conflicting_typedef
#定义conflicting_typedef暧昧使用aaa_conflicting_typedef或bbb_conflicting_typedef

如果您需要,请参照的typedef aaa.h ,您可以使用名称 aaa_conflicting_typedef ;从 bbb.h 参照的typedef ,使用的名称 bbb_conflicting_typedef 。如果(这是很可能的情况下)你不需要来指的typedef 在code,那么你是好去。最后的的#define 确保您不会使用普通的名称;您必须使用正确映射名称。

显然,这不应该被重复;如果有多个文件需要此节,你把它变成自己的头aaa_bbb.h,并​​包含在您需要的节。你甚至可以检测出是否为 aaa.h bbb.h 已被列入并拒绝编译如果它有(如果他们之后,这包括你无法检测,但他们一直在你的控制之下包含一次的事实意味着他们不应该再包括在内)。

如果这是C ++,你不得不因为类型安全联动一个更大的问题。 OTOH,库厂商那么这两个有罪不正确的名称空间的控制。

I have gotten 2 header files that I include that both have typedef of the same name.

Let's say aaa.h and bbb.h. They came from 2 unrelated 3rd party packages and I have no control of these 2 files but have to use them. I don't include both file directly but they are rather sub-included in the top level header file. (ie. I include a.h and b.h which respectively included them)

My program wont compile when they are both included because of the name conflict.

So my temporary solution is I copied aaa.h into my source tree with only the typedef removed, called myaaa.h. At the top of the file I keep the "#ifndef AAA_H, #define AAA_H" wrapper intact so when I include myaaa.h, aaa.h would not be included because the AAA_H flag is already defined, indirectly removed the typedef conflict.

Is there a cleverer way to do this without adding such file (and I have to version control it) to my source?

Thanks!

解决方案

#define conflicting_typedef aaa_conflicting_typedef
#include "aaa.h"
#undef  conflicting_typedef
#define conflicting_typedef bbb_conflicting_typedef
#include "bbb.h"
#undef  conflicting_typedef
#define conflicting_typedef ambiguous use aaa_conflicting_typedef or bbb_conflicting_typedef

If you need to refer to the typedef from aaa.h, you can use the name aaa_conflicting_typedef; to refer to the typedef from bbb.h, use the name bbb_conflicting_typedef. If (as is probably the case) you don't need to refer to either typedef in your code, then you're good to go. The final #define ensures that you don't use the plain name; you must use the correct mapped name.

Clearly, this should not be repeated; if there's more than one file that needs this stanza, you put it into your own header "aaa_bbb.h" and include that where you need the stanza. You can even detect whether either aaa.h or bbb.h has already been included and reject the compilation if it has (you can't detect if they're included after this, but the fact that they've been included once under your control means they should not be included again).

If this was C++, you'd have a bigger problem because of type-safe linkage. OTOH, the library vendors are then both guilty of improper name space control.

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

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