undefined reference to `getline' in c

后端 未结 3 1748
情书的邮戳
情书的邮戳 2020-11-28 14:34

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

#include 
#include <         


        
相关标签:
3条回答
  • 2020-11-28 15:06

    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.

    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.

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

    0 讨论(0)
  • 2020-11-28 15:07

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

    _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
    

    for glibc 2.10 or later,

    _GNU_SOURCE
    

    before that.

    0 讨论(0)
  • 2020-11-28 15:10

    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.

    0 讨论(0)
提交回复
热议问题