就是不改err_sys方法为printf, 
我把代码放在/root/Desktop/unix/apue.2e 
编辑源码解压生成的apue.2e文件夹下的Make.defines.linux 
WKDIR=/home/var/apue.2e为/root/Desktop/unix/apue.2e 
然后进入apue.2e/std 目录,编辑linux.mk。修改里面所有的nawk为awk 
vim下 
:%s/nawk/awk/ 
在/root/Desktop/unix/apue.2e下运行make
 
输出一堆... 
结尾是 
etenv1.c:4: error: ‘ARG_MAX’ undeclared here (not in a function) 
make[2]: *** [getenv1.o] 错误 1 
make[2]:正在离开目录 `/root/Desktop/unix/apue.2e/threadctl' 
make[1]: *** [linux] 错误 1 
make[1]:正在离开目录 `/root/Desktop/unix/apue.2e' 
make: *** [all] 错误 2 
基本没啥大错误 
编辑在目录下编辑fig1.c为 

Java代码  收藏代码

  1. #include "apue.h"  

  2. #define BUFFSIZE 4096  

  3. int main(void)  

  4. {  

  5.         int n;  

  6.         char buf[BUFFSIZE];  

  7.         while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0)  

  8.                 if(write(STDOUT_FILENO,buf,n)!=n)  

  9.                         err_sys("write error");  

  10.         if(n<0)  

  11.                 err_sys("read error");  

  12.         exit(0);  

  13. }  


然后运行 
gcc fig1.c -I ./include/ -L ./lib -lapue 
会生成a.out 
./a.out 写啥就打印啥了 
说明: 
-o就不说了,不写就生成a.out 
-I 指定apue.h所在的文件夹 
-L 类库的目录 
-l 找静态库,比如libapue.a的名称为apue 
编译完成后可以用 
ldd a.out看看库,是叫库吧, 

ctags: 
apt-get install ctags 
在source目录下 
ctags -R生成tags文件 
在~/.vimrc文件中定义 
set tags=生成的tags文件全路径 
vim fig1.c后用 
光标处Ctrl-]键:跳到光标所在单词的tag。Ctrl-T键:跳回原来的位置。g]键(先按g再按]):如果有同名的多个tag,可以用这两个键进行跳转,会提示选择序号 



如果是《unix网络编程-卷2-进程间通信》 
比如下载的源码在/root/Desktop/unpv22e/ 
cd /root/Desktop/unpv22e 
./configure 
make 
vi config.h 
注释掉56,57,58行 
cd lib 
make就成功
了 
cd ../pipe     # build and test a simple program 

Java代码  收藏代码

  1. [root@localhost pipe]# make pipeconf  

  2. gcc -g -O2 -D_REENTRANT -Wall   -c -o pipeconf.o pipeconf.c  

  3. gcc -g -O2 -D_REENTRANT -Wall -o pipeconf pipeconf.o ../libunpipc.a -lrt -lpthread   

  4. ../libunpipc.a(wrapunix.o): In function `Mktemp':  

  5. /root/haoning/unpv22e/lib/wrapunix.c:184: warning: the use of `mktemp' is dangerous, better use `mkstemp'  

  6. [root@localhost pipe]# ./pipeconf /tmp/  

  7. PIPE_BUF = 4096, OPEN_MAX = 1024  


参考http://tieba.baidu.com/f?kz=327192705 

在第三章 System V IPC里(在红帽5里测试) 
svmsg里的类编译不过 

Java代码  收藏代码

  1. [root@s121@251 svmsg]# make  

  2. gcc -g -O2 -D_REENTRANT -Wall   -c -o ctl.o ctl.c  

  3. ctl.c: In function ‘main’:  

  4. ctl.c:8: 错误:‘buf’ 的存储大小未知  

  5. ctl.c:10: 错误:‘MSG_R’ 未声明 (在此函数内第一次使用)  

  6. ctl.c:10: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其  

  7. ctl.c:10: 错误:所在的函数内只报告一次。)  

  8. ctl.c:10: 错误:‘MSG_W’ 未声明 (在此函数内第一次使用)  

  9. ctl.c:18: 错误:‘ulong_t’ 未声明 (在此函数内第一次使用)  

  10. ctl.c:18: 错误:expected ‘)’ before ‘info’  

  11. ctl.c:19: 警告:格式字符串实参太少  

  12. ctl.c:8: 警告:未使用的变量 ‘buf’  

  13. make: *** [ctl.o] 错误 1  


改三部分: 
1.unpv22e里面的Make.defines 
修改 
#CFLAGS = -g -O2 -D_REENTRANT -Wall 
CFLAGS = -g -O2 -D_GNU_SOURCE -D__USE_GNU -D_REENTRANT -Wall 
2.代码里面的,比如ctl.c里面的Msgget方法需要修改 
//msqid = Msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT); 
msqid = Msgget(IPC_PRIVATE, IPC_CREAT|0660); 
3. 把所有的ulong_t改成ulong 
修改后ctl.c就没错误了,其他错误还有一堆,挨个改吧,诶 
把svmsg/ctl.c改成: 

Java代码  收藏代码

  1. #include        "unpipc.h"  

  2.   

  3. int  

  4. main(int argc, char **argv)  

  5. {  

  6.         int                             msqid;  

  7.         struct msqid_ds info;  

  8.         struct msgbuf   buf;  

  9.   

  10. //      msqid = Msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT);  

  11.         msqid = Msgget(IPC_PRIVATE, IPC_CREAT|0660);  

  12.         buf.mtype = 1;  

  13.         buf.mtext[0] = 1;  

  14.         Msgsnd(msqid, &buf, 10);  

  15.   

  16.         Msgctl(msqid, IPC_STAT, &info);  

  17.         printf("read-write: %03o, cbytes = %lu, qnum = %lu, qbytes = %lu\n", info.msg_perm.mode & 0777, (ulong) info.msg_cbytes, (ul  

  18. ong) info.msg_qnum, (ulong) info.msg_qbytes);//这里的ulong_t全改成ulong了  

  19.   

  20.         system("ipcs -q");  

  21.   

  22.         Msgctl(msqid, IPC_RMID, NULL);  

  23.         exit(0);  

  24. }  


后 

Java代码  收藏代码

  1. [root@localhost svmsg]# make  

  2. gcc -g -O2 -D_GNU_SOURCE -D__USE_GNU -D_REENTRANT -Wall    -c -o ctl.o ctl.c  

  3. gcc -g -O2 -D_GNU_SOURCE -D__USE_GNU -D_REENTRANT -Wall  -o ctl ctl.o ../libunpipc.a -lrt -lpthread   

  4. ../libunpipc.a(wrapunix.o): In function `Mktemp':  

  5. /root/haoning/unpv22e/lib/wrapunix.c:184: warning: the use of `mktemp' is dangerous, better use `mkstemp'  

  6. gcc -g -O2 -D_GNU_SOURCE -D__USE_GNU -D_REENTRANT -Wall    -c -o limits.o limits.c  

  7. limits.c: In function ‘main’:  

  8. limits.c:19: 错误:‘MSG_R’ 未声明 (在此函数内第一次使用)  

  9. limits.c:19: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其  

  10. limits.c:19: 错误:所在的函数内只报告一次。)  

  11. limits.c:19: 错误:‘MSG_W’ 未声明 (在此函数内第一次使用)  

  12. make: *** [limits.o] 错误 1  


继续改limits.c 

Java代码  收藏代码

  1. #include    "unpipc.h"  

  2.   

  3. #define MAX_DATA    64*1024  

  4. #define MAX_NMESG   4096  

  5. #define MAX_NIDS    4096  

  6. int     max_mesg;  

  7.   

  8. struct mymesg {  

  9.   long  type;  

  10.   char  data[MAX_DATA];  

  11. } mesg;  

  12.   

  13. int  

  14. main(int argc, char **argv)  

  15. {  

  16.     int     i, j, msqid, qid[MAX_NIDS];  

  17.   

  18.         /* 4first try and determine maximum amount of data we can send */  

  19. //  msqid = Msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT);  

  20.     msqid = Msgget(IPC_PRIVATE, IPC_CREAT|0660);  

  21.     mesg.type = 1;  

  22.     for (i = MAX_DATA; i > 0; i -= 128) {  

  23.         if (msgsnd(msqid, &mesg, i, 0) == 0) {  

  24.             printf("maximum amount of data per message = %d\n", i);  

  25.             max_mesg = i;  

  26.             break;  

  27.         }  

  28.         if (errno != EINVAL)  

  29.             err_sys("msgsnd error for length %d", i);  

  30.     }  

  31.     if (i == 0)  

  32.         err_quit("i == 0");  

  33.     Msgctl(msqid, IPC_RMID, NULL);  

  34.   

  35.         /* 4see how many messages of varying size can be put onto a queue */  

  36.     mesg.type = 1;  

  37.     for (i = 8; i <= max_mesg; i *= 2) {  

  38.     //  msqid = Msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT);  

  39.         msqid = Msgget(IPC_PRIVATE, IPC_CREAT|0660);      

  40.         for (j = 0; j < MAX_NMESG; j++) {  

  41.             if (msgsnd(msqid, &mesg, i, IPC_NOWAIT) != 0) {  

  42.                 if (errno == EAGAIN)  

  43.                     break;  

  44.                 err_sys("msgsnd error, i = %d, j = %d", i, j);  

  45.                 break;  

  46.             }  

  47.         }  

  48.         printf("%d %d-byte messages were placed onto queue,", j, i);  

  49.         printf(" %d bytes total\n", i*j);  

  50.         Msgctl(msqid, IPC_RMID, NULL);  

  51.     }  

  52.   

  53.         /* 4see how many identifiers we can "open" */  

  54.     mesg.type = 1;  

  55.     for (i = 0; i <= MAX_NIDS; i++) {  

  56.         //if ( (qid[i] = msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT)) == -1) {  

  57.   

  58.         if ( (qid[i] = msgget(IPC_PRIVATE, IPC_CREAT|0660)) == -1) {  

  59.             printf("%d identifiers open at once\n", i);  

  60.             break;  

  61.         }  

  62.     }  

  63.     for (j = 0; j < i; j++)  

  64.         Msgctl(qid[j], IPC_RMID, NULL);  

  65.   

  66.     exit(0);  

  67. }  


slot.c---->System V IPC .P26 第三章 

Java代码  收藏代码

  1. #include        "unpipc.h"  

  2. int  

  3. main(int argc, char **argv)  

  4. {  

  5.         int             i, msqid;  

  6.   

  7.         for (i = 0; i < 10; i++) {  

  8.         //      msqid = Msgget(IPC_PRIVATE, SVMSG_MODE | IPC_CREAT);  

  9.                 msqid = Msgget(IPC_PRIVATE, IPC_CREAT|0660);  

  10.                 printf("msqid = %d\n", msqid);  

  11.   

  12.                 Msgctl(msqid, IPC_RMID, NULL);  

  13.         }  

  14.         exit(0);  

  15. }  


unpv22e/shm/svmsgread.c中 

Java代码  收藏代码

  1. [root@122226 shm]# make  

  2. gcc -g -O2 -D_GNU_SOURCE -D__USE_GNU -D_REENTRANT -Wall    -c -o svmsgread.o svmsgread.c  

  3. svmsgread.c: In function ‘main’:  

  4. svmsgread.c:27: 错误:‘MSG_R’ 未声明 (在此函数内第一次使用)  

  5. svmsgread.c:27: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其  

  6. svmsgread.c:27: 错误:所在的函数内只报告一次。)  

  7. svmsgread.c:55: 警告:格式 ‘%d’ 需要类型 ‘int’,但实参 2 的类型为 ‘s  



O_RDWR替换MSG_R或者MSG_W,网上说的 O_RDWD没找到