Linux 为什么在编译 K&R2 的第 1 章中的最长行示例时会出现“getline 类型冲突”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8763052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get a "conflicting types for getline" error when compiling the longest line example in chapter 1 of K&R2?
提问by Salar
Here is a program I'm trying to run straight from section 1.9 of "The C Programming Language".
这是我试图从“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] = '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
';
return i;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != 'int main(int argc, char **argv)
')
++i;
}
Here is the error I get when I try to compile the program using Ubuntu 11.10:
这是我尝试使用 Ubuntu 11.10 编译程序时遇到的错误:
int main(int argc, char *argv[])
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.
为了确保这本书的印刷没有问题,我在本书的章节练习后面引用了这组答案(http://users.powernet.co.uk/eton/kandr2/krx1. html) 并且当我尝试从该链接运行练习 18、19、20、21 等时,我遇到了类似的错误。当我无法运行程序以查看它们如何输出时,真的很难学习。这个问题是在一个程序中引入字符数组和函数调用时开始的。我很感激关于这个问题的任何建议。
采纳答案by Mysticial
The problem is that getline()
is a standard library function. (defined in stdio.h
) Your function has the same name and is thus clashing with it.
问题是这getline()
是一个标准库函数。(在 中定义stdio.h
)您的函数具有相同的名称,因此与它发生冲突。
The solution is to simply change the name.
解决方案是简单地更改名称。
回答by BlackBear
You have to change getline's name because it already exists.
您必须更改 getline 的名称,因为它已经存在。
回答by fge
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline' was here
/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.
那应该给你一个提示。尝试getline()
将代码中的函数重命名为其他名称。
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 for main()
: it does return an int, but has two arguments. You had better declare it as:
此外,以main()
这种方式声明是旧风格。默认情况下,没有声明返回类型和参数的函数接受未指定数量的参数并返回一个 int。几乎就是这种情况main()
:它确实返回一个 int,但有两个参数。你最好将它声明为:
$ gcc test.c -ansi
$ gcc test.c -std=c89
or:
或者:
$ gcc --version
gcc (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8)
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
回答by Sangeeth Saravanaraj
This is because the stdio.h
have a getline()
function.
这是因为stdio.h
有一个getline()
功能。
So a simple thing to make this work would be to rename your function to my_getline()
因此,使这项工作变得简单的一件事就是将您的函数重命名为 my_getline()
Both getline()
and getdelim()
were originally GNU
extensions. They were standardized in POSIX.1-2008
.
无论getline()
与getdelim()
最初GNU
的扩展。它们在POSIX.1-2008
.
回答by ouah
getline
is now a POSIX function declared in stdio.h
getline
现在是一个 POSIX 函数 stdio.h
Rename you getline
function to another name and it will compile.
将您的getline
函数重命名为另一个名称,它将编译。
回答by Mithrandir
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!
已经在“stdio.h”文件中定义了一个名为 getline 的函数。因此,原型冲突!将您的函数重命名为“my_getline”或其他名称,一切都应该没问题!
回答by moooeeeep
The conflicting function getline()
is a GNU/POSIX extension.
冲突函数getline()
是 GNU/POSIX 扩展。
K&R state that they address specifically ANSI C in their book (c.f.), which does not provide this function.
K&R 声明他们在他们的书(cf) 中专门针对 ANSI C,该书不提供此功能。
The authors present the complete guide to ANSI standard C language programming.
作者提供了 ANSI 标准 C 语言编程的完整指南。
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.
为了将 gcc 设置为“K&R 兼容模式”,您可以指定 ANSI 或 ISO 模式进行编译。这些旨在禁用扩展,例如,功能getline()
。这最终可以消除编辑 K&R 提供的其他示例的需要。
For example, the following compile just fine:
例如,以下编译就好了:
##代码##(Except that they complain about the implicit default return type of main()
with -Wall
.)
(除了他们抱怨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:
显然,在某些系统上,这些模式可能无法像这里介绍的那样工作(显然某些版本的 Mac OS 无法正确禁用所有扩展)。我在我的机器上成功测试了这个:
##代码##