UNIX網絡編程卷2 源碼編譯篇

   W. Richard Stevens的主頁
php

   源代碼下載   >>  ~/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源碼編譯已經完成。
相關文章
相關標籤/搜索