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

16

在我的项目中,我包含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;
}

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


错误信息:

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

1
你有什么需要需要同时包含两个东西吗? - mouviciel
2
我包含了<boost/asio.hpp>,它隐式地包含了<net/if.h>。同时,我还包含了<pfring.h>,它隐式地包含了<linux/if.h>。 - superK
4个回答

21

对我而言(在Ubuntu 12.04 x64上),以下的 include 解决了这个问题:

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

阿什温的帖子http://algorithmicallyrandom.blogspot.sg/2012/07/error-on-including-include.html @hfmanson - Zhang LongQI

3
这个问题已经解决了,添加编译标志“-DHAVE_PCAP”即可解决。;-)

我如何在不使用make文件的情况下,只是简单地在cli上运行g++ -o myapp myapp.cpp时应用此编译标志? - jwbensley
1
@jwbensley 只需使用 g++ -o myapp myapp.cpp -DHAVE_PCAP。顺便说一下,在 boost 1.55 和 pfring 5.6.1 中,不会再出现这种冲突。 - superK

2

首先我们来谈谈源代码:正如你所看到的,头文件来自不同的软件包,可以通过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-dev是Linux内核包的一部分,而libc6-devlibc6(版本6中的标准C库)的一部分。

它们似乎可以互换使用,因此您应该只使用其中一个(不确定是否100%正确)。如果选择linux/if.h,则可能会依赖于已编译二进制文件的内核版本。

我所知道的所有新库版本都使用net/if.h而不是Linux版本 - 因此您应该也这样做。


1

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接