1

I'm currently having some problems with getline() in c. I know it's not a standard C function, however I am using the proper resources according to what I've seen online.

I have both:

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

However I am still having problems compiling, it gives me this output:

main.c: In function 'read-file':
main.c:46:17: warning: implicit declaration of function 'getline' [-Wimplicit-function-declaration]
C:\Users\Miguel\AppData\Local\Temp\cc6aYESe.o:main.c:(.text+0x85): undefined reference to `getline'
collect2.exe: error: ld returned 1 exit status

I can't seem to figure out what's wrong, I've seen several problems like this, however no solution seems to apply.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
ElMassas
  • 367
  • 3
  • 18
  • 1
    You're on a Windows machine; are you using MinGW or Cygwin or something else? If you're using MinGW, it may well not be defined — it is a Minimal GNU for Windows. You could try requesting POSIX support (`#define _XOPEN_SOURCE 700` for example), but if `_GNU_SOURCE` doesn't work, the chances are that POSIX won't either. I don't often suggest this, but try either disabling the warning (change the compiler options), or include a declaration (`ssize_t getline(char ** restrict linep, size_t * restrict linecapp, FILE * restrict stream);`) in the code, and see whether it links. It may not. – Jonathan Leffler Jun 10 '18 at 18:43
  • In fact, given that you get an 'undefined reference to `getline`' message, the function is not defined in your environment. You will need to provide your own implementation — it isn't hard to write. Or you can contact me at a pinch; see my profile. – Jonathan Leffler Jun 10 '18 at 19:36
  • 1
    Or see this https://stackoverflow.com/a/47229318/918959 – Antti Haapala -- Слава Україні Jun 10 '18 at 21:36

1 Answers1

0

try with the following preprocessor sentence: #define _POSIX_C_SOURCE 200809L

"It allows you to use functions that are not part of the standard C library but are part of the POSIX.1 (IEEE Standard 1003.1) standard. Using the macros described in feature_test_macros allows you to control the definitions exposed by the system header files."

ref: What does #define _POSIX_SOURCE mean?

Hernando
  • 1
  • 1