linux tcp 编程中头文件引用导致的惨案
2014-07-04 14:17
183 查看
今天在linux下编写了一个 多线程的 tcp server 的 demo,发现了一个奇怪的问题。
我在使用 char *inet_ntoa(struct in_addr in); 这个方法时,编译时出现一个警告,执行的时候程序崩溃,莫名其妙,检查半天,没有发现问题,网上查找,别人都是这么用的。
调试后发现是调用inet_ntoa() 函数造成的 Segmentation fault (core dumped)
查看函数说明, man inet_ntoa 显示该函数的原型为: char *inet_ntoa(struct in_addr in); 说明如下。
说明里给出了一段关于该函数的用法的 代码,如下所示:
./inet_addr 192.168.1.1
输出结果
192.168.1.1
一切正常,非常疑惑,然后开始检查两个程序的区别,发现不是用法问题,最后检查头问题,发现我的程序里面没有 include <arpa/inet.h> 这个头文件,试着在上面的代码中注释掉第二行的头文件,发现出现一样的警告,执行也出现 segmentation fault,看来是头文件问题。
我的使用系统是 ubuntu 14.04,gcc version 4.8.2,安装了很多编译工具。
然后我换用另外一台 ubuntu 12.04系统的机子,gcc version 4.6.3, 重复上面的过程,发现注释掉 arpa/inet.h 头文件后,编译无法通过,提示:
补充:
查找头文件,在 inet.h中定义了
我在使用 char *inet_ntoa(struct in_addr in); 这个方法时,编译时出现一个警告,执行的时候程序崩溃,莫名其妙,检查半天,没有发现问题,网上查找,别人都是这么用的。
inet_addr.c:26:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=] printf("%s\n", inet_ntoa(addr)); ^
调试后发现是调用inet_ntoa() 函数造成的 Segmentation fault (core dumped)
查看函数说明, man inet_ntoa 显示该函数的原型为: char *inet_ntoa(struct in_addr in); 说明如下。
The inet_ntoa() function converts the Internet host address in, given in network byte order, to a string in IPv4 dotted-decimal notation. The string is returned in a statically allocated buffer, which subsequent calls will overwrite.
说明里给出了一段关于该函数的用法的 代码,如下所示:
#define _BSD_SOURCE #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { struct in_addr addr; if (argc != 2) { fprintf(stderr, "%s <dotted-address>\n", argv[0]); exit(EXIT_FAILURE); } if (inet_aton(argv[1], &addr) == 0) { perror("inet_aton"); exit(EXIT_FAILURE); } printf("%s\n", inet_ntoa(addr)); exit(EXIT_SUCCESS); }决定测试这段代码,编译,没有错误,也没有警告,运行
./inet_addr 192.168.1.1
输出结果
192.168.1.1
一切正常,非常疑惑,然后开始检查两个程序的区别,发现不是用法问题,最后检查头问题,发现我的程序里面没有 include <arpa/inet.h> 这个头文件,试着在上面的代码中注释掉第二行的头文件,发现出现一样的警告,执行也出现 segmentation fault,看来是头文件问题。
我的使用系统是 ubuntu 14.04,gcc version 4.8.2,安装了很多编译工具。
然后我换用另外一台 ubuntu 12.04系统的机子,gcc version 4.6.3, 重复上面的过程,发现注释掉 arpa/inet.h 头文件后,编译无法通过,提示:
error: storage size of ‘addr’ isn’t known搞了半天是我系统环境问题,导致我没有引用 inet.h这个头文件也编译通过了,只是给出一个警告。还没查到具体是哪个地方的原因。
补充:
查找头文件,在 inet.h中定义了
typedef uint32_t in_addr_t; struct in_addr{ in_addr_t s_addr; }实际上 in_addr 为 32位无符号整数,unsigned int ,修改代码,自己声明这个结构体,注释掉所有头文件,赋值addr.s_addr = 16885952, 这是之前用 192.168.1.1转换过来的,发现程序还是能编译通过,运行时出现 segmentation fault。问题原因依旧没有找到。
相关文章推荐
- 头文件引用缺少导致link错误[zt]
- 【编程好习惯】永远将头文件作为定义和引用的桥梁
- FreeMarker的FTL页面中include引用UTF-8文件导致错位的问题处理
- 遇到问题---java---myeclipse中maven项目引用另一个导致的resource文件混乱的问题
- php编程中require和include多层嵌套导致文件找不到的错误
- Eclipse编程Tips(1)-图像资源文件错误导致启动失败
- 引用js文件导致“Uncaught SyntaxError: Unexpected token <”的问题
- 编程时如果只是调用其它程序打开文件不必引用程序dll,调用系统shell执行更方便
- 解决引用相同jar文件导致的错误
- AndroidStudio 多个第三方so文件重复引用导致 java.lang.UnsatisfiedLinkError 问题
- 头文件交叉引用导致编译出错
- Hibernate的一个奇怪报错,原来是工程下.classpath文件中jar包的引用顺序导致的
- css文件引用后变成了乱码导致样式不能显示的问题的解决
- Android引用百度定位API第三方组件后导致其它.so文件无法正常加载的问题
- 转:C#编程读取Excel文件,引用Microsoft.Office.Interop.Excel出现的问题
- [搬家文]css文件的编码导致页面引用已定义的样式无效
- 解决android工程引用多个jar包导致的文件重复的错误
- FreeMarker的FTL页面中include引用UTF-8文件导致错位的问题处理
- 浅析CSS编程的外部CSS文件引用方法
- JQuery文件重复引用问题导致某些函数调用时出现undefined