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 就好了。
               

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