Linux安装apue.3e(基于ubuntu16.0.4)

时间:2023-03-09 03:27:33
Linux安装apue.3e(基于ubuntu16.0.4)

本菜刚刚学习UNIX高级编程,无奈搭建本书编程环境时遇到不少问题,参考了网上各路大神的解决办法,最终解决了问题。

(1)下载源代码,可以去官网下载:http://apuebook.com/code3e.html

(2)解压缩源代码文件:tar -zxvf src.3e.tar.gz

(3) 安装静态链接库:sudo apt-get install libbsd-dev

(4) make

(5).在编译成功的基础上,我们进行安装apue.h文件及其对应的静态链接库libapue.a

sudo cp ./include/apue.h /usr/include/
     sudo
cp ./lib/libapue.a /usr/local/lib/
为什么要将libapue.a移到/usr/local/lib中呢?原因是libapue.a是apue.h头文件中包含的所有函数及宏定义的具体实现,是一个静态链接库。

(6)创建apueerror.h文件

/**************
*
*apueerror.h
*
*************/
#include <apue.h>
#include <stdio.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(, 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(, errno, fmt, ap);
va_end(ap);
exit();
} /*
* 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(, error, fmt, ap);
va_end(ap);
exit();
} /*
* 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(, errno, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(); /* 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(, , 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(, , fmt, ap);
va_end(ap);
exit();
} /*
* 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, "\n");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL); /* flushes all stdio output streams */
}

(7)sudo cp apueerror.h  /usr/include/

(8)在要编译运行的代码中#include<apue.h>的下一行增加一行:#include <apueerror.h>

(9)编译就完事了