为什么我会收到“冲突的 getline 类型”信息编译 K&R2 第 1 章中最长行示例时出错?

发布于 2024-12-25 14:13:18 字数 1417 浏览 1 评论 0原文

这是我试图直接从“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

娇纵 2025-01-01 14:13:18

问题是 getline() 是一个标准库函数。 (在 stdio.h 中定义)您的函数具有相同的名称,因此与其发生冲突。

解决办法就是简单地更改名称。

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.

The solution is to simply change the name.

谢绝鈎搭 2025-01-01 14:13:18

冲突函数 getline() 是 GNU/POSIX 扩展。

K&R 声明他们在其书中专门讨论了 ANSI C (cf),这确实不提供此功能。

作者提供了 ANSI 标准 C 语言编程的完整指南。

为了将 gcc 设置为“K&R 兼容模式”,您可以指定 ANSI 或 ISO 模式进行编译。这些旨在禁用扩展,例如函数getline()
这最终也可以消除编辑 K&R 提供的其他示例的需要。

例如,以下编译就可以了:(

$ gcc test.c -ansi
$ gcc test.c -std=c89

除了他们抱怨 main()-Wall 的隐式默认返回类型。)

显然在某些系统上,这些模式可能无法按照此处介绍的方式工作(显然某些版本的 Mac OS 无法正确禁用所有扩展)。我在我的机器上测试成功:

$ 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.

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.

The authors present the complete guide to ANSI standard C language programming.

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:

$ gcc test.c -ansi
$ gcc test.c -std=c89

(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:

$ 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.
指尖微凉心微凉 2025-01-01 14:13:18

这是因为 stdio.h 有一个 getline() 函数。

因此,要实现此功能,一个简单的方法是将函数重命名为 my_getline()

getline()getdelim() 最初都是 <代码>GNU 扩展。它们在 POSIX.1-2008 中标准化。

This is because the stdio.h have a getline() function.

So a simple thing to make this work would be to rename your function to my_getline()

Both getline() and getdelim() were originally GNU extensions. They were standardized in POSIX.1-2008.

柠檬 2025-01-01 14:13:18

/usr/include/stdio.h:671:20: 注意:之前的“getline”声明在这里

这应该会给你一个提示。尝试将代码中的 getline() 函数重命名为其他名称。

另外,以这种方式声明 main() 是旧风格。默认情况下,没有声明返回类型和参数的函数接受未指定数量的参数并返回 int。 main() 几乎就是这种情况:它确实返回一个 int,但有两个参数。你最好将其声明为:

int main(int argc, char **argv)

或:

int main(int argc, char *argv[])

/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 for main(): it does return an int, but has two arguments. You had better declare it as:

int main(int argc, char **argv)

or:

int main(int argc, char *argv[])
空城缀染半城烟沙 2025-01-01 14:13:18

getline 现在是在 stdio.h 中声明的 POSIX 函数

getline 函数重命名为另一个名称,它将进行编译。

getline is now a POSIX function declared in stdio.h

Rename you getline function to another name and it will compile.

橙幽之幻 2025-01-01 14:13:18

您必须更改 getline 的名称,因为它已经存在。

You have to change getline's name because it already exists.

紅太極 2025-01-01 14:13:18

“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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文