學習unp網路編程,樹上的例子均存在#include "unp.h",故需要對環境進行配置。
1. 到資源頁下載unpv13e
2. 解壓並將unpv13e 移動到相應的檔案夾下
3. 編譯
>$ cd unpv13e >~unpv13e/$ ./configure>~unpv13e/$ cd lib>~unpv13e/lib/$ make>~unpv13e/lib/$ cd ../libfree>~unpv13e/libfree/$ make
如若出現以下問題:
gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o inet_ntop.o inet_ntop.cinet_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, ^make: *** [inet_ntop.o] Error 1
則需要:
>~unpv13e/libfree/$ vim inet_ntop.c //將第60行的 size_t size 改成 socklen_t size;>~unpv13e/libfree/$ make // 改變完後重新make>~unpv13e/libfree/$ cd ../libgai>~unpv13e/libgai/$ make //以下只是warning, 乎略之/usr/include/arpa/inet.h: In function ‘inet_ntop’:inet_ntop.c:152:23: warning: ‘best.len’ may be used uninitialized in this function [-Wmaybe-uninitialized] if (best.base == -1 || cur.len > best.len) ^inet_ntop.c:123:28: note: ‘best.len’ was declared here struct { int base, len; } best, cur; ^gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o inet_pton.o inet_pton.car rv ../libunp.a in_cksum.o inet_ntop.o inet_pton.oa - in_cksum.oa - inet_ntop.oa - inet_pton.oranlib ../libunp.a
至此,編譯成功產生靜態庫libunp.a
4. 將產生的libunp.a複製到/usr/lib下
>~unpv13e/libgai/$ cd ..>~unpv13e/$ sudo cp libunp.a /usr/lib
5. 修改unpv13e/lib/unp.h並複製
>~unpv13e/$ vim lib/unp.h // 將#include "../config.h" 改成 #include "config.h">~unpv13e/$ sudo cp lib/unp.h /usr/include>~unpv13e/$ sudo cp config.h /usr/include
6. 編譯例子
>~unpv13e/$ cd intro>~unpv13e/$ gcc daytimetcpcli.c -o cli -lunp
編譯成功則大功告成。