TUN / TAP上的C编译器错误

我喜欢从C ++程序中创build一个TUN / TAP接口。 我在http://backreference.org/2010/03/26/tuntap-interface-tutorial/上find了一个networking教程。

问题是,我似乎有与if.h和if_tun.h链接问题。 当我将教程剥离到下面的最小示例时,只打开一个插槽,我得到一些错误。 例:

#include <linux/if.h> #include <linux/if_tun.h> int main(void){ char *clonedev = "/dev/net/tun"; open(clonedev,O_RDWR); return 0; } 

如果认为这应该编译,但我得到以下错误:

 /usr/include/linux/if.h:184:19: error: field 'ifru_addr' has incomplete type /usr/include/linux/if.h:185:19: error: field 'ifru_dstaddr' has incomplete type /usr/include/linux/if.h:186:19: error: field 'ifru_broadaddr' has incomplete type /usr/include/linux/if.h:187:19: error: field 'ifru_netmask' has incomplete type /usr/include/linux/if.h:188:20: error: field 'ifru_hwaddr' has incomplete type tuntap.cpp: In function 'int main()': tuntap.cpp:6:18: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings] tuntap.cpp:7:15: error: 'O_RDWR' was not declared in this scope tuntap.cpp:7:21: error: 'open' was not declared in this scope 

我在Linux 3.11.4上使用Fedora 18上的GCC 4.7.2(在这种情况下,没有任何开关的命令行)。 我的图书馆有什么问题?

尝试一个不同的包括。 使用 …

 #include <net/if.h> 

… 代替 …

 #include <linux/if.h> 

另外,包含另一个文件。

 #include <fcntl.h>