Issue
I have a program2.c c code. gcc program2.c gives the following error - alt="gcc error" />
I tried with various options like
- gcc -std=gnu90 program2.c
- gcc -std=c90 program2.c
- gcc -std=c99 program2.c
- gcc -std=c11 program2.c
- gcc -std=gnu11 program2.c
- g++ program2.c
None of the above compiled the file. Error was always similar. Is there any way to compile this ? Looks like getline is not part of c standard. But some compiler do support it.
Any suggestions?
Solution
Indeed, getline
was never part of the C standard library, but is a Unix/POSIX extension. To get access to it, you need to compile with gcc in POSIX ("gnu") mode under Unix, in which case the compiler drops the function inside stdio.h. And dropping non-standard functions inside standard headers is non-compliant behavior.
To get this working under Windows, you'd need to use Cygwin to emulate Unix under Windows. The gcc/mingw port won't do, since that one uses Microsoft's C standard lib.
The good news is that you probably don't want to use getline
anyway (even in POSIX), since that function has a horrible API and is a major source for memory leaks in buggy C programs. It's one of those strange, ancient Unix functions that simply should be avoided. Instead consider using fgets
with a caller-allocated buffer: faster, safer, portable standard C.
Answered By - Lundin Answer Checked By - David Goodson (WPSolving Volunteer)