在c中未定义的“getline”引用。

时间:2021-05-11 05:30:45

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!

但是,问题是编译器会不断返回错误:未定义的“getline”引用。你能告诉我是什么问题吗?谢谢你!

I am using Win7 64bit + Eclipse Indigo + MinGW

我使用的是Win7 64位+ Eclipse Indigo + MinGW。

4 个解决方案

#1


8  

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_SOURCE before #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扩展,您必须在#include 之前定义_gnu_source。要启用它,必须使用与gnu兼容的编译器,如gcc。

Additionally, nbytes should have type size_t, not int. On my system, at least, these are of different size, with size_t being 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应该具有类型size_t,而不是int.在我的系统中,至少,这些大小不同,size_t更长,使用int*而不是size_t*可能会有严重的后果(也不会用默认的gcc设置来编译)。有关详细信息,请参阅getline手册页(http://linux.die.net/man/3/getline)。

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

有了这些更改,您的程序将编译并运行在我的系统上。

#2


3  

getline isn'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.

在这之前。

#3


2  

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 header和getline()没有出现在任何C标题中,它只出现在c++头文件中。这意味着在MinGW中不存在C函数getline()。

#4


-1  

For me compilator says

对我来说compilator说

getline.cpp: In function ‘int main(int, char*)’: getline.cpp:15:52: error: cannot convert ‘int’ to ‘size_t* {aka long unsigned int*}’ for argument ‘2’ to ‘__ssize_t getline(char**, size_t*, FILE*)’

getline。在函数' int main(int, char*) ': getline。cpp:15:52:错误:不能将“int”转换为“size_t* {aka long unsigned int*}”,用于参数“2”到“__ssize_t getline(char**, size_t*, FILE*)”

Check your compiler, linker, your folder with library. Maybe reinstall?

检查你的编译器,链接器,你的文件夹和库。也许重新安装?


Write your own readline function. It's simple. Read char by char until you get new line

编写自己的readline函数。这很简单。通过char读取char,直到找到新的行。

#1


8  

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_SOURCE before #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扩展,您必须在#include 之前定义_gnu_source。要启用它,必须使用与gnu兼容的编译器,如gcc。

Additionally, nbytes should have type size_t, not int. On my system, at least, these are of different size, with size_t being 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应该具有类型size_t,而不是int.在我的系统中,至少,这些大小不同,size_t更长,使用int*而不是size_t*可能会有严重的后果(也不会用默认的gcc设置来编译)。有关详细信息,请参阅getline手册页(http://linux.die.net/man/3/getline)。

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

有了这些更改,您的程序将编译并运行在我的系统上。

#2


3  

getline isn'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.

在这之前。

#3


2  

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 header和getline()没有出现在任何C标题中,它只出现在c++头文件中。这意味着在MinGW中不存在C函数getline()。

#4


-1  

For me compilator says

对我来说compilator说

getline.cpp: In function ‘int main(int, char*)’: getline.cpp:15:52: error: cannot convert ‘int’ to ‘size_t* {aka long unsigned int*}’ for argument ‘2’ to ‘__ssize_t getline(char**, size_t*, FILE*)’

getline。在函数' int main(int, char*) ': getline。cpp:15:52:错误:不能将“int”转换为“size_t* {aka long unsigned int*}”,用于参数“2”到“__ssize_t getline(char**, size_t*, FILE*)”

Check your compiler, linker, your folder with library. Maybe reinstall?

检查你的编译器,链接器,你的文件夹和库。也许重新安装?


Write your own readline function. It's simple. Read char by char until you get new line

编写自己的readline函数。这很简单。通过char读取char,直到找到新的行。