为什么我的编译器不接受 fork(),尽管我包含了

2024-04-19

这是我的代码(只是为了测试 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;
}

我收到以下警告:

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

这有什么问题吗?


unistd.h and fork是的一部分POSIX标准 http://pubs.opengroup.org/onlinepubs/9699919799/。它们在 Windows 上不可用(text.exe在你的 gcc 命令中提示你不在 *nix 上)。

看起来您正在使用 gcc 作为MinGW https://en.wikipedia.org/wiki/MinGW,这确实提供了unistd.h标头但没有实现类似的功能fork. Cygwin https://en.wikipedia.org/wiki/Cygwin does提供类似功能的实现fork.

但是,由于这是家庭作业,您应该已经了解如何获得工作环境的说明。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的编译器不接受 fork(),尽管我包含了 ? 的相关文章

随机推荐