最近在看apue的第二版,剛才在Linux下把隨書的源代碼編譯了一遍,還是稍微花了點(diǎn)時(shí)間,作為備忘把編譯過程記錄下來
隨書的源代碼可從www.上獲得,下載后的解壓得到名為apue.2e的目錄,在我的系統(tǒng)中該目錄的完整路徑為/home/se/apue.2e
接著首先是要閱讀/home/se/apue.2e/README,這是由apue第二版的作者Steve Rago寫的如何編譯隨書代碼的基本指導(dǎo)以及部分自本書第一版以來的更改,主要內(nèi)容如下:
Some source changes needed to be made after the book went out for the first
printing. I forgot to make corresponding changes in the source tree on the system used to develop the book. The changes are summarized below. 1. lib/recvfd.c and sockets/recvfd.c - needed sys/uio.h on Mac OS X
2. lib/sendfd.c and sockets/sendfd.c - needed sys/uio.h on Mac OS X 3. stdio/buf.c - added code for Mac OS X 4. threadctl/suspend.c - changed wait to waitloc to avoid symbol definition clash on Solaris 5. include/apue.h - FreeBSD compiles work better if we rely on the default system settings. Solaris needed a different XOPEN_SOURCE definition and also a CMSG_LEN definition. To build the source, edit the Make.defines.* file for your system and set
WKDIR to the pathname of the tree containing the source code. Then just run "make". It should figure out the system type and build the source for that platform automatically. If you are running on a system other than FreeBSD, Linux, Mac OS X, or Solaris, you‘ll need to modify the makefiles to include the settings for your system. Also, you‘ll probably need to modify the source code to get it to build on a different operating system. The example source was compiled and tested using FreeBSD 5.2.1, Linux 2.4.22, Mac OS X 10.3, and Solaris 9. For FAQs, updated source code, and the lost chapter, see http://www..
Please direct questions, suggestions, and bug reports to sar@. 基本內(nèi)容就是你用的系統(tǒng)如果是FreeBSD,Linux,Mac OS X或是Solaris,那么你只要修改相應(yīng)的Make.defines.*文件(即如果你使用的是Linux,那么你需要修改Make.defines.linux文件的內(nèi)容),將其中的設(shè)置改為你自己系統(tǒng)的設(shè)置然后在apue.2e目錄下運(yùn)行make就ok了.所有的代碼都在FreeBSD 5.2.1,Linux 2.4.22,Mac OS X 10.3,Solaris 9上編譯通過.
總的來說要編譯成功是很簡(jiǎn)單的,但總會(huì)因?yàn)槠脚_(tái)的不同會(huì)出現(xiàn)一些錯(cuò)誤,這時(shí)你就要根據(jù)自己系統(tǒng)的配置情況來進(jìn)行修改了
1.首先粗略的看了一下makefile的內(nèi)容,make首先會(huì)執(zhí)行腳本文件systype.sh,判斷所用系統(tǒng)的類型,然后根據(jù)該類型選擇對(duì)應(yīng)的Make.defines文件.這里所要做的就是給systype.sh添加執(zhí)行權(quán)限,chmod u+x systype.sh
2.因?yàn)槲矣玫氖荓inux,所以先看Make.defines.linux,需要修改的地方是WKDIR=/home/sar/apue.2e,把WKDIR改為你自己的工作目錄,在我這就是改為WKDIR=/home/se/apue.2e,這個(gè)路徑在編譯時(shí)尋找"apue.h"頭文件時(shí)使用.
3.然后我嘗試性的運(yùn)行了一次make,果然有問題,在進(jìn)入std目錄后報(bào)錯(cuò)了,說找不到nawk命令,nawk是new awk,而我的系統(tǒng)上只有awk,這時(shí)你有兩種選擇,可以在運(yùn)行make之前執(zhí)行alias nawk=‘a(chǎn)wk‘,這樣本質(zhì)上是給awk取了個(gè)叫nawk的別名,實(shí)際上運(yùn)行的還是awk,另一種方法就是修改WKDIR/std/linux.mk,把第10行和15行中的nawk都改為awk,至于什么是awk和nawk,以及它們的使用方法可以參考我之前收藏的一篇文章http://www.qqstock.cn/showWeb/0/0/308938.aspx
在這里,awk用來分別從makeconf.awk和makeopt.awk生成conf.c和options.c源文件,注意,在修改了linux.mk或是添加了alias之后要先把之前make失敗時(shí)生成的conf.c和options.c刪除,否則會(huì)報(bào)錯(cuò)
4.進(jìn)行了上述的修改后,回到WKDIR,運(yùn)行make,ok,沒有報(bào)錯(cuò),編譯成功了
之后,如果你要利用apue的lib,編譯運(yùn)行自己的代碼,必須在編譯時(shí)加上-I/home/se/apue.2e/include選項(xiàng),在連接時(shí)加上-L/home/se/apue.2e/lib source.c /home/se/apue.2e/lib/libapue.a選項(xiàng),這樣你就可以利用apue提供的想err_sys等函數(shù)了^_^
|
|