How to fix C error 'errno undeclared'

Problem:

You have C code like

main.c
errno = EFAULT;

but when you try to compile it you see an error message like

error_message.txt
main.c: In function ‘main’:
main.c:4:5: error: ‘errno’ undeclared (first use in this function)
     errno = EFAULT;
     ^~~~~
main.c:4:5: note: each undeclared identifier is reported only once for each function it appears in
main.c:4:13: error: ‘EFAULT’ undeclared (first use in this function)
     errno = EFAULT;

Solution

Add

errno_include.c
#include <errno.h>

at the top of the source file where the error occured. This will include both the errno variable and specific error codes like EFAULT.


Check out similar posts by category: C/C++, GCC Errors