LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 862|回复: 6

编译Unix环境高级编程一书中的代码出错

[复制链接]
发表于 2004-7-15 10:57:57 | 显示全部楼层 |阅读模式
以下是ls.c的代码
  1. #include <sys/types.h>
  2. #include <dirent.h>
  3. /* #include "ourhdr.h" */

  4. int
  5. main (int argc, char *argv[]) {
  6. DIR *dp;
  7. struct dirent *dirp;
  8. if (argc != 2)
  9. err_quit("a single argument (the directory name) is required");
  10. if (( dp = opendir (argv[1])) == 0)
  11. err_sys("can't open %s",argv[1]);

  12. while ( (dirp =readdir(dp)) != 0)
  13. printf("%s\n",dirp->d_name);

  14. closedir(dp);
  15. exit(0);
  16. }
复制代码

头文件ourhdr.h是自定义的,内容如下:
  1. /* Our own header, to be included *after* all standard system headers */

  2. #ifndef __ourhdr_h
  3. #define __ourhdr_h

  4. #include <sys/types.h> /* required for some of our prototypes */
  5. #include <stdio.h>     /* for convenience */
  6. #include <stdlib.h>    /* for convenience */
  7. #include <string.h>    /* for convenience */
  8. #include <unistd.h>    /* for convenience */

  9. #define MAXLINE 4096  /* max line length */

  10. #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
  11. /* default file access permissions for new files */

  12. #define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
  13. /* default permissions for new directories */

  14. typedef void Sigfunc(int);    /* for signal handlers */
  15. /* 4.3BSD Reno <signal.h> doesn't define SIG_ERR */

  16. #if defined(SIG_IGN) && !DEFINED(SIG_ERR)
  17. #define SIG_ERR ((Sigfunc *) -1)
  18. #endif

  19. #define min(a,b) ((a) < (b) ? (a) : (b))
  20. #define max(a,b) ((a) > (b) ? (a) : (b))

  21. /* prototypes for our own functions */
  22. char *path_alloc(int *);    /* Program 2.2 */
  23. int open_max(void);         /* Program 2.3 */
  24. void clr_fl(int, int);      /* Program 3.5 */
  25. void set_fl(int, int);      /* Program 3.5 */
  26. void pr_exit(int);          /* Program 8.3 */
  27. void pr_mask(const char *); /* Program 10.10 */
  28. Sigfunc *signal_intr(int, Sigfunc *); /* Program 10.13 */

  29. int tty_cbreak(int);        /* Program 11.10 */
  30. int tty_raw(int);           /* Program 11.10 */
  31. int tty_reset(int);         /* Program 11.10 */
  32. int tty_atexit(int);        /* program 11.10 */
  33. #ifdef ECHO                         /* only if <termios.h> has been included */
  34. struct termios *tty_termios(void); /* Program 11.10 */
  35. #endif

  36. void sleep_us(unsigned int);        /* Exercise 12.6 */
  37. ssize_t readn(int, void *, size_t); /* Program 12.13 */
  38. ssize_t writen(int, const void *,size_t); /* Program 12.12 */
  39. int daemon_init(void);                /* Programs 13.1 */
  40. int s_pipe(int *);                /* Programs 15.2  and 15.3 */
  41. int recv_fd(int, ssize_t (*func)(int, const void *, size_t));
  42. /* Program 15.6 and 15.8 */
  43. int send_fd(int, int);                /* Programs 15.5 and 15.7 */
  44. int send_err(int, int, const char *) /* Programs 15.20 */
  45. int serv_listen(const char *); /* Programs 15.19 and 15.22 */
  46. int serv_accept(int, uid_t *);        /* Programs 15.20 and 15.24 */
  47. int cli_conn(const char *);        /* Programs 15.21 and 15.23 */
  48. int buf_args(char *, int (*func)(int,char **));        /* Program 15.17 */

  49. int ptym_open(char *);                /* Programs 19.1 and 19.2 */
  50. int ptys_open(int, char *);        /* Programs 19.1 ans 19.2 */
  51. #ifdef TIOCGWINSZ
  52. pid_z pty_fork(int *, char *, const struct termios *, const struct winsize *);
  53. /* Program 19.3 */
  54. #endif

  55. int lock_fork(int, int, int, off_t, int, off_t); /* Programs 12.2 */
  56. #define read_lock(fd, offset, whence, len) \
  57. lock_reg(fd, F_SETLK, F_RDLCK, offset, whence, len)
  58. #define readw_lock(fd, offset, whence, len) \
  59. lock_reg(fd, F_SETLKW, F_RDLCK, offset, whence, len)
  60. #define write_lock(fd, offset, whence, len) \
  61. lock_reg(fd, F_SETLK, F_WRLCK, offset, whence, len)
  62. #define writew_lock(fd, offset, whence, len) \
  63. lock_reg(fd, F_SETLKW, F_WRLCK, offset, whence, len)
  64. #define un_lock(fd, offset, whence, len) \
  65. lock_reg(fd, F_SETLK, F_UNLCK, offset, whence, len)

  66. pid_t lock_test(int, int, off_t, int, off_t); /* Program 12.3 */

  67. #define is_readlock(fd, offset, whence, len) \
  68. lock_test(fd, F_RDLCK, offset, whence, len)
  69. #define is_writelock(fd, offset, whence, len) \
  70. lock_test(fd, F_WRLCK, offset, whence, len)

  71. void err_dump(const char *, ...); /* Appendix B */
  72. void err_msg(const char *, ...);
  73. void err_quit(const char *, ...);
  74. void err_ret(const char *, ...);
  75. void err_sys(const char *, ...);

  76. void log_msg(const char *, ...); /* Appendix B */
  77. void log_open(const char *, int, int);
  78. void log_quit(const char *, ...);
  79. void log_ret(const char *, ...);
  80. void log_sys(const char *, ...);

  81. void TELL_WAIT(void);                /* parent/child form Section 8.8 */
  82. void TELL_PARENT(pid_t);
  83. void TELL_CHILD(pid_t);
  84. void WAIT_PARENT(void);
  85. void WAIT_CHILD(void);

  86. #endif /* __ourhdr_h */
复制代码


使用下面的命令编译ls.c文件:
gcc -o ls ls.c -I/root/program
其中/root/program目录下有ourhdr.h文件
出错信息如下:
  1. cd /root/program/
  2. gcc -o ls ls.c -I/root/program
  3. /tmp/ccCUiKUZ.o: In function `main':
  4. /tmp/ccCUiKUZ.o(.text+0x1f): undefined reference to `err_quit'
  5. /tmp/ccCUiKUZ.o(.text+0x53): undefined reference to `err_sys'
  6. collect2: ld returned 1 exit status

  7. Compilation exited abnormally with code 1 at Thu Jul 15 10:59:25
复制代码


是哪出了问题,请指教:thank
发表于 2004-7-15 11:19:10 | 显示全部楼层
看看这一句,你不是把头文件已经注释掉了吗?

  1. /* #include "ourhdr.h" */
复制代码
发表于 2004-7-15 12:23:38 | 显示全部楼层
undefined reference to `err_quit'
找不到函数的实现,楼主给的源码还少了另一个.c文件

不如不用err_quit和err_sys, 改成直接printf然后exit,试起来方便些
 楼主| 发表于 2004-7-15 13:41:49 | 显示全部楼层
最初由 kj501 发表
看看这一句,你不是把头文件已经注释掉了吗?

  1. /* #include "ourhdr.h" */
复制代码



sorry,刚学emacs不久,有些热键不熟搞混了

我去掉注释后还是不行,出错如下:
  1. cd /root/program/
  2. gcc -o ls ls.c -I/root/program
  3. /tmp/ccCOLn3d.o: In function `main':
  4. /tmp/ccCOLn3d.o(.text+0x1f): undefined reference to `err_quit'
  5. /tmp/ccCOLn3d.o(.text+0x53): undefined reference to `err_sys'
  6. collect2: ld returned 1 exit status

  7. Compilation exited abnormally with code 1 at Thu Jul 15 13:41:46
复制代码
发表于 2004-7-15 15:59:02 | 显示全部楼层
/tmp/ccCOLn3d.o(.text+0x1f): undefined reference to `err_quit'
/tmp/ccCOLn3d.o(.text+0x53): undefined reference to `err_sys'
把这两个函数的实现代码加到你的程序中去,或者不要用这两个函数了
 楼主| 发表于 2004-7-15 16:13:22 | 显示全部楼层
最初由 doubleelec 发表
/tmp/ccCOLn3d.o(.text+0x1f): undefined reference to `err_quit'
/tmp/ccCOLn3d.o(.text+0x53): undefined reference to `err_sys'
把这两个函数的实现代码加到你的程序中去,或者不要用这两个函数了


thanks,我想是还有一个头文件没包含吧,让我再看看,多谢
发表于 2004-7-15 19:42:17 | 显示全部楼层
是没有链接库文件。这些文件应该可以到作者的主页上去下载。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表