0

I am trying to work with getdelim() function which apperantly is the preferred method with getline() over fgets(). However, when I try to run the code below, I get undefined reference to getdelim() error.

I don't think it has anything to do with the code but rather the gcc version that I am using. So to the cmd I typed gcc -v and apperantly I have gcc version 8.1.0 (x86_64-win32-seh-rev0, Built by MinGW-W64 project)

I am not sure how old it is or if that's the problem. If so, what version of the gcc should I use and can I solve this with adding some fancy macros?

Code:

#include <stdio.h>

int main()
{
    int size = 10;
    char *string;

    printf ("Please enter a string: ");

    string = (char*)malloc(size);

    getdelim (&string, &size, '-', stdin);

    printf( "%s\n", string );

    return 0;
}
aulven
  • 521
  • 3
  • 14
  • Is this the only message you get from your compiler? What compiler options are you using? – KamilCuk Mar 23 '20 at 22:32
  • `getdelim()` is not a standard C function, it's a GNU extension. You have to `#define _GNU_SOURCE` *before* including `stdio.h`. If that doesn't work either, your library does not have the function. – Marco Bonelli Mar 23 '20 at 22:32
  • @MarcoBonelli Yeah it didn't work. Can I maually add the function or can I upgrade the library? – aulven Mar 23 '20 at 22:33
  • No you can't manually add it. Yon can try with another version or a different C library although that's a different problem. – Marco Bonelli Mar 23 '20 at 22:35
  • @KamilCuk make all 'Building target: project' 'Invoking: Cross GCC Linker' gcc -o "project" ./src/tryingGetDelim.o ./src/tryingGetDelim.o: In function `main': C:\Users\User\eclipse-workspace\project\Debug/../src/tryingGetDelim.c:13: undefined reference to `getdelim' collect2.exe: error: ld returned 1 exit status make: *** [makefile:32: project] Error 1 "make all" terminated with exit code 2. Build might be incomplete. 01:36:15 Build Failed. 2 errors, 0 warnings. (took 211ms) I am not sure what compiler options I use, I didn't modified them so probably eclipse default optns – aulven Mar 23 '20 at 22:38
  • Sorry, but mingw gcc just does not support GNU getdelim() functions. `what version of the gcc` it's not a version of gcc, rather version of the [C standard library](https://en.wikipedia.org/wiki/C_standard_library#Implementations) you use. mingw gcc uses msvcrt, which just does not implement `getdelim()`. You can install cygwin, which I think uses newlib, which implements [getdelim](https://github.com/bminor/newlib/blob/master/newlib/libc/stdio/getdelim.c) – KamilCuk Mar 23 '20 at 22:43
  • Does this answer your question? [Where/how to get the "getline" function if it is missing from stdio.h?](https://stackoverflow.com/questions/27381903/where-how-to-get-the-getline-function-if-it-is-missing-from-stdio-h) – KamilCuk Mar 23 '20 at 22:45
  • No, at that link they say add the macro that Marco Bonelli mentioned, which does not work. I really don't want to setup another compiler and link it :/ Can I upgrade C Standart Library? Sorry If I'm missing stuff here, but does what you say mean that I cannot use it with minGW? I didn't quite understand. – aulven Mar 23 '20 at 23:00

0 Answers0