《UNIX環境高階程式設計》編譯問題徹底解決方案

2021-09-06 01:36:28 字數 2290 閱讀 2534

前些天,寫了篇部落格(

最後一年的時間,應該做些什麼呢?

)發誓要好好學一下《unix環境高階編譯》(下面簡稱apue),可乙個小小的編譯問題就悲劇了我好幾天,比如apue書上的第乙個程式是這樣的:

#include "apue.h" #include int main(int argc, char *argv) if((dp = opendir(argv[1])) == null) while((dirp = readdir(dp)) != null) closedir(dp); exit(0); }

直接這樣用gcc編譯會很悲劇的出現一大堆錯誤,錯誤這裡就不羅列出來了。

其實細心的朋友會發現本程式第一行包含了乙個apue.h的標頭檔案,這個標頭檔案在/usr/include可是沒有的哦,也就是說這不是標準標頭檔案,其實從它用雙引號包含也可以看出來。那這個標頭檔案在哪呢?

附錄裡。

找到了問題,解決就好辦了,把附錄裡的apue.h和error.c敲進電腦裡,我敲好了,懶得就直接複製吧。

#include "apue.h" #include /* for definition of errno */ #include /* 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, ...) /* * fatal error related to a system call. * print a message and terminate. */ void err_sys(const char *fmt, ...) /* * 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, ...) /* * fatal error related to a system call. * print a message, dump core, and terminate. */ void err_dump(const char *fmt, ...) /* * nonfatal error unrelated to a system call. * print a message and return. */ void err_msg(const char *fmt, ...) /* * fatal error unrelated to a system call. * print a message and terminate. */ void err_quit(const char *fmt, ...) /* * print a message and return to caller. * caller specifies "errnoflag". */ static void err_doit(int errnoflag, int error, const char *fmt, va_list ap)

這樣涉及到兩個檔案的同時編譯問題,如果你更懶,那麼還有辦法解決。

給個批處理用用吧。

#!/bin/sh #gg #以下是修改的gg指令碼,新增了乙個函式,indent每乙個.c和.h的功能 #wraper the gcc #set -x #用indent所有的c檔案和標頭檔案(因為我向ssh中拷**的時候縮排總是出問題,所以需要indent) go2indent() #不需要indent的話,直接注釋下面這條語句 go2indent $* exec_obj=`echo $1|cut -d. -f1` if gcc error.c $* -wall -o $exec_obj; then echo "gcc done! create executable file /"$exec_obj/"." else echo "gcc failed!" exit 1 fi #end

把批處理命名為gg,是不是迫不及待的想試試了?

不過,還有等一下,應該此時你還沒有執行許可權,敲命令sudo chmod 755 gg提公升許可權。然後別忘了用ls -l看一下,如果是該檔案許可權是-rwxr-xr-x,即本使用者和超級使用者讀寫執行,本組使用者讀執行,其他使用者執行。

以後只要把這3個檔案拷貝到你編寫的apue例程資料夾中,以./gg 檔名,來進行編譯就可以了

這樣操作就可以了。

好了,可以好好的,認真的,仔細的,開始看偉大的著作apue嘍。

但願可以學好。

unix環境高階程式設計

unix 日曆時間 自1970 年1 月1 日00 00 00 以來的國際標準時間 utc 程序時間 cpu 時間 時鐘時間 程序執行時間的總量。使用者cpu 時間 執行使用者指令時間量。系統cpu 時間 執行核心所經歷時間。命令 time 第三章至第七章 原子操作 任何乙個要求多於1 個函式呼叫的...

unix環境高階程式設計

檔案描述符file descriptor通常是乙個小的非負整數,核心用以標識乙個特定程序正在訪問的檔案。當核心開啟乙個現有檔案或建立乙個新檔案時,它都返回乙個檔案描述符。在讀寫檔案時,可以私用這個檔案描述符。按管理,每當執行乙個新程式時,所有的shell都為其開啟了3個標準檔案描述符 標準輸入,標準...

UNIX環境高階程式設計

本書是被譽為unix程式設計 聖經 的advanced programming in the unix environment一書的更新版。在本書第1版出版後的十幾年中,unix行業已經有了巨大的變化,特別是影響unix程式設計介面的有關標準變化很大。本書在保持了前一版風格的基礎上,根據最新的標準對...