《UNIX环境高级编程》配置
在编译UNIX环境高级编程一书中的代码时,需要配置apue.h等一些代码文件。
下载源码-apue.h, errno.h
UNIX环境高级编程官方网站
点击下载

拷贝到本机头文件目录
下载后解压,找到include目录中的apue.h和lib目录下的error.c,放到本机编译器头文件目录中,我的是mac g++,通过g++ -v查看–with-gxx-include-dir选项,即为编译器头文件目录,放入其中。
补充apue.h中的error处理方法
apue.h中声明了一些error处理方法,如err_quit, err_msg等,但没有定义,下面把定义以及其需要的两个头文件引入写到里面
#include <stdarg.h>
#include <errno.h>
static void err_doit(int, int, const char *, va_list);
/*
* * Nonfatal error related to a system call.
* * Print a message and return.
* */
void
err_ret(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
}
/*
* * Fatal error related to a system call.
* * Print a message and terminate.
* */
void
err_sys(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
exit(1);
}
/*
* * Fatal error unrelated to a system call.
* * Error code passed as explict parameter.
* * Print a message and terminate.
* */
void
err_exit(int error, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, error, fmt, ap);
va_end(ap);
exit(1);
}
/*
* * Fatal error related to a system call.
* * Print a message, dump core, and terminate.
* */
void
err_dump(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(1); /* shouldn't get here */
}
/*
* * Nonfatal error unrelated to a system call.
* * Print a message and return.
* */
void
err_msg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
}
/*
* * Fatal error unrelated to a system call.
* * Print a message and terminate.
* */
void
err_quit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
exit(1);
}
/*
* * Print a message and return to caller.
* * Caller specifies "errnoflag".
* */
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
char buf[MAXLINE];
vsnprintf(buf, MAXLINE, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",strerror(error));
strcat(buf, " ");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL); /* flushes all stdio output streams */
}
这样就可以正常使用了。
本文介绍如何配置《UNIX环境高级编程》一书中所需的apue.h和errno.h文件,包括下载源码、放置到本地编译器头文件目录的过程,并补充了apue.h中的错误处理方法。
784

被折叠的 条评论
为什么被折叠?



