linux下《UNIX環境高級編程》(apue2)源碼編譯出錯的處理方法

 

原文鏈接:http://www.linuxdiyf.com/bbs/thread-90655-1-8.html

 

 

相信很多跟我一樣想要學習unix編程的朋友在興沖沖拿到《unix環境高級編程》後,準備拿源碼練練手時,執行第一個myls就出現一大堆的錯誤,這未免時個不小的打擊。今天把解決方法寫下來,第一自己有個記錄,第二也幫助那些被同樣問題困擾的朋友儘快的進入linux美麗的世界。(只限linux系統

首先需要make一次源代碼
編輯源碼解壓生成的apue.2e文件夾下的Make.defines.linux
修改WKDIR=/home/var/apue.2e爲你的apue.2e目錄,比如我的apue源碼解壓在/usr/local,那我就改爲:
WKDIR=/usr/local/apue.2e
然後進入apue.2e/std 目錄,編輯linux.mk。修改裏面所有的nawk爲awk。
最後返回apue.2e目錄,執行make命令

以下是編譯源碼時的錯誤提示跟解決方法(假定你的工作目錄跟我的一樣,爲/usr/local/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 */
}


然後在你需要使用這幾種錯誤處理函數的程序源代碼里加入:
#include <myerr.h>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章