I have a simple C program that makes a call to the wait() function. However, my program doesn't include sys/wait.h. It still compiles and works fine. It's job is essentially to rewrite the process image with the command line arguments.
During compilation, I get this warning:
my_program.c: in function 'main' warning: implicit declaration of function 'wait', did you mean 'main'?
My program looks like this... It does exactly what I want it to do. I just don't know why wait() is still being defined when I don't include it.
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(int argc, char **argv)
{
pid_t my_pid;
my_pid = fork();
if (my_pid < 0)
{
printf("The fork didn't work! Terminate\n");
exit(0);
}
if (my_pid != 0)
{
// Parent block
printf("The parent will now wait\n");
wait(NULL);
printf("Done!\n");
}
else
{
// Child block
int resp;
resp = execvp(*(argv+1), argv+1);
printf("There was an error. Response code: %d\n Errno: %s\n", resp, stderror(errno));
}
}
Yet when I add #include <sys/wait.h>, the program compiles without a warning. Why is it able to compile in the first place, without sys/wait.h imported?