UNIX环境高级编程(一)下载并安装apue.h

来源:互联网 发布:windows mac 编辑:程序博客网 时间:2024/05/18 01:03
环境:Ubuntu14.04


因为 APUE  使用的是 nawk, 而 Ubuntu 自带的是 nawk, 应该是不修改可以直接使用的.(其他发行版需要进入apue.2e目录下的std目录,打开linux.mk,将里面的nawk全部替换为awk。

网上也有些方法是要安装 gawk 然后修改 std/linux.mk  将两个 nawk 改为 gawk 来实现的. (要放在 make 之前)

所以, 就要使用如下命令来安装 gawk 了: 

[cpp] view plaincopy
  1. sudo apt-get install gawk  


然后............


1、先在这个网站 http://www.apuebook.com/ 下载tar.gz格式的源码包,然后解压至某个目录,比如说/home/xhm/下,然后进入目录apue.2e,把文件 Make.defines.linux 中的 WKDIR=/home/sar/apue.2e 修改为 WKDIR=/home/xhm/apue.2e 。

2、然后在此目录下运行make命令,即回到 /home/xhm/apue.2e 目录在终端中输入 “make” (不含引号)
出现错误1:没找到apue.h。
解决:把当前源码中的apue.h(在include/apue.h)拷贝到/usr/include
再编译,出现错误2:ARG_MAX 没找到
在apue.h中加入#define ARG_MAX 4096
再编译:出现没找到
<apue.h>。在那个文件加入 #include <apue.h>就可以
(apue.2e/threadctl/getenv1.c 和apue.2e/threadctl/getenv3.c 一般这两个文件出现这点错误)
再编译,出现错误:
In file included from /usr/include/i386-linux-gnu/bits/time.h:86:0,
                 from /usr/include/time.h:42,
                 from /usr/include/pthread.h:26,
                 from printd.c:11:
/usr/include/i386-linux-gnu/bits/timex.h:31:7: 错误: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘.’ token


这是linux的time.h中定义全局变量status,在apue.h/ipp的ipp.h中也定义了,改成Status,
编译出错的时候将相应位置的status改成Status即可,再把apue.h/ipp/printd.c中的hp->status 改成hp->Status即可。

最后编译通过。


3、最后把apue.h/lib下的error.c放到/usr/include/中,用vi在apue.h的最后一行 #endif前 加一句#include"error.c "这就是书本中第一个实例定义的err_quit和err_sys输出方法。如果用gedit添加,可能还是会出现
“致命错误: error.c :没有那个文件或目录
编译中断。


接下来,书中的代码就可以使用了~~

PS:其实还可以更加简单,下载了apue.h以后,将apue.h放到
/usr/include/下,在/apue.2e/lib/error.c,将error.c复制到/usr/include/下,在apue.h的最后一行 #endif前 加一句#include"error.c ",即可。但是不保证能全部执行书上代码。
不过如一楼网友所言,这样简单做APUE的例子代码16_4的时候就出现了函数重定义的错误。本人没验证过,受教。



附第一个程序myls代码


#include "apue.h"#include <dirent.h>int main(int argc, char *argv[]){        DIR *dp;        struct dirent *dirp;        if(argc != 2)                err_quit("usage: ls directory_name");        if((dp = opendir(argv[1])) == NULL)                err_sys("can't open %s", argv[1]);        while((dirp = readdir(dp)) != NULL)                printf("%s\n", dirp->d_name);        closedir(dp);        exit(0);}


不使用apue.h版本:

#include <stdio.h>#include <dirent.h>#include <stdlib.h>int main(int argc, char *argv[]){    DIR *dp;    struct dirent *dirp;    if(argc != 2){         printf("You need input th directory name.\n");        exit(1);    }       if((dp = opendir(argv[1])) == NULL){        printf("cannot open %s\n",argv[1]);         exit(1);    }       while((dirp = readdir(dp)) != NULL)        printf("%s\n", dirp->d_name);    closedir(dp);    exit(0);}




0 0
原创粉丝点击