C语言 c 中对“getline”的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13112784/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:13:13  来源:igfitidea点击:

undefined reference to `getline' in c

creferenceundefinedgetlinemingw32

提问by Eric Cartman

I am learning to use getline in C programming and tried the codes from http://crasseux.com/books/ctutorial/getline.html

我正在学习在 C 编程中使用 getline 并尝试了来自http://crasseux.com/books/ctutorial/getline.html的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int atgc, char *argv[])
{
    int bytes_read = 1;
    int nbytes = 10;
    char *my_string;

    my_string = (char *)malloc(nbytes+1);

    puts("Please enter a line of text");

    bytes_read = getline(&my_string, &nbytes, stdin);

    if (bytes_read == -1)
    {
        puts ("ERROR!");
    }
    else
    {
        puts ("You typed:");
        puts (my_string);
    }

    return 0;
 }

However, the problem is that the compiler keeps returning errors of this: undefined reference to 'getline'. Could you please tell me what the problem is? Thank you!

但是,问题是编译器不断返回以下错误:undefined reference to 'getline'。你能告诉我是什么问题吗?谢谢!

I am using Win7 64bit + Eclipse Indigo + MinGW

我使用的是 Win7 64bit + Eclipse Indigo + MinGW

回答by amaurea

The other answers have covered most of this, but there are several problems. First, getline()is not in the C standard library, but is a POSIX 2008 extension. Normally, it will be available with a POSIX-compatible compiler, as the macros _POSIX_C_SOURCE will be defined with the appropriate values. You possibly have an older compiler from before getline()was standardized, in which case this is a GNU extension, and you must #define _GNU_SOURCEbefore #include <stdio.h>to enable it, and must be using a GNU-compatible compiler, such as gcc.

其他答案已经涵盖了大部分内容,但存在几个问题。首先,getline()不在 C 标准库中,而是 POSIX 2008 扩展。通常,它可用于兼容 POSIX 的编译器,因为宏 _POSIX_C_SOURCE 将使用适当的值进行定义。您可能有一个以前getline()标准化的旧编译器,在这种情况下,这是一个 GNU 扩展,您必须#define _GNU_SOURCE#include <stdio.h>启用它,并且必须使用与 GNU 兼容的编译器,例如 gcc。

Additionally, nbytesshould have type size_t, not int. On my system, at least, these are of different size, with size_tbeing longer, and using an int*instead of a size_t*can have grave consequences (and also doesn't compile with default gcc settings). See the getline manual page (http://linux.die.net/man/3/getline) for details.

此外,nbytes应该有 type size_t,而不是int. 至少在我的系统上,它们具有不同的大小,size_t更长,并且使用 aint*代替 asize_t*可能会产生严重后果(并且也不能使用默认的 gcc 设置进行编译)。有关详细信息,请参阅 getline 手册页 (http://linux.die.net/man/3/getline)。

With that change made, your program compiles and runs fine on my system.

做出更改后,您的程序可以编译并在我的系统上正常运行。

回答by jdarthenay

I am also using MinGW. I checked MinGW headers and getline()does not appear in any C header, it appears only in C++ headers. This means the C function getline()does not exist in MinGW.

我也在使用 MinGW。我检查了 MinGW 头文件并getline()没有出现在任何 C 头文件中,它只出现在 C++ 头文件中。这意味着getline()MinGW 中不存在C 函数。

回答by Daniel Fischer

getlineisn't a standard function, you need to set a feature test macro to use it, according to my man page,

getline不是标准功能,根据我的手册页,您需要设置一个功能测试宏才能使用它,

_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700

for glibc 2.10 or later,

对于 glibc 2.10 或更高版本,

_GNU_SOURCE

before that.

在那之前。