gpt4 book ai didi

c - 错误 : conflicting types for ‘wstat’ in gcc

转载 作者:太空狗 更新时间:2023-10-29 15:28:10 24 4
gpt4 key购买 nike

我从文档中复制了这个程序: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();
}
}

当我运行 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) {

据我了解,wstat 被定义了两次。这是否意味着文档不正确?以及如何解决?

最佳答案

是的,那个密码刚刚坏了。我不知道 union wait wstat; 的意图是什么。也许是文档作者的一些复制粘贴错误。谁知道。总的来说,代码写得很糟糕,IMO。

无论如何,下面是代码实际要执行的操作:

int wstat;
pid_t pid;

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

关于c - 错误 : conflicting types for ‘wstat’ in gcc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56628418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com