简体   繁体   中英

getline function not compiling with mingw-w64 gcc

I have a program2.c c code. gcc program2.c gives the following error - gcc 错误

I tried with various options like

  1. gcc -std=gnu90 program2.c
  2. gcc -std=c90 program2.c
  3. gcc -std=c99 program2.c
  4. gcc -std=c11 program2.c
  5. gcc -std=gnu11 program2.c
  6. 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM