4

我正在使用 K&R,使用 Clang 作为我的编译器。

使用 Clang 编译时,练习 1-16 会产生“'getline' 的类型冲突”错误。我猜是因为默认库之一有一个 getline 函数。

在编译 K&R 练习时,我应该将哪些选项传递给 Clang,以避免包含其他任何内容?

需要修改的练习样本为:

#include <stdio.h>
#define MAXLINE 1000

int getline(char line[], int maxline);
void copy(char to[], char from[]);

/* print longest input line */
main()
{
  int len; /* current line length */
  int max; /* maximum line lenght seen so far */
  char line[MAXLINE]; /* current input line */
  char longest[MAXLINE]; /* longest line saved here */

  max = 0;

  while ((len = getline(line, MAXLINE)) > 0)
    if ( len > max) {
      max = len;
      copy(longest, line); /* line -> longest */
    }

  if (max > 0) /* there was a line */
    printf("\n\nLength: %d\nString: %s", max -1, longest);
  return 0;
}

/* getline: read a line into s, return length */
int getline(char s[], int lim)
{
  int c,i;

  for (i=0; i<lim-1 && (c=getchar()) != EOF && c!='\n'; ++i)
    s[i] = c;

  if (c == '\n') {
    s[i] = c;
    ++i;
  }

  s[i] = '\0';
  return i;
}

/* copy: copy "from" into "to"; assume to is big enough */
void copy(char to[], char from[])
{
  int i;

  i = 0;

  while((to[i] = from[i]) != '\0')
    ++i;
}

调用时来自 Clang 的错误:cc ex1-16.c -o ex1-16

ex1-16.c:4:5: error: conflicting types for 'getline'
int getline(char line[], int maxline);
    ^
/usr/include/stdio.h:449:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE *...
        ^
ex1-16.c:17:38: error: too few arguments to function call, expected 3, have 2
  while ((len = getline(line, MAXLINE)) > 0)
                ~~~~~~~              ^
/usr/include/stdio.h:449:1: note: 'getline' declared here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE *...
^
ex1-16.c:29:5: error: conflicting types for 'getline'
int getline(char s[], int lim)
    ^
/usr/include/stdio.h:449:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE *...
        ^
3 errors generated.
4

2 回答 2

6

问题只是您的系统已经提供了一个名为getline. man getline应该告诉你它的签名。在我的系统上是:

ssize_t getline(char ** restrict linep, size_t * restrict linecapp, FILE * restrict stream);

您可以匹配它,也可以将您的函数重命名为“mygetline”或类似的名称。

或者,如果您可以避免包含stdio.h,则可以完全避免该问题。

至于你的最后一个问题:

在编译 K&R 练习时,我应该将哪些选项传递给 Clang,以避免包含其他任何内容?

你不能 - 系统头文件就是它们的样子,并且可能自 K&R 在 1988 年最后一次修订以来一直在继续。从那时起,已经有多个 C 标准更新。在某些方面,K&R 真的开始长进了。

于 2013-07-02T20:00:21.433 回答
1

这里有一个类似的问题:为什么在编译 K&R2 的第 1 章中最长的行示例时会出现“getline 的冲突类型”错误?

这是同样的问题,但使用 gcc。一种解决方案是将编译器置于禁用 GNU/POSIX 扩展的 ANSI C 模式。

尝试以下操作:

$ clang test.c -ansi 

或者

$ clang test.c -std=c89

在我的机器上测试成功:

$ clang --version
clang version 3.3 (tags/RELEASE_33/rc2)
Target: x86_64-redhat-linux-gnu
Thread model: posix

在我大学的机器上使用这个编译器,甚至不需要为成功编译指定 ANSI 模式:

->clang --version
Apple clang version 1.7 (tags/Apple/clang-77) (based on LLVM 2.9svn)
Target: x86_64-apple-darwin10
Thread model: posix
于 2013-07-02T20:00:06.737 回答