在ubuntu8.10下搞定《unix环境高级编程》第一个例子

     本人的所用操作系统是ubuntu8.10。

     开始学习《unix环境高级编程》,编译第一个例子就出现了问题:

             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。

看网上的人是怎么解决的,发现需要将apue.h手动添加到开发环境中。
     于是去www.apuebook.com下载源代码。

     将源代码解压后会出现目录apue.2e。将apue.2e下的include目录里面的apue.h文件拷贝至/usr/include。

                 cd  apue.2e所在目录

                 cd  include

                 cp  apue.h /usr/include

     再编译还是出问题:

             /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函数的定义。

     其实这两个函数在《unix环境高级编程》一书的附录B中可以找到。

     在这里给出一种解决办法。

     转到目录/usr/include,创建文件myerr.h。将下列代码添加至此文件:

 

#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 "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 */
}

    然后编辑同一目录下的apue.h文件(这个文件是通过前一个步骤添加的)。在最后一行#endif    /* _APUE_H */前面添加一个声明:

         #include "myerr.h"              /*自己添加的头文件,方便unix环境高级编程之用*/

    然后转至源代码所在目录,编译:

        gcc   -o myls myls.c

    这次获得成功,运行:

       ./myls  /usr

    出现如下结果

     src
     ..
     .
     lib
     share
     sbin
     include
     local
     X11R6
     bin
     games

     成功列出所有文件夹,success!

     希望对和我一样的初学者有一定的帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值