最近在学习UNIX的编程,用的书是《UNIX环境高级编程》,看到书中有很有实例,我用的操作系统是RadHat,照着书把程序清单输入后编译却通不过,显示的错误是没有“apue.h头文件”。这下对我这只菜鸟来说就是当头一棒,这不坑爹吗?就照着书把程序再抄了一遍,发现出现同样的问题,这下引起我的思考。原来apue.h是作者自己写的一个文件,包含了常用的头文件,系统不自带。其中包含了常用的头文件,以及出错处理函数的定义。需要自己去配置这样的头文件,特将解决的方法总结如下:
在http://www.apuebook.com/下载src.tar.gz源代码的压缩包。
1、解压至/home/user/目录下
2、修改 Make.defines.linux中的WKDIR=/home/xxx/apue.2e,为WKDIR=/home/user/apue.2e
3、然后再进入apue.2e目录下的std目录,打开linux.mk,将里面的nawk全部替换为awk,可以使用这个命令 :%s/nawk/awk/g
4、把 /home/limeng/apue.2e/inlcude目录下的 apue.h 文件最后添加一行 #include "error.c",将该文件拷贝到/usr/include 目录中。
5、把 /home/limeng/apue.2e/lib目录下的 error.c 文件第一行 #include "apue.h"注释掉或者删除,也将该文件拷贝到/usr/include 目录中。
6、编译成功。
然后就可以方便的使用apue.h编译《unix高级环境编程》的的程序了。
apue.h内容如下:
1 #ifndef _APUE_H
2 #define _APUE_H
3
4 #define _XOPEN_SOURCE 600 /* Single UNIX Specification, Version 3 */
5
6 #include <sys/types.h> /* some systems still require this */
7 #include <sys/stat.h>
8 #include <sys/termios.h> /* for winsize */
9 #ifndef TIOCGWINSZ
10 #include <sys/ioctl.h>
11 #endif
12 #include <stdio.h> /* for convenience */
13 #include <stdlib.h> /* for convenience */
14 #include <stddef.h> /* for offsetof */
15 #include <string.h> /* for convenience */
16 #include <unistd.h> /* for convenience */
17 #include <signal.h> /* for SIG_ERR */
18
19
20 #define MAXLINE 4096 /* max line length */
21
22 /*
23 * Default file access permissions for new files.
24 */
25 #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
26
27 /*
28 * Default permissions for new directories.
29 */
30 #define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
31
32 typedef void Sigfunc(int); /* for signal handlers */
33
34 #if defined(SIG_IGN) && !defined(SIG_ERR)
35 #define SIG_ERR ((Sigfunc *)-1)
36 #endif
37
38 #define min(a,b) ((a) < (b) ? (a) : (b))
39 #define max(a,b) ((a) > (b) ? (a) : (b))
40
41 /*
42 * Prototypes for our own functions.
43 */
44 char *path_alloc(int *); /* Figure 2.15 */
45 long open_max(void); /* Figure 2.16 */
46 void clr_fl(int, int); /* Figure 3.11 */
47 void set_fl(int, int); /* Figure 3.11 */
48 void pr_exit(int); /* Figure 8.5 */
49 void pr_mask(const char *); /* Figure 10.14 */
50 Sigfunc *signal_intr(int, Sigfunc *); /* Figure 10.19 */
51
52 int tty_cbreak(int); /* Figure 18.20 */
53 int tty_raw(int); /* Figure 18.20 */
54 int tty_reset(int); /* Figure 18.20 */
55 void tty_atexit(void); /* Figure 18.20 */
56 #ifdef ECHO /* only if <termios.h> has been included */
57 struct termios *tty_termios(void); /* Figure 18.20 */
58 #endif
59
60 void sleep_us(unsigned int); /* Exercise 14.6 */
61 ssize_t readn(int, void *, size_t); /* Figure 14.29 */
62 ssize_t writen(int, const void *, size_t); /* Figure 14.29 */
63 void daemonize(const char *); /* Figure 13.1 */
64
65 int s_pipe(int *); /* Figures 17.6 and 17.13 */
66 int recv_fd(int, ssize_t (*func)(int,
67 const void *, size_t));/* Figures 17.21 and 17.23 */
68 int send_fd(int, int); /* Figures 17.20 and 17.22 */
69 int send_err(int, int,
70 const char *); /* Figure 17.19 */
71 int serv_listen(const char *); /* Figures 17.10 and 17.15 */
72 int serv_accept(int, uid_t *); /* Figures 17.11 and 17.16 */
73
74 int cli_conn(const char *); /* Figures 17.12 and 17.17 */
75 int buf_args(char *, int (*func)(int,
76 char **)); /* Figure 17.32 */
77
78 int ptym_open(char *, int); /* Figures 19.8, 19.9, and 19.10 */
79 int ptys_open(char *); /* Figures 19.8, 19.9, and 19.10 */
80 #ifdef TIOCGWINSZ
81 pid_t pty_fork(int *, char *, int, const struct termios *,
82 const struct winsize *); /* Figure 19.11 */
83 #endif
84
85 int lock_reg(int, int, int, off_t, int, off_t); /* Figure 14.5 */
86 #define read_lock(fd, offset, whence, len) \
87 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
88 #define readw_lock(fd, offset, whence, len) \
89 lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
90 #define write_lock(fd, offset, whence, len) \
91 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
92 #define writew_lock(fd, offset, whence, len) \
93 lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
94 #define un_lock(fd, offset, whence, len) \
95 lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
96
97 pid_t lock_test(int, int, off_t, int, off_t); /* Figure 14.6 */
98
99 #define is_read_lockable(fd, offset, whence, len) \
100 (lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
101 #define is_write_lockable(fd, offset, whence, len) \
102 (lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)
103
104 void err_dump(const char *, ...); /* Appendix B */
105 void err_msg(const char *, ...);
106 void err_quit(const char *, ...);
107 void err_exit(int, const char *, ...);
108 void err_ret(const char *, ...);
109 void err_sys(const char *, ...);
110
111 void log_msg(const char *, ...); /* Appendix B */
112 void log_open(const char *, int, int);
113 void log_quit(const char *, ...);
114 void log_ret(const char *, ...);
115 void log_sys(const char *, ...);
116
117 void TELL_WAIT(void); /* parent/child from Section 8.9 */
118 void TELL_PARENT(pid_t);
119 void TELL_CHILD(pid_t);
120 void WAIT_PARENT(void);
121 void WAIT_CHILD(void);
122
123 #endif /* _APUE_H */