《Unix环境高级编程》中的一些问题之 pathalloc()函数问题
2012-04-18 10:54
190 查看
注:在《Unix环境高级编程》这本书中的程序都是自己敲入的,因此会遇到的一些问题,在此与大家分享。
问题:
在《Unix环境高级编程》英文原版Figure4.22和Figure4.24(中文版例子分别是4-7和4-9),直接在VIM中敲入程序Figure4.22后,运行gcc 4-22.c error.c -o a.4-22 发现编译并不能通过,会出现如下错误:
![](http://my.csdn.net/uploads/201204/18/1334715210_8626.jpg)
这是由于path_alloc()函数只声明未定义的原因,在《Unix环境高级编程》第一版中该函数包含在“ourhdr.h”中,在《Unix环境高级编程》第二版包含在“apue.h”中,书中附录只有函数声明没有函数定义。
解决办法:
我们只需在编译时加上pathalloc.c,就可以解决问题:(在vim中输入
gcc 4-22.c error.c pathalloc.c -o a.4-22编译即可通过)
![](http://my.csdn.net/uploads/201204/18/1334715679_5973.jpg)
代码:
1.
pathalloc.c
![](http://my.csdn.net/uploads/201204/18/1334716924_8988.jpg)
2.
4-22.c
3. error.c
#include <errno.h> /* for definition of errno */
#include <stdarg.h> /* ANSI C header file */
#include "ourhdr.h"
static void err_doit(int, const char *, va_list);
char *pname = NULL; /* caller can set this from argv[0] */
/* 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, fmt, ap);
va_end(ap);
return;
}
/* 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, 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, 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, fmt, ap);
va_end(ap);
return;
}
/* 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, fmt, ap);
va_end(ap);
exit(1);
}
/* Print a message and return to caller.
* Caller specifies "errnoflag". */
static void
err_doit(int errnoflag, const char *fmt, va_list ap)
{
int errno_save;
char buf[MAXLINE];
errno_save = errno; /* value caller might want printed */
vsprintf(buf, fmt, ap);
if (errnoflag)
sprintf(buf+strlen(buf), ": %s", strerror(errno_save));
strcat(buf, "\n");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(stderr); /* SunOS 4.1.* doesn't grok NULL argument */
return;
}
问题:
在《Unix环境高级编程》英文原版Figure4.22和Figure4.24(中文版例子分别是4-7和4-9),直接在VIM中敲入程序Figure4.22后,运行gcc 4-22.c error.c -o a.4-22 发现编译并不能通过,会出现如下错误:
![](http://my.csdn.net/uploads/201204/18/1334715210_8626.jpg)
这是由于path_alloc()函数只声明未定义的原因,在《Unix环境高级编程》第一版中该函数包含在“ourhdr.h”中,在《Unix环境高级编程》第二版包含在“apue.h”中,书中附录只有函数声明没有函数定义。
解决办法:
我们只需在编译时加上pathalloc.c,就可以解决问题:(在vim中输入
gcc 4-22.c error.c pathalloc.c -o a.4-22编译即可通过)
![](http://my.csdn.net/uploads/201204/18/1334715679_5973.jpg)
代码:
1.
pathalloc.c
![](http://my.csdn.net/uploads/201204/18/1334716924_8988.jpg)
2.
4-22.c
3. error.c
#include <errno.h> /* for definition of errno */
#include <stdarg.h> /* ANSI C header file */
#include "ourhdr.h"
static void err_doit(int, const char *, va_list);
char *pname = NULL; /* caller can set this from argv[0] */
/* 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, fmt, ap);
va_end(ap);
return;
}
/* 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, 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, 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, fmt, ap);
va_end(ap);
return;
}
/* 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, fmt, ap);
va_end(ap);
exit(1);
}
/* Print a message and return to caller.
* Caller specifies "errnoflag". */
static void
err_doit(int errnoflag, const char *fmt, va_list ap)
{
int errno_save;
char buf[MAXLINE];
errno_save = errno; /* value caller might want printed */
vsprintf(buf, fmt, ap);
if (errnoflag)
sprintf(buf+strlen(buf), ": %s", strerror(errno_save));
strcat(buf, "\n");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(stderr); /* SunOS 4.1.* doesn't grok NULL argument */
return;
}
相关文章推荐
- 字符串操作函数的一些细节问题
- 关于count()函数一些问题
- 函数返回类型为指针类型时的一些问题
- 函数返回类型为指针类型时的一些问题
- 关于定义类成员导出函数需要注意的一些问题
- 有关STL使用上的一些注意事项。关于某些函数的参数问题。
- Java Split()函数使用中一些要注意的问题
- 由malloc等函数想到的一些问题
- 浅议C编程中遇到scanf函数的一些问题
- 关于数组以及函数的一些问题与解答
- DoModal函数的一些问题
- C++指针作为函数的参数进行传递时需要注意的一些问题
- 函数返回类型为指针类型时的一些问题
- 有关scanf的输入函数的一些问题
- 函数返回局部变量的一些问题
- 字符串处理函数的一些问题
- SQL中使用的一些函数问题
- C++随记(六)---函数处理数组的一些问题
- 函数设计中存在的一些问题
- python中类的设计问题(一些高级问题探讨,函数重载,伪私有,工厂模式,类方法等)