APUE编译错误小结(整理)_c语言常见错误小结

2020-02-27 其他工作总结 下载本文

APUE编译错误小结(整理)由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c语言常见错误小结”。

linux下《UNIX环境高级编程》(apue2)源码编译出错的处理方法相信很多跟我一样想要学习unix编程的朋友在兴冲冲拿到《unix环境高级编程》后,准备拿源码练练手时,执行第一个myls就出现一大堆的错误,这未免时个不小的打击。今天把解决方法写下来,第一自己有个记录,第二也帮助那些被同样问题困扰的朋友尽快的进入linux美丽的世界。(只限linux系统)

首先需要make一次源代码

编辑源码解压生成的apue.2e文件夹下的Make.defines.linux 修改WKDIR=/home/var/apue.2e为你的apue.2e目录,比如我的apue源码解压在/home/lc,那我就改为:

WKDIR=/home/lc/apue.2e 然后进入apue.2e/std 目录,编辑linux.mk。修改里面所有的nawk为awk。最后返回apue.2e目录,执行make命令: 在我的机器上编译时,提示ARG_MAX未定义,可以这么修改。在apue.2e/include/apue.h中添加一行:

#define ARG_MAX 4096 打开apue.2e/threadtl/getenv1.c 和apue.2e/threadctl/getenv3.c,添加一行: #include “apue.h” 这样就可以编译通过了,复制apue.2e/include/apue.h到/usr/include下,apue.2e/lib/libapue.a 到/usr/lib/和 /usr/lib64下。

以下是编译源码时的错误提示跟解决方法(假定你的工作目录跟我的一样,为/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 /* for definition of errno */ #include /* ISO C variable aruments */ static void err_doit(int, int, const char *, va_list);/* * Nonfatal error related to a system call.* Print a meage 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 meage 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 paed as explict parameter.* Print a meage 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 meage, 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 meage 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 meage 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 meage 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”

至此,APUE就可以顺利编译啦

《APUE编译错误小结(整理).docx》
将本文的Word文档下载,方便收藏和打印
推荐度:
APUE编译错误小结(整理)
点击下载文档
相关专题 c语言常见错误小结 小结 错误 APUE c语言常见错误小结 小结 错误 APUE
[其他工作总结]相关推荐
    [其他工作总结]热门文章
      下载全文