要开始接触网络编程,用的是大名鼎鼎的Unix Network Programming这本书,第一个例子就遇到困难了,unp.h没有,到谷歌上搜索了一下,找到了一些方法。先下载unpv13e.tar.gz,
1.解压下载的unpv13e.tar.gz文件
- tar
-zxvf unpv13e.tar.gz
2。终端中进入unpv13e目录(需要给configure文件增加x权限)
- ./configure
- cd
lib - make
- cd
../libfree # continue building the basic library - make
如若出现以下问题:
- gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o inet_ntop.o inet_ntop.c
- inet_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
gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o inet_ntop.o inet_ntop.c
inet_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.c
- ar rv ../libunp.a in_cksum.o inet_ntop.o inet_pton.o
- a - in_cksum.o
- a - inet_ntop.o
- a - inet_pton.o
- ranlib ../libunp.a
>~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.c
ar rv ../libunp.a in_cksum.o inet_ntop.o inet_pton.o
a - in_cksum.o
a - inet_ntop.o
a - inet_pton.o
ranlib ../libunp.a
至此,编译成功生成静态库libunp.a
然后在unpv13e文件夹中成功生成libunp.a文件
3.将生成的libunp.a静态库复制到/usr/lib/中
- cd
.. - sudo
cp libunp.a /usr/lib
4,修改unp.h并将其和config.h拷贝到/usr/include中,为了以后include方便
- vim
lib/unp.h //将unp.h中#include "../config.h"修改为#include "config.h"
- sudo
cp lib/unp.h /usr/include - sudo
cp config.h /usr/include
这里复制到/usr/include目录后,非root用户无权查看,记得chmod 744 一下