解決unix環境進階編程的第一個程式運行問題

來源:互聯網
上載者:User

想學unix環境進階編程,雖然已有《unix環境進階編程》已有第三版,但網上只找到第二版的電子書,在下很窮買起書,只得看第二版的電子書了。

    看到書上第第一個列出指定目錄的內容的那個例子,其實就是shell中  ls  的內容,讓我受到了點小打擊

[root@localhost ~]# vi ls.c #include "apue.h"#include <dirent.h>intmain(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 directoryls.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 oncels.c:13: error: for each function it appears in.)ls.c:16: warning: incompatible implicit declaration of built-in function aprintfals.c:19: warning: incompatible implicit declaration of built-in function aexita[root@localhost ~]# 

  哎,想要運行一個程式咋就這麼難啊,我百度下,居然讓我找到瞭解決方法。。。

參考解決方案主要來自2個頁面,下面給出連結http://www.linuxdiyf.com/bbs/thread-90655-1-8.htmlhttp://hi.chinaunix.net/?uid-14367145-action-viewspace-itemid-223731.APUE2原始碼下載:http://www.apuebook.com/src.tar.gz2.我儲存到了/root下.解壓縮:tar -xzvf src.tar.gz3.cd apue.2e進入apue.2e目錄,查看README,告訴我們linux系統只要修改Make.defines.linux再make4.vi Make.defines.linux 修改WKDIR=/root/apue.2e 就是說工作目錄為WKDIR=/root/apue.2e5.修改/root/apue.2e/std/linux.mk把全部的nawk改為awk.因些linux預設沒有nawk6.makeerr_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.confissuephp.dsysconfiggtk-2.0localtimelogin.defsrc5.drc4.dpinforcsasl2pmbashrcntop    第一個程式終於跑起來了
轉自:http://world77.blog.51cto.com/414605/458442

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.