您的位置:首页 > 理论基础 > 计算机网络

Linux网络编程(1):如何使用"unp.h"

2014-06-07 01:08 435 查看
俗话说万事开头难,学习新知识也是如此,当我们下定决心要实现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网络编程的大门。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: