linux/if.h 和 net/if.h 有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8755030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:56:23  来源:igfitidea点击:

What's wrong with linux/if.h and net/if.h?

linux

提问by u1105178

In my project, I include pfring.h, but compile error: some functions in net/if.h and linux/if.h are redefinition. I found that the pfring.h include linux/if.h So, I test a program, my test code:

在我的项目中,我包含了pfring.h,但是编译错误:net/if.h 和linux/if.h 中的一些函数被重新定义。我发现 pfring.h 包含 linux/if.h 所以,我测试了一个程序,我的测试代码:

#include <linux/if.h>
#include <net/if.h>

int main(void) {
    return 0;
}

It expected compile error. So, what's wrong with linux/if.h and net/if.h ? Can not I include them at once?

它预计编译错误。那么, linux/if.h 和 net/if.h 有什么问题?我不能一次包括它们吗?



error message:

错误信息:

In file included from test.c:1:0:
/usr/include/linux/if.h:178:19: error: field 'ifru_addr' has incomplete type
/usr/include/linux/if.h:179:19: error: field 'ifru_dstaddr' has incomplete type
/usr/include/linux/if.h:180:19: error: field 'ifru_broadaddr' has incomplete type
/usr/include/linux/if.h:181:19: error: field 'ifru_netmask' has incomplete type
/usr/include/linux/if.h:182:20: error: field 'ifru_hwaddr' has incomplete type
In file included from test.c:2:0:
/usr/include/net/if.h:45:5: error: expected identifier before numeric constant
/usr/include/net/if.h:112:8: error: redefinition of 'struct ifmap'
/usr/include/linux/if.h:136:8: note: originally defined here
/usr/include/net/if.h:127:8: error: redefinition of 'struct ifreq'
/usr/include/linux/if.h:170:8: note: originally defined here
/usr/include/net/if.h:177:8: error: redefinition of 'struct ifconf'
/usr/include/linux/if.h:219:8: note: originally defined here

采纳答案by u1105178

This problem has been resolved, add the compile flag -DHAVE_PCAP is fix. ;-)

此问题已解决,添加编译标志 -DHAVE_PCAP 已修复。;-)

回答by Dmitry

For me (on Ubuntu 12.04 x64) the following include solved the problem:

对我来说(在 Ubuntu 12.04 x64 上)以下包括解决了这个问题:

#include <sys/socket.h> // <-- This one
#include <linux/if.h>
#include <linux/if_tun.h>

回答by A. Binzxxxxxx

At first let us talk about the source: The header files are from different Packages as you can see asking dpkg.

首先让我们谈谈来源:头文件来自不同的包,你可以看到询问dpkg.

$ dpkg -S /usr/include/linux/if.h
linux-libc-dev:i386: /usr/include/linux/if.h
$ dpkg -S /usr/include/net/if.h
libc6-dev:i386: /usr/include/net/if.h

linux-libc-devis part of linux kernel packages while libc6-devis part of the libc6(Standard C library in version 6).

linux-libc-dev是 linux 内核包libc6-dev的一部分,同时是libc6(版本 6 中的标准 C 库)的一部分。

It seams like they are interchangeable so you should only use one (not 100% sure about this). If you pick linux/if.h, you may depend on Kernel versions with your compiled binary.

看起来它们是可以互换的,所以你应该只使用一个(不是 100% 确定)。如果你选择linux/if.h,你可能依赖于你编译的二进制文件的内核版本。

All new Library versions I have in mind stick with net/if.hinstead of the linux one - so you should do the same.

我想到的所有新库版本都坚持使用net/if.h而不是 linux 版本 - 所以你应该这样做。

回答by user31986

If you are using one of the interface state flags (eg: IFF_UP, etc.), you need one more header than mentioned in other posts.

如果您正在使用接口状态标志之一(例如:IFF_UP 等),您需要一个比其他帖子中提到的更多的标头。

#include <sys/types.h>   // <== 
#include <sys/socket.h>
#include <linux/if.h>