Blieque Blieque - 27 days ago 17
C Question

Preventing "warning: implicit declaration of function 'setlinebuf'..."

Both Clang/LLVM and GCC show this warning. I am aware that headers must be included in order to use many functions, but

stdio.h
is included at the top of the file. I've used other functions (e.g.,
setbuf()
) from the same header, and these work fine. Removing the header include causes errors and warnings to be thrown for these other functions, as expected.

I used
clang -MD -MF
to show included header files and checked
/usr/include/stdio.h
, which contains a prototype for
setlinebuf()
. I found one reference online to
unix.h
, but this doesn't appear to exist on my system.

I'm using Lubuntu 16.10 x86_64, Clang 3.8.1.

View source: Include on line 1, function use on line 32.

Answer

Include this before including stdio.h:

#define _BSD_SOURCE

setlinebuf() is not a standard C function and thus it may not be included by default in stdio.h.

But note that _BSD_SOURCE has been deprecated since glibc 2.20 and been superseded by _DEFAULT_SOURCE.