《Unix环境高级编程》中的一些问题之 pathalloc()函数问题
来源:互联网 发布:华星网络报销系统 编辑:程序博客网 时间:2024/05/17 23:34
注:在《Unix环境高级编程》这本书中的程序都是自己敲入的,因此会遇到的一些问题,在此与大家分享。
问题:
在《Unix环境高级编程》英文原版Figure4.22和Figure4.24(中文版例子分别是4-7和4-9),直接在VIM中敲入程序Figure4.22后,运行gcc 4-22.c error.c -o a.4-22 发现编译并不能通过,会出现如下错误:
这是由于path_alloc()函数只声明未定义的原因,在《Unix环境高级编程》第一版中该函数包含在“ourhdr.h”中,在《Unix环境高级编程》第二版包含在“apue.h”中,书中附录只有函数声明没有函数定义。
解决办法:
我们只需在编译时加上pathalloc.c,就可以解决问题:(在vim中输入 gcc 4-22.c error.c pathalloc.c -o a.4-22编译即可通过)
代码:
1. pathalloc.c
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环境高级编程》中的一些问题之 pathalloc()函数问题
- 《unix 高级环境编程》中的apue问题
- 学习《Unix环境高级编程》之问题
- unix 环境高级编程之sigsuspend函数
- unix 环境高级编程之 system函数
- UNIX环境高级编程之-----popen函数
- 《UNIX环境高级编程》apue.h问题
- UNIX环境高级编程的编译问题
- UNIX环境高级编程--“apue.h”问题
- UNIX环境高级编程中的11章程序11-2编译问题---undefined reference to `pthread_create'
- UNIX环境高级编程中的apue.h头文件的问题
- 《unix环境高级编程》学习之 --- fread 函数
- unix环境高级编程之 read与write 函数详解
- 《Unix环境高级编程》之popen函数使用
- 《UNIX环境高级编程》编译问题彻底解决方案
- Unix环境高级编程-execlp示例问题
- unix环境高级编程中代码运行问题
- 《UNIX环境高级编程》中apue.h的问题
- 类型转换
- 数组初始化
- 免费注册参加"使用表格控件EJunGrid,实现企业级表单的录入与展现" 网络研讨会!
- Android Property System
- 一位女程序员的故事
- 《Unix环境高级编程》中的一些问题之 pathalloc()函数问题
- Android 媒体库数据更新
- asp.net中使用基于角色role的Forms验证
- ORA-24248 XMLDB extensible security not installed
- 56 关注应该程序的支持和维护
- 网站及系统源码
- Oracle 表中的列带有default值的陷阱
- 软件设计师考试学习之路
- 用Entity Framework的edmx 和WCF进行开发时,数据从silverlight端传到wcf 实体被初始化