LinuxSir.cn,穿越时空的Linuxsir!

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

(新手提问)C语言程序第二版中第5章第6节sort例子无法在linux下编译

[复制链接]
发表于 2007-2-16 15:52:43 | 显示全部楼层 |阅读模式
The C Programming Language第二版中第5章第6节sort例子无法在linux下编译
出错信息如下:
/tmp/cc9i87Qf.o(.text+0xc8): In function `readlines':
: undefined reference to `alloc'
collect2: ld returned 1 exit status

好像是alloc未定义,请问怎么解决?
相关代码如下:

  1. #define MAXLEN 1000 /* 每个输入行的最大长度 */
  2. int getline(char *, int);
  3. char *alloc(int);

  4. /* readlines 函数:读取输入行 */
  5. int readlines(char *lineptr[], int maxlines)
  6. {
  7.         int len, nlines;
  8.         char *p, line[MAXLEN];
  9.         nlines = 0;
  10.         while ((len = getline(line, MAXLEN)) > 0)
  11.                 if (nlines >= maxlines  || (p = alloc(len)) == NULL)
  12.                         return -1;
  13.                 else {
  14.                         line[len-1] = '\0';                /* 删除换行符 */
  15.                         strcpy(p, line);
  16.                         lineptr[nlines++] = p;
  17.                 }
  18.         return nlines;
  19. }
复制代码
发表于 2007-2-16 17:20:56 | 显示全部楼层

alloc应该是书上的自己定义的吧

见这一句:
char *alloc(int);
………………………………
不过没看到它的实现,可能在其它章节中,
其实可以用
#define alloc(A) malloc (A)
先试用起来

gcc -o test test1.c
test1.c: In function ‘readlines’:
test1.c:20: warning: incompatible implicit declaration of built-in function ‘strcpy’
/tmp/ccSz0uGd.o: In function `readlines':
test1.c.text+0x52):对‘alloc’未定义的引用
collect2: ld returned 1 exit status
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-2-16 18:07:04 | 显示全部楼层
thz.....
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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