问题描述
我正在处理一个大型项目,该项目的文件中有一行A.h
I am working on a huge project which has one file A.h whose code has a line
typedef unsigned __int16 Elf64_Half;
此外,由于我在Linux上构建并使用dlinfo
函数,因此我必须在项目中包含link.h
文件.这就是它造成冲突的地方,因为我有两个具有相同名称Elf64_Half
的typedef. (Linux link.h
包括elftypes.h
,它也具有:typedef unsigned short Elf64_Half;
).
Also since I am building on Linux and using dlinfo
function, I have to include link.h
file in my project. And this is where it creates a conflict because I have two typedefs having the same name Elf64_Half
. (Linux link.h
includes elftypes.h
and it too has: typedef unsigned short Elf64_Half;
).
在这种情况下我该怎么办?我唯一的选择是更改a.h
中的typedef吗?请记住,这并不是一件容易的事,因为该项目非常庞大,我将不得不在几个地方进行更改.
What do I do in such a case? Is the only option I have, to change my typedef in a.h
? Remember it is not too easy because the project is huge and I will have to make a change in several places.
是否可以取消定义 typedef或其他方法?
Is there a way to undef a typedef or something?
推荐答案
为澄清起见,Rahul Manne提供了一个简单的解决方案.做
For clarification, Rahul Manne gave an easy solution. Do
#define Elf64_Half The_Elf64_Half_I_dont_care
#include<link.h>
#undef Elf64_Half
#include<A.h>
/*
* Code here and use Elf64_Half from A.h as you like.
* However, you can't use Elf64_Half from link.h here
* or you have to call it The_Elf64_Half_I_dont_care.
*
*/
这会将link.h
中的每个Elf64_Half
替换为The_Elf64_Half_I_dont_care
,因此您不会与A.h
发生冲突.只要您不想显式使用link.h
的Elf64_Half
,就可以正常工作.您只需要记住link.h
中的Elf64_Half
现在被称为The_Elf64_Half_I_dont_care
,以防您不得不在此文件中显式使用它.
This will substitute each Elf64_Half
in link.h
with The_Elf64_Half_I_dont_care
and thus you get no conflicts with A.h
. As long as you don't want to use Elf64_Half
of link.h
explicitly that will work with no problems. You just have to remember that Elf64_Half
from link.h
is now called The_Elf64_Half_I_dont_care
in case you ever have to use it explicitly in this file.
这篇关于在C ++中取消对typedef的定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!