【问题标题】:why there is an error when executing the fork() system call?为什么执行 fork() 系统调用时会出错?
【发布时间】:2017-07-01 16:08:03
【问题描述】:

尝试执行 fork() 调用时出错。 警告:函数“fork”的隐式声明 [-Wimplicit-function-declaration fork(); 这是我的代码

#include <stdio.h>
int main()
{
    int a, b;
    b = fork();
    printf("hello");
    if (b == 0)
    {
        printf("child");
    }
    else
    {
        printf("parent");
    }
}

【问题讨论】:

  • 请修正您的格式并尝试确定您使用的是 C 还是 C++。
  • 因为你不包含声明的header fork,也就是unistd.h
  • fork is 隐式声明,因为你需要做#include &lt;unistd.h&gt;
  • 这个错误应该是在编译代码时出现的,而不是在执行时出现的。

标签: c++ c


【解决方案1】:

试试

#include <stdio.h>
#include <unistd.h>
int main()
{
    int a;
    pid_t b;
    b = fork();
    printf("hello");
    if (b == 0)
    {
        printf("child");
    }
    else
    {
        printf("parent");
    }
}

【讨论】:

    【解决方案2】:
      ## try this code##
      #include <stdio.h>
      #include <unistd.h>
      int main()
    {
        int a;
        pid_t b;
        b = fork();
       printf("HELLO");
       if (b == 0)
       {
          printf("child");
       }
       else
       {
        printf("parent");
       }
    

    }

    【讨论】:

      【解决方案3】:

      通常,只有当您尝试使用的方法或函数未在任何已包含的标头中定义时,才会引发错误 -Wimplicit-function-declaration

      例如:尝试使用 printf 而不包括 stdio.h

      fork 函数包含在 unistd.h 中。

      【讨论】:

        猜你喜欢
        • 2012-02-10
        • 2013-10-19
        • 2016-04-30
        • 2017-03-14
        • 1970-01-01
        • 2015-12-16
        • 1970-01-01
        • 1970-01-01
        • 2016-10-03
        相关资源
        最近更新 更多