linux下搭建unix編譯環境

確保你已經安裝了gcc和gawk。
 
步驟
 
1.到www.apuebook.com下載源碼
 
2.tar解包,cd apue.2e
 
3.vi Make.defines.linux   修改變量WKDIR,指向你的apue源碼的位置,我的是/home/huangz/code/apue.2e,所以
  WKDIR=/home/huangz/code/apue.2e
 
4.vi include/apue.h  增加一個常量ARG_MAX,這是threadctl/getenv1.c和threadctl/getenv3.c要用到的;4096這個值是參考裏給的,如果有問題,自己修改吧。
  #define ARG_MAX 4096
 
5.vi threadctl/getenv1.c   增加
  #include "apue.h"
 
6.vi threadctl/getenv3.c   增加
  #include "apue.h"
 
7.vi threads/badexit2.c   修改第31行,將pthread_self()的返回值轉換爲int類型。
  printf("thread 2: ID is %d\n", (int)pthread_self());
 
8.vi std/linux.mk   將兩個nawk改爲gawk
 
9.make
 
10、如果出現:stropts.h找不到的情況,則下載glibc-2.8,解壓縮
11. cp include/apue.h /usr/include
      cp lib/libapue.a /usr/lib
 好了,測試一下,記得要用-lapue命令讓編譯器鏈接apue庫
gcc main.c -lapue
12.如果此時在編譯其他程序還出現錯誤:
: undefined reference to `err_quit'
: undefined reference to `err_sys'
collect2: ld returned 1 exit status
解決辦法:
err_quit跟err_sys是作者自己定義的錯誤處理函數,需要單獨定義頭文件
在/usr/include 下新建一個名爲myerr.h的文件
拷貝下邊的內容到myerr.h(其實此頭文件在原書的附錄B中)
QUOTE:#include "apue.h"
#include   <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 就好了。
               

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章