How to fix C warning: implicit declaration of function '_exit'

Problem:

You have C code like

example.txt
_exit(1);
```c {filename="main.c"}

but when you try to compile it you see a warning message like

```plaintext {filename="implicit_declaration_exit_output.txt"}
main.c: In function ‘main’:
main.c:3:5: warning: implicit declaration of function ‘_exit’ [-Wimplicit-function-declaration]
     _exit(1);
     ^~~~~
main.c:3:5: warning: incompatible implicit declaration of built-in function ‘_exit’

Solution

Add

main_fixed_unistd.c
#include <unistd.h>

at the top of the source file where the error occured.

Note that in case you use _Exit(...) (capital E) instead of _exit(...), you need to add

main_fixed_stdlib.c
#include <stdlib.h>

instead.

Read the manpage for _exit in case you need further information on _exit(...).


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