Search code examples
cwindowsmingwfork

Why does my compiler not accept fork(), despite my inclusion of <unistd.h>?


Here's my code (created just to test fork()):

#include <stdio.h>  
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h> 

int main()
{   
    int pid;     
    pid=fork();

    if (pid==0) {
        printf("I am the child\n");
        printf("my pid=%d\n", getpid());
    }

    return 0;
}

I get following warnings:

warning: implicit declaration of function 'fork'
undefined reference to 'fork'

What is wrong with it?


Solution

  • unistd.h and fork are part of the POSIX standard. They aren't available on windows (text.exe in your gcc command hints that's you're not on *nix).

    It looks like you're using gcc as part of MinGW, which does provide the unistd.h header but does not implement functions like fork. Cygwin does provide implementations of functions like fork.

    However, since this is homework you should already have instructions on how to obtain a working environment.