Linux - Unix环境高级编程(第三版) 源代码编译(即头文件apue.h如何使用问题)

来源:互联网 发布:淘宝的万菊芬壶能买吗 编辑:程序博客网 时间:2024/05/01 21:21

1. 下载代码:http://www.apuebook.com/code3e.html

2. 安装依赖库:sudo apt-get install libbsd-dev 

3. 进入下载目录make

4. 复制头文件和动态链接库

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. sudo cp ./include/apue.h /usr/include/  
  2. sudo cp ./lib/libapue.a /usr/local/lib/  
  3. sudo cp ./lib/libapue.a /usr/lib/  
5. 设置错误文件error.h

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "apue.h"  
  2. #include <errno.h>      /* for definition of errno */  
  3. #include <stdarg.h>     /* ISO C variable aruments */  
  4.   
  5. static void err_doit(intintconst char *, va_list);  
  6.   
  7. /* 
  8.  * Nonfatal error related to a system call. 
  9.  * Print a message and return. 
  10.  */  
  11. void err_ret(const char *fmt, ...)  
  12. {  
  13.     va_list     ap;  
  14.   
  15.     va_start(ap, fmt);  
  16.     err_doit(1, errno, fmt, ap);  
  17.     va_end(ap);  
  18. }  
  19.   
  20.   
  21. /* 
  22.  * Fatal error related to a system call. 
  23.  * Print a message and terminate. 
  24.  */  
  25. void err_sys(const char *fmt, ...)  
  26. {  
  27.     va_list     ap;  
  28.   
  29.     va_start(ap, fmt);  
  30.     err_doit(1, errno, fmt, ap);  
  31.     va_end(ap);  
  32.     exit(1);  
  33. }  
  34.   
  35.   
  36. /* 
  37.  * Fatal error unrelated to a system call. 
  38.  * Error code passed as explict parameter. 
  39.  * Print a message and terminate. 
  40.  */  
  41. void err_exit(int error, const char *fmt, ...)  
  42. {  
  43.     va_list     ap;  
  44.   
  45.     va_start(ap, fmt);  
  46.     err_doit(1, error, fmt, ap);  
  47.     va_end(ap);  
  48.     exit(1);  
  49. }  
  50.   
  51.   
  52. /* 
  53.  * Fatal error related to a system call. 
  54.  * Print a message, dump core, and terminate. 
  55.  */  
  56. void err_dump(const char *fmt, ...)  
  57. {  
  58.     va_list     ap;  
  59.   
  60.     va_start(ap, fmt);  
  61.     err_doit(1, errno, fmt, ap);  
  62.     va_end(ap);  
  63.     abort();        /* dump core and terminate */  
  64.     exit(1);        /* shouldn't get here */  
  65. }  
  66.   
  67.   
  68. /* 
  69.  * Nonfatal error unrelated to a system call. 
  70.  * Print a message and return. 
  71.  */  
  72. void err_msg(const char *fmt, ...)  
  73. {  
  74.     va_list     ap;  
  75.   
  76.     va_start(ap, fmt);  
  77.     err_doit(0, 0, fmt, ap);  
  78.     va_end(ap);  
  79. }  
  80.   
  81.   
  82. /* 
  83.  * Fatal error unrelated to a system call. 
  84.  * Print a message and terminate. 
  85.  */  
  86. void err_quit(const char *fmt, ...)  
  87. {  
  88.     va_list     ap;  
  89.   
  90.     va_start(ap, fmt);  
  91.     err_doit(0, 0, fmt, ap);  
  92.     va_end(ap);  
  93.     exit(1);  
  94. }  
  95.   
  96.   
  97. /* 
  98.  * Print a message and return to caller. 
  99.  * Caller specifies "errnoflag". 
  100.  */  
  101. static void err_doit(int errnoflag, int error, const char *fmt, va_list ap)  
  102. {  
  103.     char    buf[MAXLINE];  
  104.    vsnprintf(buf, MAXLINE, fmt, ap);  
  105.    if (errnoflag)  
  106.        snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",  
  107.          strerror(error));  
  108.    strcat(buf, "\n");  
  109.    fflush(stdout);     /* in case stdout and stderr are the same */  
  110.    fputs(buf, stderr);  
  111.    fflush(NULL);       /* flushes all stdio output streams */  
  112. }  

6. 注销,重启

7. 代码文件

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "apue.h"  
  2. #include "error.h"  

0 0
原创粉丝点击