系统文件的结构重定义问题

发布于 2024-11-16 13:20:24 字数 259 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明明#如月 2024-11-23 13:20:24

某些头文件针对此类情况专门设置了#define。例如在windows中,如果你想使用winsock2并且有windows.h,你需要这样做:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>

否则windows.h中包含的winsock1的定义将与winsock2冲突。但我怀疑 pbf.h 是否属于这种情况。这种模式在 UNIX 中并不常见。

最后的选择是决定您需要更多的头文件,删除第二个头文件并手动添加您缺少的定义,然后从第二个头文件复制粘贴它。您可能想要编写一个小脚本,根据函数名称为您进行复制粘贴,这样它就不会那么脆弱。

Some header files have #defines in place for exactly such cases. For instance in windows, if you want to use winsock2 and have windows.h you need to do:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文