14

I'm writing a program in C using Code::Blocks 13.12 on Windows 8 (the C compiler is mingw32-gcc). I would like to use the "getline" function but it seems to be missing from the stdio.h. Is there any way to get it except for writing own implementation?

Blackstar
  • 155
  • 1
  • 1
  • 4
  • This might help: http://stackoverflow.com/questions/13112784/undefined-reference-to-getline-in-c – Paul92 Dec 09 '14 at 14:59

2 Answers2

15

getline is a POSIX function, and Windows isn't POSIX, so it doesn't have some POSIX C functions available.

You'll need to roll your own. Or use one that has already been written.

Community
  • 1
  • 1
rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • 1
    Or use [`fgets`](http://www.cplusplus.com/reference/cstdio/fgets/) which is part of the C stdlib? – Mr. Llama Dec 09 '14 at 15:03
  • @Mr.Llama [If you're careful, sure](http://stackoverflow.com/a/16323215/256138)... – rubenvb Dec 09 '14 at 15:05
  • @rubenvb Looked over your [has already been written](http://stackoverflow.com/a/735472/256138). Not a bad implementation, but needs a few corner case improvements. – chux - Reinstate Monica Dec 09 '14 at 15:55
  • @chux it's not mine, but feel free to tell the author of that code (who seems to need a reason why no one writes their own code anymore). It'd be a great reality check for him/her. – rubenvb Dec 09 '14 at 15:58
  • @rubenvb The author says: "15 minutes, and I haven't written C in 10 years." In addition, the author acknowledged the edge cases, but couldn't be bothered to do yet more homework. Also the author was wondering if anyone "writes code anymore," which is not the same as "needing a reason..." It's a valid question, as most code these days is garbage and bloated. If people wrote their own code at this super basic level, the state of the web would be at least an order of magnitude better than it is now. And we wouldn't have to wade through snide remarks like the "great reality check" one. – SO_fix_the_vote_sorting_bug Dec 09 '20 at 16:33
5

getline is not a standard C library function. Some implementations (such as gcc) provide it as an extension.

If you're compiling with gcc, you'll need to define the feature macro _GNU_SOURCE in order to make it available for your code:

#define _GNU_SOURCE
#include <stdio.h>
...
getline (...);

EDIT

Hopefully since mingw is a GNU compiler, this should be available for you on windows.

John Bode
  • 119,563
  • 19
  • 122
  • 198