您的位置:首页 > 编程语言

解决unix环境高级编程的第一个程序运行问题

2013-06-21 23:17 399 查看
想学unix环境高级编程,虽然已有《unix环境高级编程》已有第三版,但网上只找到第二版的电子书,在下很穷买起书,只得看第二版的电子书了。
看到书上第第一个列出指定目录的内容的那个例子,其实就是shell中 ls 的内容,让我受到了点小打击

[root@localhost ~]# vi ls.c
#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);
}
~
~
~
"ls.c" 20L, 458C written
[root@localhost ~]# gcc ls.c
ls.c:1:18: error: apue.h: No such file or directory
ls.c: In function amaina:
ls.c:13: error: aNULLa undeclared (first use in this function)
ls.c:13: error: (Each undeclared identifier is reported only once
ls.c:13: error: for each function it appears in.)
ls.c:16: warning: incompatible implicit declaration of built-in function aprintfa
ls.c:19: warning: incompatible implicit declaration of built-in function aexita
[root@localhost ~]#

哎,想要运行一个程序咋就这么难啊,我百度下,居然让我找到了解决方法。。。
参考解决方法主要来自2个页面,下面给出链接
http://www.linuxdiyf.com/bbs/thread-90655-1-8.html

http://hi.chinaunix.net/?uid-14367145-action-viewspace-itemid-22373

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

err_quit跟err_sys是作者自己定义的错误处理函数,需要单独定义头文件

在/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 <myerr.h>

就好了。

这个时候我们在回去看看我们的第一个程序:

[root@localhost ~]# gcc ls.c
[root@localhost ~]# ./ls.out /etc/
.
..
pam_smb.conf
issue
php.d
sysconfig
gtk-2.0
localtime
login.defs
rc5.d
rc4.d
pinforc
sasl2
pm
bashrc
ntop

第一个程序终于跑起来了

转自:http://world77.blog.51cto.com/414605/458442
http://www.linuxdiyf.com/bbs/thread-90655-1-8.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: