《Unix NetWork Programming》(Unix网络编程)—环境搭建(解决unp.h等源码编译问题)
下面开始用简单但典型的客户端和服务器端程序说明如何进行网络编程。这一小节讲的是客户端,一个用来连接并读取服务器发送来的时间的客户端。
这里涉及到了编写代码,因此要
搭建《unix网络编程》那本书所需的环境
我用的是ubuntu11.10系统:
1,安装编译器,为了齐全还是安装一下build-essential
- sudo apt-get install build-essential
2,下载本书的头文件及示例源码
点此下载unpv13e.tar.gz http://ishare.iask.sina.com.cn/f/13238521.html
3,解压unpv13e.tar.gz后进入目录查看README,然后可以按照里面提示操作,不过会遇到问题
第一步:在终端中进入upnv13e目录,然后执行代码:
- chmod u+x configure
- ./configure
第二步:进入lib目录下执行make:
- cd lib
- make
第三步:建立基础类库:
- cd ../libfree # continue building the basic library
- make
第四步:编译函数库:
- 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
- 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
6,编译源代码
- cd ./intro
- gcc daytimetcpcli.c -o daytimetcpcli -lunp
解决方法:daytimetcpcli.c:1:17: 致命错误: /usr/include/unp.h:权限不够
2.进入unpv13e解压目录下的lib文件里面找到unp.h,然后右键查看属性,选中权限,修改它的权限为读写,或只读sudo rm /usr/include/unp.h
ok,按照第6步的命令 重新编译,pass,成功了sudo cp lib/unp.h /usr/include
跟普通的编译不一样的是要在最后加上刚才那个链接库,-l参数加上刚才那个libunp.a去掉lib和后面的.a。最后得到参数-lunp。
7,编写程序
以后编写完的程序安装6中的代码编译就行了,然后代码可以完全跟书上的一样。代码编辑器或IDE等就按自己的喜好来选了。这个网上很多c语言编程环境搭建教程。
我选择命令行用vim编写程序。我的vim的使用和 Div环境方法:点击打开链接 http://blog.youkuaiyun.com/szu_tanglanting/article/details/17439005
在进行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 然后运行。