阅读unix网络编程(卷一)第一章demo

首先根据书上,在书上对应的网址下载到源码包。

season@season-PC:/storage/unix$ ./configure

根据readme的指示,编译库文件

season@season-PC:/storage/unix$ cd lib
season@season-PC:/storage/unix$ make

巴拉巴拉一大堆,最后一行为:ranlib ../libunp.a 说明成功了之后继续编译:

season@season-PC:/storage/unix: cd ../libfree
season@season-PC:/storage/unix/libfree : make

如果爆出下面错误:

gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o in_cksum.o in_cksum.c
gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o inet_ntop.o inet_ntop.c
inet_ntop.c: In function ‘inet_ntop’:
inet_ntop.c:60:9: error: argument ‘size’ doesn’t match prototype
size_t size;
^In file included from inet_ntop.c:27:0:/usr/include/arpa/inet.h:64:20: error: prototype declaration
extern const char *inet_ntop (int __af, const void *__restrict __cp,
^
: recipe for target 'inet_ntop.o' failed
make: *** [inet_ntop.o] Error 1

错误原因可以看出是,类型不对。解决办法 :找到inet_ntop.c第61行 size_t size ->改成 socklen_t size) 也有可能是60行,之后继续make一下。打开lib目录

修改unp.h
    #include"../config.h"

改成 
    #include"config.h"

返回unix文件夹

season@season-PC:sudo cp config.h /usr/local/include
season@season-PC:sudo cp lib/unp.h /usr/local/include
season@season-PC:sudo cp libunp.a /usr/local/lib
season@season-PC:cd intro

到此搭建完毕,测试一下 进入intro文件内

gcc daytimetcpcli.c -o 获取时间

又报以下错误:

/tmp/cciq4Jvt.o:在函数‘main’中:
daytimetcpcli.c:(.text+0x3b):对‘err_quit’未定义的引用
daytimetcpcli.c:(.text+0x6d):对‘err_sys’未定义的引用
daytimetcpcli.c:(.text+0xe5):对‘err_quit’未定义的引用
daytimetcpcli.c:(.text+0x114):对‘err_sys’未定义的引用
daytimetcpcli.c:(.text+0x153):对‘err_sys’未定义的引用
daytimetcpcli.c:(.text+0x196):对‘err_sys’未定义的引用
collect2: error: ld returned 1 exit status

在本目录下make下 成功

season@season-PC:./daytimetcpcli 127.0.0.1
season@season-PC:connect error: Connection refused

又错咯!!继续上搜索引擎,原来是由于某些原因daytime服务并没有启动,可能系统还没有安装这个服务。

season@season-PC:apt-get install xinetd

启用daytime服务,打开 /etc/xinetd.d/daytime 有2个文件修改disable yes 改为 disable no重启服务

season@season-PC:service xinetd restart
season@season-PC:./daytimetcpcli 127.0.0.1
11 SEP 2017 23:02:35 CST

关于单独编译daytimetcpcli.c出错的原因,没有指定需要链接的库。gcc -l参数是用来指定程序要链接的库,-l紧接着就是库名,那么库名跟真正的库文件名有什么关系呢?就拿数学库来说,他的库名是m,他的库文件名是libm.so,容易看出,把库文件名的头lib和尾.so去掉就是库名.解决加入libunp.a库

season@season-PC:gcc daytimetcpcli.c -o 获取时间 -lunp
season@season-PC:./获取时间 127.0.0.1
11 SEP 2017 23:02:35 CST

你可能感兴趣的:(阅读unix网络编程(卷一)第一章demo)