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,添加一行:
#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
《UNIX环境高级编程》单个源码编译方法
如何确定系统调用函数包含在那个头文件中呢?这在Unix/Linux系统下并非一件难事。Unix/Linux下命令 man可以帮助我们找到。man命令不仅可以帮助我们查找一般命令的用法,同时提供不同层次的帮助诸如系统调用或者管理员级别的命令等等(譬如FreeBSD6.1中,man1是用户专用手册,man 2是系统调用,man 3是库函数查询等等)。
该1-1.c中所用到的系统函数调用有:opnedir(),readdir(),printf(),closedir()和exit()。
其中,对于常用的函数prinft()和exit(),它们所在的头文件一般都知道,分别是<stdio.h>和<stdlib.h>。而对于 opnedir(),readdir()和closedir(),我们可以通过man opendir,man readdir,manclosedir得到这三个关于目录 操作的函数所在的头文件都是:<sys/types.h>和<dirent.h>。这两个头文件在源程序中也已经列出。
次,1-1.c中还用到了作者自定义的两个函数:err_quit()和err_sys()。这两个函数主要使用来进行出错处理的。当然,使用这两个函数对错误信息的处理是比较完善的。但是,作为我们学习来讲,了解程序的核心功能是首要的,我们可以将出错处理简化一点,即当遇到错误的时候,我们只简单的使用printf()函数来提示一下有错误发生。当然,用printf()来进行出错处理并不是一种很合理的方法,而且往往我们看不到更关键的错误信息,但对于我们仅仅作为学习来用还是可以接受的。毕竟我们要理解的核心部分是程序的功能实现,出错处理在于其次。