【问题标题】:C compiler errors on TUN/TAPTUN/TAP 上的 C 编译器错误
【发布时间】:2013-11-18 23:44:12
【问题描述】:

我喜欢用 C++ 程序创建一个 TUN/TAP 接口。我在网上找到了一个直截了当的教程 http://backreference.org/2010/03/26/tuntap-interface-tutorial/.

问题是,我似乎对 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(在这种情况下,从命令行不带任何开关)。 我的库有什么问题?

【问题讨论】:

  • 尝试了(gcc 和 .c 文件扩展名以及 g++ 和 .cpp 文件扩展名),结果相同。

标签: c++ c linux tun


【解决方案1】:

尝试不同的包含。使用...

#include <net/if.h>

...而不是...

#include <linux/if.h>

另外,包含另一个文件。

#include <fcntl.h>

【讨论】:

  • 解决了一半(不完整的类型),谢谢。编译器仍然无法识别 0_RDWR 和 open()。有趣的是,当使用 eclipse 编码时,语法高亮器确实识别它并告诉使用正确的参数。
  • @Marste :查看更新的答案。您确实应该只选择一种 语言来使用,C 或C++。您需要学习查找程序的依赖项。在源代码级别,它将被包含,但在链接时您也会遇到共享对象和库。了解所有部件的来源以及何时需要它们对于成为一名高效的程序员至关重要。
  • 非常感谢,成功了。我正在用 C++ 对整个项目进行编程,我只是认为在这种特殊情况下它没有任何区别。但你是对的,正如你显然注意到的那样,编程不是我的主要工作(我是网络专家),所以有很多事情要习惯。
猜你喜欢
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 2011-01-11
相关资源
最近更新 更多