unix网络编程环境搭建及使用方法

课本倒是能看懂,但一上手就有点懵了,碰到各种各样的问题,可能还是对linux不是很熟悉吧。下面把流程一步一步写下来,以便更好的学习。


1:从图灵网上下载到源代码,解压到linux工作目录下,会看到各种目录和文件,就是书本中提到的目录及源文件。

2:安装编译器,为了齐全还是安装一下build-essential

sudo apt-get install build-essential

3:要看解压后目录下的READEME,里面有教你一些步骤,让你配置自己的工作环境:

(1)在终端中进入upnv13e目录,执行

chmod u+x configure
./configure  
(2)进入lib目录执行make

cd lib  
make

(3)建立基础类库

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

(4)编译函数库

cd ../libgai   # the getaddrinfo() and getnameinfo() functions  
make  

4:将生成的libunp.a静态库复制到/usr/lib/和/usr/lib64/中

cd ..   //回到unpv12e目录  
sudo cp libunp.a /usr/lib  
sudo cp libunp.a /usr/lib64  

5:修改unp.h并将其和config.h拷贝到/usr/include中,为了以后include方便

gedit lib/unp.h   //将unp.h中#include "../config.h"修改为#include "config.h"  
sudo cp lib/unp.h /usr/include  
sudo cp config.h /usr/include  

以上我们的环境就搭建好了。但是还是会碰到一些问题。

编译我们的源代码:

gcc daytimetcpcli.c -o daytimetcpcli

此时会提示:

undefined reference to 'err_quit'
undefined reference to 'err_sys'

解决方法:

法1:当gcc命令时,使用-l参数

gcc daytimetcpcli.c -o daytimetcpcli -lunp  

详细的介绍见:GCC 命令详解:http://blog.csdn.net/a45872055555/article/details/26731521

法2:上面的函数err_quit等在error.c文件中详细定义,将error.c放入/usr/include中;并修改unp.h文件,在最后一行#endif前面添加

#include "error.c"
然后复制到/usr/include中


法3:将error.c文件放到要编译的源文件同一目录下,用gcc编译时一同编译

gcc daytimetcpcli.c error.c -o daytimetcpcli

另外

在进行unix进程通信的安装中遇到

[szu@localhost lib]$ make
gcc -g -O2 -D_REENTRANT -Wall -D_POSIX_PTHREAD_SEMANTICS   -c -o daemon_inetd.o daemon_inetd.c
In file included from /usr/include/netinet/in.h:25,
                 from /usr/include/rpc/types.h:91,
                 from /usr/include/rpc/rpc.h:39,
                 from unpipc.h:116,
                 from daemon_inetd.c:2:
/usr/include/stdint.h:49: 错误:重复的‘unsigned’
/usr/include/stdint.h:49: 错误:声明指定了两个以上的数据类型
/usr/include/stdint.h:50: 错误:重复的‘unsigned’
/usr/include/stdint.h:50: 错误:重复的‘short’
/usr/include/stdint.h:52: 错误:重复的‘unsigned’
/usr/include/stdint.h:52: 错误:声明指定了两个以上的数据类型
make: *** [daemon_inetd.o] 错误 1


只要在config.h中注释掉在源代码文件夹中,运./configure进行配置后,修改源文件夹下的config.h文件,注释掉此文件中的56,57,58行关于多种类型的定义,保存后再进入lib子目录中执行make命令进行编译即可。

56//#define       uint8_t unsigned char                               /* <sys/types.h> */
 57 //#define       uint16_t unsigned short                             /* <sys/types.h> */
 58 //#define       uint32_t unsigned int 


接下来应该和unix环境编程一样环境一样从4步骤开始的步骤一样拷贝静态库函数和头文件等等!

但是编译的时候格式是:

gcc constumer.c -lunpipc -lrt -lpthread    (我是在他的/home/szu/unixPIC/unpv22e/中的Make.defines的到提示的。)

应该就通过编译了:

[szu@localhost jincheng]$ ./a.out 10000 5
count[0] = 0
count[1] = 0
count[2] = 116
count[3] = 9882
count[4] = 2


如果还是不能够成功那么就直接在那个源码里面测试了。如:/home/szu/unixPIC/unpv22e/mutex

对里面的代码进行编译  make  然后运行。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值