相信很多跟我一样想要学习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(intintconst 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(
00
, 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(
00
, 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>

就好了。


 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐