這本書在 Unix C 編程圈的地位不必多說。不過書裡幾乎每個範例都會 include 作者自寫的 apue.h,而它並非標準 C 標頭檔,得自己下載設定一次,之後所有範例才能直接編譯。
到書的官方網站把 source code 抓下來。以下示範以第三版為例。
解開之後你會看到一個 README,作者其實已經把做法寫在裡面了:
Read the file called DISCLAIMER.
On Freebsd, type "gmake". On other platforms, type "make"
(as long as this is gnu make).
For FAQs, updated source code, and the lost chapter,
see https://www.apuebook.com.
照 README 執行 make 即可(FreeBSD 使用者改用 gmake)。
真正需要的只有兩個檔案:include/apue.h 與 lib/error.c。以我的環境為例,它們分別位於:
/home/mark/Downloads/apue.3e/include /* apue.h */
/home/mark/Downloads/apue.3e/lib /* error.c */
把它們複製到 C 語言的預設標頭檔目錄:
cp ./include/apue.h ./lib/error.c /usr/include
在 apue.h 的最後一行之前,加入:
#include "error.c"
完成後的檔尾長得像這樣:
#include "error.c"
#endif /* _APUE_H */
之後書上的範例就能直接 gcc xxx.c -o xxx 編譯,不必再為找不到標頭檔而卡住。把標頭檔直接 include 進去並不是嚴謹做法,但對跟著書做練習來說,是最省事的方式。