《Unix网络编程(第3版)》代码编译的一些问题

现在学习《UNIX网络编程(第3版)》一书,书中源代码有一些默认情况下编译不能通过,要经过一些修改都行。这编文档将记录下我遇到的不能正常编译的程序的修改步骤。

28章:traceroute程序:

$ make
gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o icmpcode_v6.o icmpcode_v6.c
icmpcode_v6.c: In function ‘icmpcode_v6’:
icmpcode_v6.c:13: error: ‘ICMP6_DST_UNREACH_NOTNEIGHBOR’ undeclared (first use in this function)
icmpcode_v6.c:13: error: (Each undeclared identifier is reported only once
icmpcode_v6.c:13: error: for each function it appears in.)
make: *** [icmpcode_v6.o] Error 1

在网上查了半天都没有找到ICMP6_DST_UNREACH_NOTNEIGHBOR的信息,直接把icmpcode_v6中相关的13~14行注释好了。

$ make
gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o main.o main.c
gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o icmpcode_v4.o icmpcode_v4.c
gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o icmpcode_v6.o icmpcode_v6.c
gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o recv_v4.o recv_v4.c
recv_v4.c: In function ‘recv_v4’:
recv_v4.c:55: error: ‘struct udphdr’ has no member named ‘uh_sport’
recv_v4.c:56: error: ‘struct udphdr’ has no member named ‘uh_dport’
recv_v4.c:72: error: ‘struct udphdr’ has no member named ‘uh_sport’
recv_v4.c:73: error: ‘struct udphdr’ has no member named ‘uh_dport’
make: *** [recv_v4.o] Error 1

提示udphdr结构没有uh_[sd]port成员,解决方法是在trace.h文件中增加#define __FAVOR_BSD声明。注意要加在4个netinet头文件之前,unp.h头文件之后。就像这样:

#include    "unp.h"
#define __FAVOR_BSD
#include    <netinet/in_systm.h>
分享到