1 tar -xzfv unpv22e.tar.gz
2 cd unpv22e
3 ./configure
4 cd lib
5 make
make编译失败,因为需要对两个文件修改,unpv22e/config.h和unpv22e/wrapunix.c。
vi config.h
/*注释掉这三行*/
// #define uint8_t unsigned char /* <sys/types.h> */
// #define uint16_t unsigned short /* <sys/types.h> */
// #define uint32_t unsigned int /* <sys/types.h> */
/*添加MSG_R和MSG_W定义*/
typedef unsigned long ulong_t;
#define MSG_R 0400
#define MSG_W 0200
添加_GNU_SOURCE定义
vi config.h
#define _GNU_SOURCE
vi lib/wrapunix.c
/*编译warpunix.c,使用mkstemp函数替换mktemp函数*/
void
Mktemp(char *template)
{
if (mkstemp(template) == NULL || template[0] == 0)
err_quit("mktemp error");
}
再次执行make命令,会在unp22e目录下生成静态库文件libunpipc.a和在lib目录下生成目标文件*.o。
1 /* 编译生成libunpipc.a */
2 cd lib
3 make
将config.h和pipe目录下的unpipc.h 复制到/usr/include/目录下,并修改unpipc.h下面的#include "../config.h" 为 #include "config.h".(备注:由于考虑到UNIX网络编程卷1也把config.h拷贝到/usr/include目录中,所以需要把卷2的config.h改为其他名字,例如ipcconfig.h。同时也需要修改/pipe/unpipc.h文件中"./ipcconfig.h")。
cd unp22e
mv config.h ipconfig.h
vi pipe/unpipc.h
/*unpipc.h修改内容*/
#include "./ipconfig.h"
/*复制libunpipc.a ipconfig.h unpipc.h*/
sudo cp ipconfig.h pipe/unpipc.h /usr/include
sudo cp libunpipc.a /usr/lib
一切准备工作就绪,那就让我们来编译书本中的第一个例子mainpipe.c
cd pipe
gcc mainpipe.c server.c client.c -lunpipc -o mainpipe.out
编译失败,缺少对应库文件,加上 -lrt编译选项即可。
gcc mainpipe.c server.c client.c -lunpipc -rt -o mainpipe.out
编译失败提示:undefined reference to symbol 'sem_timedwait@@GLIBC_2.2.5'。google之答案,原来需要线程苦支持,加上-lpthread选项。
gcc mainpipe.c server.c client.c -lunpipc -lrt -lpthread -o mainpipe.out
./mainpipe.out
/*输入文件名*/
mainpipe.c
#include "unpipc.h"
void client(int, int), server(int, int);
int
main(int argc, char **argv)
{
int pipe1[2], pipe2[2];
pid_t childpid;
Pipe(pipe1); /* create two pipes */
Pipe(pipe2);
if ( (childpid = Fork()) == 0) { /* child */
Close(pipe1[1]);
Close(pipe2[0]);
server(pipe1[0], pipe2[1]);
exit(0);
}
/* 4parent */
Close(pipe1[0]);
Close(pipe2[1]);
client(pipe2[0], pipe1[1]);
Waitpid(childpid, NULL, 0); /* wait for child to terminate */
exit(0);
}
至此,UNIX网络编程卷2源码编译已经完成。