unix高级环境编程(第三版)环境搭建

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

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

3. 进入下载目录 执行make

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

  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

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

6.复制到 系统环境 

  1. cp apueerror.h /usr/include/     

7.要编译运行的代码中#include<apue.h>的下一行增加一行:#include<error.h>

 

 

 

参考博客:

https://blog.csdn.net/caroline_wendy/article/details/39743345

https://blog.csdn.net/aboboo5200/article/details/58716172

猜你喜欢

转载自blog.csdn.net/qq_24889575/article/details/81173131