为什么我会收到“冲突的 getline 类型”信息编译 K&R2 第 1 章中最长行示例时出错?
这是我试图直接从“C 编程语言”第 1.9 节运行的程序。
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max) {
max = len;
copy(longest, line);
}
if (max > 0)
printf("%s", longest);
return 0;
}
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;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
这是当我尝试使用 Ubuntu 11.10 编译程序时遇到的错误:
cc word.c -o word
word.c:4:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
word.c:26:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
make: *** [word] Error 1
为了确保这不是书中打印的问题,我引用了书中章节后面练习的这套答案(http ://users.powernet.co.uk/eton/kandr2/krx1.html),当我尝试从该链接运行练习 18、19、20、21 等时,我收到类似的错误。当我无法运行程序来查看它们的输出时,学习真的很难。这个问题是在一个程序中引入字符数组和函数调用时开始的。对于这个问题的任何建议,我将不胜感激。
Here is a program I'm trying to run straight from section 1.9 of "The C Programming Language".
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max) {
max = len;
copy(longest, line);
}
if (max > 0)
printf("%s", longest);
return 0;
}
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;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
Here is the error I get when I try to compile the program using Ubuntu 11.10:
cc word.c -o word
word.c:4:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
word.c:26:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
make: *** [word] Error 1
Just to make sure it wasn't a problem with the print in the book, I referenced this set of answers to back of chapter exercises from the book (http://users.powernet.co.uk/eton/kandr2/krx1.html) and I get a similar error when I try to run exercises 18, 19, 20, 21, etc., from that link. It's really hard to learn when I can't run the programs to see how they output. This issue started when introducing character arrays and function calls in one program. I'd appreciate any advice on this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
问题是
getline()
是一个标准库函数。 (在stdio.h
中定义)您的函数具有相同的名称,因此与其发生冲突。解决办法就是简单地更改名称。
The problem is that
getline()
is a standard library function. (defined instdio.h
) Your function has the same name and is thus clashing with it.The solution is to simply change the name.
冲突函数
getline()
是 GNU/POSIX 扩展。K&R 声明他们在其书中专门讨论了 ANSI C (cf),这确实不提供此功能。
为了将 gcc 设置为“K&R 兼容模式”,您可以指定 ANSI 或 ISO 模式进行编译。这些旨在禁用扩展,例如函数
getline()
。这最终也可以消除编辑 K&R 提供的其他示例的需要。
例如,以下编译就可以了:(
除了他们抱怨
main()
和-Wall
的隐式默认返回类型。)显然在某些系统上,这些模式可能无法按照此处介绍的方式工作(显然某些版本的 Mac OS 无法正确禁用所有扩展)。我在我的机器上测试成功:
The conflicting function
getline()
is a GNU/POSIX extension.K&R state that they address specifically ANSI C in their book (c.f.), which does not provide this function.
In order set gcc into "K&R compatibility mode" you can specify the ANSI or ISO modes for compilation. These are intended to disable extensions, e.g., the function
getline()
.This could eventually eliminate the need to edit other examples provided by K&R as well.
For example, the following compile just fine:
(Except that they complain about the implicit default return type of
main()
with-Wall
.)Apparently on some systems, these modes may not work as presented here (apparently some version(s) of Mac OS fail to correctly disable all extensions). I tested this successfully on my machine:
这是因为
stdio.h
有一个getline()
函数。因此,要实现此功能,一个简单的方法是将函数重命名为
my_getline()
getline()
和getdelim()
最初都是 <代码>GNU 扩展。它们在POSIX.1-2008
中标准化。This is because the
stdio.h
have agetline()
function.So a simple thing to make this work would be to rename your function to
my_getline()
Both
getline()
andgetdelim()
were originallyGNU
extensions. They were standardized inPOSIX.1-2008
./usr/include/stdio.h:671:20: 注意:之前的“getline”声明在这里
这应该会给你一个提示。尝试将代码中的 getline() 函数重命名为其他名称。
另外,以这种方式声明
main()
是旧风格。默认情况下,没有声明返回类型和参数的函数接受未指定数量的参数并返回 int。main()
几乎就是这种情况:它确实返回一个 int,但有两个参数。你最好将其声明为:或:
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
That should give you a hint. Try and rename the
getline()
function in the code to something else.Also, declaring
main()
this way is old style. A function with no declared return type and arguments, by defaults, accepts an unspecified number of arguments and returns an int. This is nearly the case formain()
: it does return an int, but has two arguments. You had better declare it as:or:
getline
现在是在stdio.h
中声明的 POSIX 函数将
getline
函数重命名为另一个名称,它将进行编译。getline
is now a POSIX function declared instdio.h
Rename you
getline
function to another name and it will compile.您必须更改 getline 的名称,因为它已经存在。
You have to change getline's name because it already exists.
“stdio.h”文件中已经定义了一个名为 getline 的函数。因此原型发生冲突!
将您的函数重命名为“my_getline”或其他名称,一切都应该没问题!
The is already a function called getline defined in the "stdio.h" file. Thus a conflict in prototypes!
Rename your function to "my_getline" or some other name and all should be fine!