27

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?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Suspended
  • 1,184
  • 2
  • 12
  • 28
  • 1
    Do you have your C library's headers installed? How are you invoking your compiler? – Shea Levy Mar 08 '12 at 02:59
  • 4
    gcc test.c -pedantic -Wall -o test.exe – Suspended Mar 08 '12 at 03:01
  • are you sure fork() is part of stdio.h? it isn't according to http://www.cplusplus.com/reference/clibrary/cstdio/. maybe there is a conflict with another header file you are including? – moesef Mar 08 '12 at 03:02
  • 1
    Apparently it's part of How can it be conflicting with what I included? – Suspended Mar 08 '12 at 03:09
  • @user1166935: There are no conflicts. The problem is your compiler options. Try without `-pedantic`, it should work then. – Niklas B. Mar 08 '12 at 03:11
  • @moesef: The word "fork" does not appear on the page you linked to. (Also, it's not in the C standard, so I don't see why it'd be in ) – Billy ONeal Mar 08 '12 at 03:18
  • @NiklasB.: `undefined reference` means "link error" -- `-pedantic` isn't going to make any difference. – Billy ONeal Mar 08 '12 at 03:19
  • @Billy: `implicit declaration of function 'fork'` means that the compiler doesn't know about the function as well. – Niklas B. Mar 08 '12 at 03:26
  • 1
    @NiklasB.: Yes, that's true. But unrelated to the point that `-pedantic` isn't making any difference here. (And this warning is turned on when `-pedantic` is not anyway... all functions *should* be prototyped, even in C89 (and such is required in C99, C++)) – Billy ONeal Mar 08 '12 at 03:28
  • 2
    @user1166935: What operating system are you using? Is it possible you have a bad `unistd.h` file for some reason? – Keith Thompson Mar 08 '12 at 03:34
  • Also it is better to use pid_t rather than int for the variable pid. pid_t is a typedef provided for storing pid. – Abhay Patil Oct 07 '20 at 19:09

4 Answers4

47

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.

strcat
  • 5,376
  • 1
  • 27
  • 31
  • 3
    The OP has `` available; otherwise gcc would have reported a fatal error for the missing header file. – Keith Thompson Mar 08 '12 at 03:33
  • 3
    MinGW does not implement fork(), you could perhaps try Cygwin (or a real POSIX system) if you really need fork(). Or, you could try a similar windows function. – Sam Watkins Mar 08 '12 at 03:38
  • 5
    Although Cygwin does implement `fork()`, [it ain't pretty](http://cygwin.com/cygwin-ug-net/highlights.html#ov-hi-process). – Adam Rosenfield Mar 08 '12 at 04:14
  • see also http://code.google.com/p/chromium/wiki/CygwinDllRemappingFailure re cygwin `fork()` – Christoph Mar 08 '12 at 09:11
  • 1
    "should already have instructions on how to obtain a working environment" -if only Universities were that good. – Ryan Oct 08 '19 at 04:30
8

You have got #include <unistd.h> which is where fork() is declared.

So, you probably need to tell the system to show the POSIX definitions before you include the system headers:

#define _XOPEN_SOURCE 600

You can use 700 if you think your system is mostly POSIX 2008 compliant, or even 500 for an older system. Because fork() has been around forever, it will show up with any of those.

If you are compiling with -std=c99 --pedantic, then all the declarations for POSIX will be hidden unless you explicitly request them as shown.

You can also play with _POSIX_C_SOURCE, but using _XOPEN_SOURCE implies the correct corresponding _POSIX_C_SOURCE (and _POSIX_SOURCE, and so on).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    with gcc and glibc, the feature test macro [isn't actually required](http://linux.die.net/man/2/fork) for `fork`. Including [unistd.h](http://linux.die.net/include/unistd.h) is enough. The standard does state that applications should define `_POSIX_C_SOURCE` before including headers though. – strcat Mar 08 '12 at 03:21
6

As you've already noted, fork() should be defined in unistd.h - at least according to the man pages that come with Ubuntu 11.10. The minimal:

#include <unistd.h>

int main( int argc, char* argv[])
{
    pid_t procID;

    procID = fork();
    return procID;
}

...builds with no warnings on 11.10.

Speaking of which, what UNIX/Linux distribution are you using? For instance, I've found several non-remarkable functions that should be defined in Ubuntu 11.10's headers aren't. Such as:

// string.h
char* strtok_r( char* str, const char* delim, char** saveptr);
char* strdup( const char* const qString);

// stdio.h
int fileno( FILE* stream);

// time.h
int nanosleep( const struct timespec* req, struct timespec* rem);

// unistd.h
int getopt( int argc, char* const argv[], const char* optstring);
extern int opterr;
int usleep( unsigned int usec);

As long as they're defined in your C library it won't be a huge problem. Just define your own prototypes in a compatibility header and report the standard header problems to whoever maintains your OS distribution.

Craig Mc
  • 179
  • 1
  • 2
  • 3
    `fork` is available on far more than "GNU/Linux" systems -- it's available anywhere POSIX is supported, which includes GNU/Hurd, Unix, BSD, etc. (And for the record, I believe the OP is on Windows given his comments) – Billy ONeal Mar 08 '12 at 03:21
  • 1
    (Oh, forgot Solaris and MacOSX, it's available there too) – Billy ONeal Mar 08 '12 at 03:29
  • 1
    Actually, I said "UNIX/Linux", but yes, fork() is a POSIX feature. Just because your host OS provides unistd.h, doesn't mean it supports POSIX features (which actually is the main point of unistd.h [link](http://en.wikipedia.org/wiki/Unistd.h)). That link provides a clue. Maybe the OP needs to install Cygwin to provide some POSIX/Windows bridge. Or just use a different host OS. – Craig Mc Mar 08 '12 at 05:10
0

I think that you have to do the following instead:

pid_t pid = fork();

To learn more about Linux API, go to this online manual page, or even go into your terminal right now and type,

man fork

Good luck!

2fewChars
  • 1
  • 1