Why does my compiler not accept fork(), despite my inclusion of ?

前端 未结 4 1378
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 16:48

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

#include   
#include 
#include 
#include 
#inclu         


        
4条回答
  •  庸人自扰
    2020-12-01 17:02

    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 
    
    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.

提交回复
热议问题