c++ - 无法使用 gcc 构建 sigqueue 示例,但 g++ 可以吗?

标签 c++ c linux signals sigqueue

我有一个奇怪的构建问题。

我有一个简单的测试程序,可以将 sigqueue 发送到另一个进程。

这个小代码示例在我将其构建为 c++ 程序(使用 g++ 编译)时构建并运行 但是当我将它编译为 c 程序(使用 gcc)时,我得到一个错误,他找不到 sigval 结构。

简短的例子:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char *argv[])
{
        sigval value;
        value.sival_int = 123;
        sigqueue(0,SIGUSR1, value);
}

请注意,我将 pid 替换为 0 以简化此问题。

如果我用 gcc 编译我得到这个:

$> gcc sigusr1_mini.c 
sigusr1_mini.c: In function ‘main’:
sigusr1_mini.c:9: error: ‘sigval’ undeclared (first use in this function)
sigusr1_mini.c:9: error: (Each undeclared identifier is reported only once
sigusr1_mini.c:9: error: for each function it appears in.)
sigusr1_mini.c:9: error: expected ‘;’ before ‘value’
sigusr1_mini.c:10: error: ‘value’ undeclared (first use in this function)

我在这里错过了什么,为什么他找不到 sigval 结构? 为什么 g++ 可以找到它?

谢谢 约翰

最佳答案

在 C 中,structunion 标签不会像在 C++ 中那样引入可以单独使用的名称。你必须拼写出来:

union sigval value;

关于c++ - 无法使用 gcc 构建 sigqueue 示例,但 g++ 可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3811604/

相关文章:

linux - 如何找出套接字指向什么?

linux - debian 错误 : Dependency is not satisfiable: libqt4-network (>= 4:4. 8.0)

c++ - Ubuntu 上 Jupyter 的 Cling 内核

c++ - 我如何将这些元素表达为 C++

c++ - 从文件中读取 double

c++ - 如何检测终端中的unicode字符串宽度?

linux - 有没有办法找到shell中最后执行的命令的运行时间?

c - 用于并行的 OpenMP get_num_threads

c - 为什么 GCC 不对无法访问的代码发出警告?

c - Tornado shell 命令在 vxworks 5.5 中查找当前系统日期