1. 程式人生 > >Linux網路程式設計(1):如何使用"unp.h"

Linux網路程式設計(1):如何使用"unp.h"

俗話說萬事開頭難,學習新知識也是如此,當我們下定決心要實現UNP中的例子時,發現卻無法將程式部署上去,這種感覺是不是很令人沮喪?本文就是用來給我自己這種linux菜鳥掃盲用的。

首先,UNP的原始碼連結為 點選開啟連結,下載完成後使用tar -zxvf命令進行解壓。

$ tar -zxvf unpv13e.tar.gz
解壓完成後,進入該目錄。
$ cd unpv13e
對於初學者,一看到軟體包裡這麼多的檔案,肯定會一頭霧水,這尼瑪都是用來幹啥的?不急,第一件事情就是去讀一下README。
Execute the following from the src/ directory:

    ./configure    # try to figure out all implementation differences

    cd lib         # build the basic library that all programs need
    make           # use "gmake" everywhere on BSD/OS systems

    cd ../libfree  # continue building the basic library
    make

    cd ../libroute # only if your system supports 4.4BSD style routing sockets
    make           # only if your system supports 4.4BSD style routing sockets

    cd ../libxti   # only if your system supports XTI
    make           # only if your system supports XTI

    cd ../intro    # build and test a basic client program
    make daytimetcpcli
    ./daytimetcpcli 127.0.0.1

If all that works, you're all set to start compiling individual programs.

Notice that all the source code assumes tabs every 4 columns, not 8.

第一步當然是./configure來檢查系統檔案了,等等!這怎麼這麼像用原始碼安裝檔案,我們不是隻用使用unp.h這個標頭檔案麼?好吧,大牛的標頭檔案怎麼能和菜鳥心目中簡陋的標頭檔案一樣呢?

然後按照README的步驟一步一步的來,我只編譯了lib下面的base庫,當make完成之後,我們會發現make完成後提示

ranlib ../libunp.a

libunp.a?這是啥玩意,對,這個是靜態連結檔案。這時候有人肯定會疑問:靜態連結是用來幹啥的?這就複雜了,給大家個提示,百度”從程式碼到可執行檔案的過程“,”動態連結與靜態連結的區別“。接下來,進入advio資料夾,嘗試著像readme所說的,make daytimetcpcli,我們會發現make的過程如下:
gcc -I../lib -g -O2 -D_REENTRANT -Wall -o daytimetcpcli daytimetcpcli.o ../libunp.a -lpthread

聰明如你立馬就從這個命令裡面想到了怎麼樣在自己新建的資料夾裡使用gcc編譯unp裡面的例子了吧?

gcc -o daytimetcpcli(可執行檔案) daytimetcpcli.c(C原始檔) /usr/lib/libunp.a(我自己的libunp.a的存放位置),再依據config.h和unp.h的具體位置,去編輯unp.h的#include ”../config.h",至此我開心地敲開了Unix網路程式設計的大門。