简体   繁体   English

错误:gcc中'wstat'的冲突类型

[英]error: conflicting types for ‘wstat’ in gcc

I copied this program from this documentation: https://docs.oracle.com/cd/E19455-01/806-4750/signals-7/index.html 我从这个文档中复制了这个程序: https//docs.oracle.com/cd/E19455-01/806-4750/signals-7/index.html

#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/resource.h>

void proc_exit()
{
        int wstat;
        union wait wstat;
        pid_t   pid;

        while (TRUE) {
            pid = wait3 (&wstat, WNOHANG, (struct rusage *)NULL );
            if (pid == 0)
                return;
            else if (pid == -1)
                return;
            else
                printf ("Return code: %d\n", wstat.w_retcode);
        }
}
main ()
{
        signal (SIGCHLD, proc_exit);
        switch (fork()) {
            case -1:
                perror ("main: fork");
                exit (0);
            case 0:
                printf ("I'm alive (temporarily)\n");
                exit (rand());
            default:
                pause();
        }
}

When I run gcc main.c I get this error: 当我运行gcc main.c此错误:

main.c: In function ‘proc_exit’:
main.c:9:13: error: conflicting types for ‘wstat’
  union wait wstat;
             ^~~~~
main.c:8:6: note: previous declaration of ‘wstat’ was here
  int wstat;
      ^~~~~
main.c:9:13: error: storage size of ‘wstat’ isn’t known
  union wait wstat;
             ^~~~~
main.c:12:9: error: ‘TRUE’ undeclared (first use in this function)
  while (TRUE) {

As I understand wstat is defined twice. 据我所知, wstat定义了两次。 Does it mean that documentation is incorrect? 这是否意味着文档不正确? And how it can be fixed? 它是如何修复的?

Yeah, that code is just broken. 是的,那个代码刚刚破解。 I don't know what the intention was with the union wait wstat; 我不知道union wait wstat;的意图是什么union wait wstat; . Maybe some copy paste error from the documentation author. 可能是文档作者的一些复制粘贴错误。 Who knows. 谁知道。 The code in general is written quite poorly, IMO. 一般来说,代码编写得非常糟糕,IMO。

Anyway, here's what the code is actually trying to do: 无论如何,这是代码实际上要做的事情:

int wstat;
pid_t pid;

while (1) {
    pid = wait3(&wstat, WNOHANG, NULL);
    if (pid == 0 || pid == -1)
        return;
    printf("Return code: %d\n", wstat);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM