《UNIX环境高级编程》编译问题彻底解决方案

前些天,写了篇博客( 最后一年的时间,应该做些什么呢? )发誓要好好学一下《UNIX环境高级编译》(下面简称APUE),可一个小小的编译问题就悲剧了我好几天,比如APUE书上的第一个程序是这样的:

  1. #include "apue.h"  
  2. #include <dirent.h>  
  3. int  
  4. main(int argc, char *argv[])  
  5. {  
  6.     DIR *dp;  
  7.     struct dirent *dirp;  
  8.     if(argc != 2)  
  9.     {  
  10.         err_quit("usage: ls directory_name");  
  11.     }  
  12.     if((dp = opendir(argv[1])) == NULL)  
  13.     {  
  14.         err_sys("can`t open %s", argv[1]);  
  15.     }  
  16.     while((dirp = readdir(dp)) != NULL)  
  17.     {  
  18.         printf("%s/n", dirp->d_name);  
  19.     }  
  20.     closedir(dp);  
  21.     exit(0);  
  22. }  

 

     直接这样用gcc编译会很悲剧的出现一大堆错误,错误这里就不罗列出来了。

     其实细心的朋友会发现本程序第一行包含了一个apue.h的头文件,这个头文件在/usr/include可是没有的哦,也就是说这不是标准头文件,其实从它用双引号包含也可以看出来。那这个头文件在哪呢?

     附录里。

     找到了问题,解决就好办了,把附录里的apue.h和error.c敲进电脑里,我敲好了,懒得就直接复制吧。

  1. /* Our own header, to be included before all standard system headers */  
  2. #ifndef _APUE_H  
  3. #define _APUE_H  
  4. #if defined(SOLARIS)  
  5. #define _XOPEN_SOURCE   500     /* Single UNIX Specification, Version 2  for Solaris 9 */  
  6. #define CMSG_LEN(x)     _CMSG_DATA_ALIGN(sizeof(struct cmsghdr)+(x))  
  7. #elif !defined(BSD)  
  8. #define _XOPEN_SOURCE   600     /* Single UNIX Specification, Version 3 */  
  9. #endif  
  10. #include <sys/types.h>          /* some systems still require this */  
  11. #include <sys/stat.h>  
  12. #include <sys/termios.h>        /* for winsize */  
  13. #ifndef TIOCGWINSZ  
  14. #include <sys/ioctl.h>  
  15. #endif  
  16. #include <stdio.h>              /* for convenience */  
  17. #include <stdlib.h>             /* for convenience */  
  18. #include <stddef.h>             /* for offsetof */  
  19. #include <string.h>             /* for convenience */  
  20. #include <unistd.h>             /* for convenience */  
  21. #include <signal.h>             /* for SIG_ERR */  
  22. #include <errno.h>  
  23. #include <stdarg.h>  
  24. #include <syslog.h>  
  25. #include <error.h>  
  26. #define MAXLINE 4096                    /* max line length */  
  27. /****/  
  28. /* 
  29.  * Default file access permissions for new files. 
  30.  */  
  31. #define FILE_MODE       (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)  
  32. /* 
  33.  * Default permissions for new directories. 
  34.  */  
  35. #define DIR_MODE        (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)  
  36. typedef void    Sigfunc(int);   /* for signal handlers */  
  37. #if     defined(SIG_IGN) && !defined(SIG_ERR)  
  38. #define SIG_ERR ((Sigfunc *)-1)  
  39. #endif  
  40. #define min(a,b)        ((a) < (b) ? (a) : (b))  
  41. #define max(a,b)        ((a) > (b) ? (a) : (b))  
  42. /* 
  43.  * Prototypes for our own functions. 
  44.  */  
  45. char    *path_alloc(int *);                             /* {Prog pathalloc} */  
  46. long     open_max(void);                                /* {Prog openmax} */  
  47. void     clr_fl(intint);                              /* {Prog setfl} */  
  48. void     set_fl(intint);                              /* {Prog setfl} */  
  49. void     pr_exit(int);                                  /* {Prog prexit} */  
  50. void     pr_mask(const char *);                 /* {Prog prmask} */  
  51. Sigfunc *signal_intr(int, Sigfunc *);   /* {Prog signal_intr_function} */  
  52. int              tty_cbreak(int);                               /* {Prog raw} */  
  53. int              tty_raw(int);                                  /* {Prog raw} */  
  54. int              tty_reset(int);                                /* {Prog raw} */  
  55. void     tty_atexit(void);                              /* {Prog raw} */  
  56. #ifdef  ECHO    /* only if <termios.h> has been included */  
  57. struct termios  *tty_termios(void);             /* {Prog raw} */  
  58. #endif  
  59. void     sleep_us(unsigned int);                        /* {Ex sleepus} */  
  60. ssize_t  readn(intvoid *, size_t);            /* {Prog readn_writen} */  
  61. ssize_t  writen(intconst void *, size_t);     /* {Prog readn_writen} */  
  62. void     daemonize(const char *);                       /* {Prog daemoninit} */  
  63. int              s_pipe(int *);                                 /* {Progs streams_spipe sock_spipe} */  
  64. int              recv_fd(int, ssize_t (*func)(int,  
  65.                          const void *, size_t));/* {Progs recvfd_streams recvfd_sockets} */  
  66. int              send_fd(intint);                             /* {Progs sendfd_streams sendfd_sockets} */  
  67. int              send_err(intint,  
  68.                           const char *);                /* {Prog senderr} */  
  69. int              serv_listen(const char *);             /* {Progs servlisten_streams servlisten_sockets} */  
  70. int              serv_accept(int, uid_t *);             /* {Progs servaccept_streams servaccept_sockets} */  
  71. int              cli_conn(const char *);                /* {Progs cliconn_streams cliconn_sockets} */  
  72. int              buf_args(char *, int (*func)(int,  
  73.                           char **));                    /* {Prog bufargs} */  
  74. int              ptym_open(char *, int);        /* {Progs3 ptyopen_streams ptyopen_bsd ptyopen_linux} */  
  75. int              ptys_open(char *);                     /* {Progs3 ptyopen_streams ptyopen_bsd ptyopen_linux} */  
  76. #ifdef  TIOCGWINSZ  
  77. pid_t    pty_fork(int *, char *, intconst struct termios *,  
  78.                           const struct winsize *);              /* {Prog ptyfork} */  
  79. #endif  
  80. int             lock_reg(intintint, off_t, int, off_t); /* {Prog lockreg} */  
  81. #define read_lock(fd, offset, whence, len) /  
  82.                         lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))  
  83. #define readw_lock(fd, offset, whence, len) /  
  84.                         lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))  
  85. #define write_lock(fd, offset, whence, len) /  
  86.                         lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))  
  87. #define writew_lock(fd, offset, whence, len) /  
  88.                         lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))  
  89. #define un_lock(fd, offset, whence, len) /  
  90.                         lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))  
  91. pid_t   lock_test(intint, off_t, int, off_t);         /* {Prog locktest} */  
  92. #define is_read_lockable(fd, offset, whence, len) /  
  93.                         (lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)  
  94. #define is_write_lockable(fd, offset, whence, len) /  
  95.                         (lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)  
  96. void    err_dump(const char *, ...);            /* {App misc_source} */  
  97. void    err_msg(const char *, ...);  
  98. void    err_quit(const char *, ...);  
  99. void    err_exit(intconst char *, ...);  
  100. void    err_ret(const char *, ...);  
  101. void    err_sys(const char *, ...);  
  102. void    log_msg(const char *, ...);                     /* {App misc_source} */  
  103. void    log_open(const char *, intint);  
  104. void    log_quit(const char *, ...);  
  105. void    log_ret(const char *, ...);  
  106. void    log_sys(const char *, ...);  
  107. void    TELL_WAIT(void);                /* parent/child from {Sec race_conditions} */  
  108. void    TELL_PARENT(pid_t);  
  109. void    TELL_CHILD(pid_t);  
  110. void    WAIT_PARENT(void);  
  111. void    WAIT_CHILD(void);  
  112. #endif  /* _APUE_H */  

 

  1. #include "apue.h"  
  2. #include <errno.h>      /* for definition of errno */  
  3. #include <stdarg.h>     /* ISO C variable aruments */  
  4. static void err_doit(intintconst char *, va_list);  
  5. /* 
  6.  * Nonfatal error related to a system call. 
  7.  * Print a message and return. 
  8.  */  
  9. void  
  10. err_ret(const char *fmt, ...)  
  11. {  
  12.     va_list     ap;  
  13.     va_start(ap, fmt);  
  14.     err_doit(1, errno, fmt, ap);  
  15.     va_end(ap);  
  16. }  
  17. /* 
  18.  * Fatal error related to a system call. 
  19.  * Print a message and terminate. 
  20.  */  
  21. void  
  22. err_sys(const char *fmt, ...)  
  23. {  
  24.     va_list     ap;  
  25.     va_start(ap, fmt);  
  26.     err_doit(1, errno, fmt, ap);  
  27.     va_end(ap);  
  28.     exit(1);  
  29. }  
  30. /* 
  31.  * Fatal error unrelated to a system call. 
  32.  * Error code passed as explict parameter. 
  33.  * Print a message and terminate. 
  34.  */  
  35. void  
  36. err_exit(int error, const char *fmt, ...)  
  37. {  
  38.     va_list     ap;  
  39.     va_start(ap, fmt);  
  40.     err_doit(1, error, fmt, ap);  
  41.     va_end(ap);  
  42.     exit(1);  
  43. }  
  44. /* 
  45.  * Fatal error related to a system call. 
  46.  * Print a message, dump core, and terminate. 
  47.  */  
  48. void  
  49. err_dump(const char *fmt, ...)  
  50. {  
  51.     va_list     ap;  
  52.     va_start(ap, fmt);  
  53.     err_doit(1, errno, fmt, ap);  
  54.     va_end(ap);  
  55.     abort();        /* dump core and terminate */  
  56.     exit(1);        /* shouldn't get here */  
  57. }  
  58. /* 
  59.  * Nonfatal error unrelated to a system call. 
  60.  * Print a message and return. 
  61.  */  
  62. void  
  63. err_msg(const char *fmt, ...)  
  64. {  
  65.     va_list     ap;  
  66.     va_start(ap, fmt);  
  67.     err_doit(0, 0, fmt, ap);  
  68.     va_end(ap);  
  69. }  
  70. /* 
  71.  * Fatal error unrelated to a system call. 
  72.  * Print a message and terminate. 
  73.  */  
  74. void  
  75. err_quit(const char *fmt, ...)  
  76. {  
  77.     va_list     ap;  
  78.     va_start(ap, fmt);  
  79.     err_doit(0, 0, fmt, ap);  
  80.     va_end(ap);  
  81.     exit(1);  
  82. }  
  83. /* 
  84.  * Print a message and return to caller. 
  85.  * Caller specifies "errnoflag". 
  86.  */  
  87. static void  
  88. err_doit(int errnoflag, int error, const char *fmt, va_list ap)  
  89. {  
  90.     char    buf[MAXLINE];  
  91.    vsnprintf(buf, MAXLINE, fmt, ap);  
  92.    if (errnoflag)  
  93.        snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",  
  94.          strerror(error));  
  95.    strcat(buf, " ");  
  96.    fflush(stdout);     /* in case stdout and stderr are the same */  
  97.    fputs(buf, stderr);  
  98.    fflush(NULL);       /* flushes all stdio output streams */  
  99. }  

 

这样涉及到两个文件的同时编译问题,如果你更懒,那么还有办法解决。

 

给个批处理用用吧。

  1. #!/bin/sh  
  2. #gg  
  3. #以下是修改的gg脚本,添加了一个函数,indent每一个.c和.h的功能  
  4. #wraper the gcc  
  5. #set -x  
  6. #用indent所有的c文件和头文件(因为我向ssh中拷代码的时候缩进总是出问题,所以需要indent)  
  7. go2indent()  
  8. {  
  9.    for tt in $*  
  10.    do  
  11. char=`echo $tt|cut -d. -f2`  
  12. if [ $char = "c" -o $char = "h" ]; then  
  13.    char=""  
  14.    #此处indent格式自己修改。  
  15.    indent -ts2 -bli0 $tt  
  16.    rm $tt~ >/dev/null 2>&1  
  17. fi  
  18.    done  
  19. }  
  20. #不需要indent的话,直接注释下面这条语句  
  21. go2indent $*  
  22. exec_obj=`echo $1|cut -d. -f1`  
  23. if gcc error.c $* -Wall -o $exec_obj; then  
  24.    echo "gcc Done! Create executable file /"$exec_obj/"."  
  25. else  
  26.    echo "gcc failed!"  
  27.    exit 1  
  28. fi  
  29. #end  

 

把批处理命名为gg,是不是迫不及待的想试试了?

不过,还有等一下,应该此时你还没有执行权限,敲命令sudo chmod 755 gg提升权限。然后别忘了用ls -l看一下,如果是该文件权限是-rwxr-xr-x,即本用户和超级用户读写执行,本组用户读执行,其他用户执行。

 

以后只要把这3个文件拷贝到你编写的APUE例程文件夹中,以./gg 文件名,来进行编译就可以了

这样操作就可以了。

 

好了,可以好好的,认真的,仔细的,开始看伟大的著作APUE喽。

 

但愿可以学好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值