注:以下写作风格均学习自潘云登前辈
/******************************************************************/
By: 聂强
Date: 2012-2-12
Email: sddzycnq@gmail.com
Homepage: http://blog.csdn.net/sddzycnqjn
Copyright: 该文章版权由聂强所有。可在非商业目的下任意传播和复制。
对于商业目的下对本文的任何行为需经作者同意。
/******************************************************************/
运行环境:Ubuntu 10.04.1 LTS
Linux version 2.6.32-24-generic
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
在当当网订购的《unix环境高级编程》还未到。求学心切,先下载了PDF版一睹为快。
但编译第一个例子就出现了问题:
myls.c:1:19: apue.h: No such file or directory
myls.c: In function `main':
myls.c:13: error: `NULL' undeclared (first use in this function)
myls.c:13: error: (Each undeclared identifier is reported only once
myls.c:13: error: for each function it appears in.)
找不到头文件apue.h。
网友支招:
1. www.apuebook.com下载源代码。
2. 解压得到目录apue.2e。
3. 将 apue.2e/include下apue.h文件拷贝至/usr/include。
此时,再编译还会出问题:
/tmp/ccBBopm0.o(.text+0x2b): In function `main':
: undefined reference to `err_quit'
/tmp/ccBBopm0.o(.text+0x5f): In function `main':
: undefined reference to `err_sys'
collect2: ld returned 1 exit status
提示找不到函数err_quit和err_sys。
4. 解决步骤3问题的办法:
在目录/usr/include,创建文件myerr.h。其中myerr.h内容为附录B中内容(现将代码贴出):
读书要仔细:
《unix环境高级编程》第5页作者有这样一句话“在这20行的程序中,有很多细节需要考虑”。
·首先,其中包含了一个头文件apue.h。本书中几乎每一个程序都包含此头文件。……。附录B中列出了这一头文件。
·……
·调用了两个自编的函数来对错误进行处理:err_sys和err_quit。……。这两个出错处理函数在附录B中说明,1.7节将更多地叙述出错处理。
#include "apue.h"
#include <errno.h>/* for definition of errno */
#include <stdarg.h> /* ISO C variable aruments */
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 */
}
5. 编辑/usr/include/apue.h。在最后一行#endif /* _APUE_H */前面添加一个声明:#include "myerr.h"
OK,至此。
再次编译:gcc -o myls myls.c 成功
运行: ./myls /dev
结果:
.
..
mixer
audio
dsp
……
……
port
null
mem
rfkill
vga_arbiter