UNIX网络编程卷2 源码编译篇

   W. Richard Stevens的主页

   源代码下载   >>  ~/Downloads/unpv22e.tar.gz;

1 tar -xzfv unpv22e.tar.gz
2 cd unpv22e
3 ./configure
4 cd lib
5 make

 

  make编译失败,因为需要对两个文件修改,unpv22e/config.h和unpv22e/wrapunix.c。
 1 vi   config.h  
 2 
 3 /*注释掉这三行*/
 4 // #define uint8_t unsigned char /* <sys/types.h> */  
 5 // #define uint16_t unsigned short /* <sys/types.h> */  
 6 // #define uint32_t unsigned int /* <sys/types.h> */ 
 7 
 8 /*添加MSG_R和MSG_W定义*/
 9 typedef unsigned long ulong_t;  
10 #define MSG_R 0400  
11 #define MSG_W 0200 
12 
13 添加_GNU_SOURCE定义
14 vi config.h  
15 #define _GNU_SOURCE 
16 
17 
18 vi   lib/wrapunix.c
19 
20 /*编译warpunix.c,使用mkstemp函数替换mktemp函数*/
21 
22 void  
23 Mktemp(char *template)  
24 {  
25      if (mkstemp(template) == NULL || template[0] == 0)  
26          err_quit("mktemp error");  
27  } 
   再次执行make命令,会在unp22e目录下生成静态库文件libunpipc.a和在lib目录下生成目标文件*.o。
1 /* 编译生成libunpipc.a */
2  cd lib  
3  make 
  将config.h和pipe目录下的unpipc.h 复制到/usr/include/目录下,并修改unpipc.h下面的#include "../config.h" 为 #include "config.h".(备注:由于考虑到UNIX网络编程卷1也把config.h拷贝到/usr/include目录中,所以需要把卷2的config.h改为其他名字,例如ipcconfig.h。同时也需要修改/pipe/unpipc.h文件中"./ipcconfig.h")
 1 cd unp22e
 2 mv config.h ipcconfig.h
 3 
 4 vi pipe/unpipc.h
 5 /*unpipc.h修改内容*/
 6 #include "./ipconfig.h"
 7 
 8 /*复制libunpipc.a ipcconfig.h unpipc.h*/
 9 sudo cp ipcconfig.h pipe/unpipc.h  /usr/include
10 sudo cp libunpipc.a  /usr/lib
  一切准备工作就绪,那就让我们来编译书本中的第一个例子mainpipe.c
1 cd pipe
2 
3 gcc mainpipe.c server.c client.c -lunpipc -o mainpipe.out
  编译失败,缺少对应库文件,加上 -lrt编译选项即可。
1 gcc mainpipe.c server.c client.c -lunpipc -rt -o mainpipe.out
  编译失败提示:undefined reference to symbol 'sem_timedwait@@GLIBC_2.2.5'。google之答案,原来需要线程苦支持,加上-lpthread选项。
 1 gcc mainpipe.c server.c client.c -lunpipc -rt -l pthread -o mainpipe.out
 2 
 3 ./mainnpipe.out
 4 
 5 /*输入文件名*/
 6 mainpipe.c
 7 #include "unpipc.h"
 8 void client(int, int), server(int, int);
 9 
10 int
11 main(int argc, char **argv)
12 {
13         int     pipe1[2], pipe2[2];
14         pid_t    childpid;
15 
16     Pipe(pipe1);    /* create two pipes */
17     Pipe(pipe2);
18 
19     if ( (childpid = Fork()) == 0) {        /* child */
20         Close(pipe1[1]);
21         Close(pipe2[0]);
22 
23         server(pipe1[0], pipe2[1]);
24         exit(0);
25     }
26         /* 4parent */
27     Close(pipe1[0]);
28     Close(pipe2[1]);
29 
30     client(pipe2[0], pipe1[1]);
31 
32     Waitpid(childpid, NULL, 0);        /* wait for child to terminate */
33     exit(0);
34 }
35 
36 
37      
  至此,UNIX网络编程卷2源码编译已经完成。
 
posted @ 2014-05-19 23:07  solaire  阅读(1188)  评论(0编辑  收藏  举报