系统文件的结构重定义问题
在 Mac OS X 上,我的代码中包含 /usr/include/net/bpf.h 和 /usr/include/pcap/bpf.h。现在,这两个都定义了一些相同的结构,因此我收到重新定义错误。我需要这两个文件,因为它们都有我需要的其他代码。 我的问题是, 如何在我的代码中实现此功能而无需修改任何系统文件?有没有办法让我在代码中执行此操作,而不必简单地创建此头文件的副本并使用它?
我不想修改这些文件,因为还有其他应用程序仅包含这两个文件之一,并且需要定义结构。 提前致谢。
On Mac OS X, I have /usr/include/net/bpf.h and /usr/include/pcap/bpf.h included in my code. Now both of these have some of the same structures defined so I get a redefinition error. I need these both files as they both have some other code that I need.
My question is,
how do I get this working in my code without having to modify either system file ? Is there a way for me to do this in my code without having to simply create a copy of this header file and use that instead ?
I don't want to modify these files as there are other app's that include only one of these 2 file's and would need the structure's defined.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
某些头文件针对此类情况专门设置了
#define
。例如在windows中,如果你想使用winsock2并且有windows.h,你需要这样做:否则windows.h中包含的winsock1的定义将与winsock2冲突。但我怀疑
pbf.h
是否属于这种情况。这种模式在 UNIX 中并不常见。最后的选择是决定您需要更多的头文件,删除第二个头文件并手动添加您缺少的定义,然后从第二个头文件复制粘贴它。您可能想要编写一个小脚本,根据函数名称为您进行复制粘贴,这样它就不会那么脆弱。
Some header files have
#define
s in place for exactly such cases. For instance in windows, if you want to use winsock2 and have windows.h you need to do:Otherwise the definitions of winsock1 which are included in windows.h would clash with winsock2. I doubt however that this is the case with
pbf.h
. this patten doesn't occur so much in unix.The last resort options is to decide which header file you need more of, remove the second one and add the definition you're missing by hand,y copy-pasting it from the second. You might want to write a small script that does the copy-pasting for you based on the names of the functions so it won't be as fragile.