Linux下APUE2源码编译出错的处理方法

时间:2008-11-14 12:39:33   来源:  作者:  点击:次  出处:技术无忧
关键字:linux apue2 编译

最近学习Linux编程,收获真的很多。以前看都是别人或man告诉你某个命令怎么使用,但现在我才领悟到,是程序的源代码决定了全部。
 
linux要编程,首先需要安装gcc与其它编程工具与库文件。最简单的方法是进入图形界面的添加删除程序,选上开发工具就可以了。
 
 
1.APUE2源代码下载:http://www.apuebook.com/src.tar.gz
2.我保存到了/root下.解压缩:tar -xzvf src.tar.gz
3.cd apue.2e进入apue.2e目录,查看README,告诉我们linux系统只要修改Make.defines.linux再make
4.vi Make.defines.linux 修改WKDIR=/root/apue.2e 就是说工作目录为WKDIR=/root/apue.2e
5.修改/root/apue.2e/std/linux.mk把全部的nawk改为awk.因些linux默认没有nawk
6.make
 
一个需要注意的地方:
apue编程的例子都有关include "apue.h"的代码,其实apue.h并不是系统自带的,它是作者编写的头文件,源代码在附录B中.因此一个方法是.把刚才生成的 /root/apue.2e/include/apue.h直接复制到/usr/include下.
还有像err_sys的函数也在附录B中.同样的方法是在/usr/include新建一个 my_err.h的文件,把Figure B.3. Error functions that output to standard error内容复制进去.这样按例子编程就只需要在行首添加:#include "my_err.h"就可以了..


来处:http://www.linuxdiyf.com/bbs/thread-90655-1-8.html

linux下《UNIX环境高级编程》(apue2)源码编译出错的处理方法
相信很多跟我一样想要学习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 */

staticvoiderr_doit(int,int,constchar*,va_list);

/*
* Nonfatal error related to a system call.
* Print a message and return.
*/
void
err_ret(constchar*fmt,...)
{
    va_listap;

    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(constchar*fmt,...)
{
    va_listap;

    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(interror,constchar*fmt,...)
{
    va_listap;

    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(constchar*fmt,...)
{
    va_listap;

    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(constchar*fmt,...)
{
    va_listap;

    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(constchar*fmt,...)
{
    va_listap;

    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".
*/
staticvoid
err_doit(interrnoflag,interror,constchar*fmt,va_listap)
{
    charbuf[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 */
}

然后在你需要使用这几种错误处理函数的程序源代码里加入:技术无忧.com


文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面

300x250广告位招租