UNIX网络编程书中源代码测试环境搭建 (centos中取时间问题)

今天使用centos搭建了unix网络编程的第一个练习,不停出错,找到了一些资料外加自己调试,现在记录下来


一.我用的是unpv12e.tar,在51cto里面搜索《unix网络编程》上面有;

二.最好专门建立一个文件目录,我的是在/tmp/c/UNIX/unpv12e  ->>建议阅读下README文     件        

三.进入目录,运行./configure

四.进入 cd /tmp/c/UNIX/unpv12e/lib  

                                                 make  //产生错误


   错误代码为:

  1. gcc -g -O2 -D_REENTRANT -Wall   -c -o connect_nonb.o connect_nonb.c  

  2. In file included from connect_nonb.c:1:  

  3. unp.h:114: error: redefinition of ‘struct in_pktinfo’  

  4. make: *** [connect_nonb.o] 错误 1


redefinition of ‘struct in_pktinfo’的解决方法如下:

结构in_pktinfo已经包含在标准头文件bits/in.h中了, 它又通过 netinet/in.h 在unp.h中被包括进来. 只要将 unp.h 中的结构定义comment out注释掉就行了.

注释掉之后重新

make  生成了../libunp.a文件。


五.cd ../libfree

           make


六.cd ../libgai  

           make  

错误提示如下:

gcc -g -O2 -D_REENTRANT -Wall   -c -o getaddrinfo.o getaddrinfo.c  getaddrinfo.c: In function ‘getaddrinfo’:  getaddrinfo.c:58: error: ‘EAI_ADDRFAMILY’ undeclared (first use in this function)  getaddrinfo.c:58: error: (Each undeclared identifier is reported only once  getaddrinfo.c:58: error: for each function it appears in.)  getaddrinfo.c:116: error: ‘EAI_NODATA’ undeclared (first use in this function)  make: *** [getaddrinfo.o] 错误 1

解决方法如下:


回到unpv12e目录:


cd ..  vim configure.in

找到下面一行注释掉
LIBGAI_OBJS="getaddrinfo.o getnameinfo.o freeaddrinfo.o gai_strerror.o"
然后用下面的代码代替注释掉的那行

LIBGAI_OBJS=if test "$ac_cv_func_getaddrinfo" = no ; thenLIBGAI_OBJS="$LIBGAI_OBJS getaddrinfo.o"fiif test "$ac_cv_func_getnameinfo" = no ; thenLIBGAI_OBJS="$LIBGAI_OBJS getnameinfo.o"fiif test "$ac_cv_func_freeaddrinfo" = no ; thenLIBGAI_OBJS="$LIBGAI_OBJS freeaddrinfo.o"fiif test "$ac_cv_func_gai_strerror" = no ; thenLIBGAI_OBJS="$LIBGAI_OBJS gai_strerror.o"fi

autoconf  ./configure  

gedit Make.defines  

在文件末尾加上下面这句:
CFLAGS = -g -O2 -D_REENTRANT -Wall -D_GNU_SOURCE

注意:使用x86_64架构的CPU,make时出现如下提示:

checking build system type... Invalid configuration `x86_64-pc-linux-gnuoldld': machine `x86_64-pc' not recognized
configure: error: /bin/sh ./config.sub x86_64-pc-linux-gnuoldld failed


命令如下:vim /tmp/c/UNIX/unpv12e/config.sub  在其中找到

# Recognize the basic CPU types with company name.
       vax-* | tahoe-* | i[3456]86-* | x86_64-* | i860-* | m68k-* | m68000-* | m88k-*

将红色字段插入以上位置就可进行编译了


七.将生成的文件复制到以下目录:

[root@localhost unpv12e]# cp libunp.a /usr/lib
[root@localhost unpv12e]# cp libunp.a /usr/lib64
[root@localhost unpv12e]# vim lib/unp.h
[root@localhost unpv12e]# cp lib/unp.h /usr/include/
[root@localhost unpv12e]# cp config.h /usr/include/


八.编译,运行,结果

     [root@localhost intro]#  gcc daytimetcpcli.c -o daytimetcpcli -lunp

     [root@localhost intro]# ./daytimetcpcli 127.0.0.1
Mon Aug 12 19:48:23 2013

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章