/bbs/thread

阅读: 评论:0

/bbs/thread

/bbs/thread

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

就好了。


转自:.html


本文发布于:2024-01-31 00:38:27,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170663280123995.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:www   http   linuxdiyf   thread   bbs
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23