voi*_*urn 16 c++ linux typedef header-files
我正在开发一个庞大的项目,它有一个文件啊,其代码有一行
typedef unsigned __int16 Elf64_Half;
Run Code Online (Sandbox Code Playgroud)
此外,由于我在Linux上构建并使用dlinfo函数,我必须link.h在我的项目中包含文件.这就是它产生冲突的地方,因为我有两个具有相同名称的typedef Elf64_Half.(Linux link.h包括elftypes.h,它也有:) typedef unsigned short Elf64_Half;.
在这种情况下我该怎么办?我有唯一的选择,改变我的typedef a.h?请记住,这不是太容易,因为项目很大,我将不得不在几个地方做出改变.
有没有办法解开 typedef或什么?
imi*_*mix 17
为了澄清,Rahul Manne给出了一个简单的解决方案.做
#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.
*
*/
Run Code Online (Sandbox Code Playgroud)
这将替换每个Elf64_Halfin link.h,The_Elf64_Half_I_dont_care因此你没有与之冲突A.h.只要你不想使用Elf64_Half的link.h明确,将没有任何问题的工作.您只需要记住,现在调用Elf64_Halffrom ,以防您必须在此文件中显式使用它.link.hThe_Elf64_Half_I_dont_care
在这种情况下我该怎么办?
一个常见的补救措施是将需要最少可见性的那个放在"编译防火墙"后面.也就是说,创建自己的抽象/接口,提供所需的功能,然后将所包含文件的可见性限制为仅*.cpp包括在内*.cpp.当然,*.cpp也不允许该文件包含具有其他定义的标题typedef.
然后声明不会引起冲突,因为它们永远不会对同一个翻译单元可见.
在您的示例中,您可能会创建一个包含所需dlinfo()功能的包装器.为了显示:
DLInfo.hpp
namespace MON {
class DLInfo {
public:
/* ...declare the necessary public/client functionality here... */
int foo();
...
};
}
Run Code Online (Sandbox Code Playgroud)
DLInfo.cpp
#include "DLInfo.hpp"
// include the headers for dlinfo() here.
// these includes should not be in your project's headers
#include <link.h>
#include <dlfcn.h>
// and define your MON::DLInfo implementation here, with full
// ability to use dlinfo():
int MON::DLInfo::foo() {
...
}
...
Run Code Online (Sandbox Code Playgroud)
这是一个小小的工作我想通了:如果你先把它定义为别的东西,那么你可以稍后输入def.请参阅示例中的示例(我使用g ++在OS X上):
#import <iostream>
using namespace std;
typedef unsigned int uint32;
int main() {
cout << sizeof(uint32) << endl;
}
Run Code Online (Sandbox Code Playgroud)
这个程序的输出是
4
Run Code Online (Sandbox Code Playgroud)
现在考虑修改后的程序:
#import <iostream>
using namespace std;
typedef unsigned int uint32;
#define uint32 blah
typedef unsigned long uint32;
int main() {
cout << sizeof(uint32) << endl;
}
Run Code Online (Sandbox Code Playgroud)
这个程序的输出是
8
Run Code Online (Sandbox Code Playgroud)
因此,要回答您的问题,您需要在代码中添加一行:
#define Elf64_Half Elf64_Half_custom
Run Code Online (Sandbox Code Playgroud)
为清楚起见,这是有效的,因为您基本上将它们全部重命名,但是使用单个命令执行此操作,而不必更改所有自己的名称.
| 归档时间: |
|
| 查看次数: |
10172 次 |
| 最近记录: |