http://ssepqhyneg.blog.163.com/blog/static/4105553920106804956179/
首先需要make一次源代码
编辑源码解压生成的apue.2e文件夹下的Make.defines.linux 以及其他几个.linux文件
修改WKDIR=/home/var/apue.2e为你的apue.2e目录,比如我的apue源码解压在/home/share,那我就改为:
WKDIR=/home/share
然后进入apue.2e/std 目录,编辑linux.mk。修改里面所有的nawk为awk。
最后返回apue.2e目录,执行make命令。
# make
以下是编译源码时的错误提示跟解决方法(假定你的工作目录跟我的一样,为/home/share/apue.2e)
错误提示1:
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到系统默认头文件目录中
$cp /usr/local/apue.2e/include/apue.h /usr/include
错误提示2:
/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是作者自己定义的错误处理函数,需要单独定义头文件
在/usr/include 下新建一个名为myerr.h的文件
拷贝下边的内容到myerr.h(其实此头文件在原书的附录B中)
#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 */
}
然后在你需要使用这几种错误处理函数的程序源代码里加入
How_to_use_apue_source_file
My Environment.
===========================================================
shunwang@shunwang-desktop:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 9.10
Release: 9.10
Codename: karmic
shunwang@shunwang-desktop:~$ uname -a
Linux shunwang-desktop 2.6.31-20-generic #57-Ubuntu SMP Mon Feb 8 09:05:19 UTC 2010 i686 GNU/Linux
===========================================================
Get the source code, from http://www.apuebook.com/
1. Modify apue.2e/Make.defines.linux
WKDIR=/home/shunwang/shawn_file/myfiles/my_study/kernel/APUE_source_code/apue/apue.2e
#WKDIR=/home/sar/apue.2e
2. ARG_MAX undeclared.
Error description.
$ make
...
make[2]: Entering directory `/home/shunwang/shawn_file/myfiles/my_study/kernel/APUE_source_code/apue/apue.2e/threadctl'
gcc -DLINUX -ansi -I/home/shunwang/shawn_file/myfiles/my_study/kernel/APUE_source_code/apue/apue.2e/include -Wall -D_GNU_SOURCE -c -o getenv1.o getenv1.c
getenv1.c:4: error: ‘ARG_MAX’ undeclared here (not in a function)
make[2]: *** [getenv1.o] Error 1
make[2]: Leaving directory `/home/shunwang/shawn_file/myfiles/my_study/kernel/APUE_source_code/apue/apue.2e/threadctl'
make[1]: *** [linux] Error 1
make[1]: Leaving directory `/home/shunwang/shawn_file/myfiles/my_study/kernel/APUE_source_code/apue/apue.2e'
make: *** [all] Error 2
Solution:
在apue.2e/include/apue.h中添加一行:
#define ARG_MAX 4096
打开apue.2e/threadctl/getenv3.c,getenv1.c, getenv2.c添加一行:
#include "apue.h"
再运行make, 编译通过。
3. 使用第一章的例子fig1.3
# ll fig1.3
lrwxrwxrwx 1 shunwang shunwang 10 2010-07-08 10:30 fig1.3 -> file/ls1.c
#ls file
access cdpwd.c devrdev fileflags.c freebsd.mk hello hole.c longpath.c macos.mk seek testerror uidgid.c unlink zap.c
access.c changemod devrdev.c filetype ftw4 hello.c linux.mk ls1 mycd seek.c testerror.c umask unlink.c
cdpwd changemod.c fileflags filetype.c ftw4.c hole longpath ls1.c mycd.c solaris.mk uidgid umask.c zap
# ./file/ls1 /home/
shunwang
..
lost+found
.
Error on Fedora 13 and the solution:
refer to: http://blog.chinaunix.net/u3/96584/showart_2121016.html
apue2配书源码在Fedora-12下的编译
1 获得源码
从apue的官方网站http://www.apuebook.com/下载源码。
2 WKDIR
编辑源码解压生成的apue.2e文件夹下的Make.defines.linux
修改WKDIR=/home/sar/apue.2e为你的apue.2e目录,
比如