gpt4 book ai didi

c - 如何使用 sigaction()?结构 sigaction 未定义

转载 作者:行者123 更新时间:2023-11-30 16:21:49 24 4
gpt4 key购买 nike

我正在做简单的 sigaction 示例来练习 C,但是当我尝试编译代码时,它声称 struct sigaction 不存在 [1]。

当我检查我生成的一些旧代码时,我发现我在文件的最顶部添加了一些 POSIX 字符串 [2]。但是当我阅读 sigaction 的手册(man 2 sigaction)时,里面没有关于 _POSIX_SOURCE 的内容,最接近的是 _POSIX_C_SOURCE ,它不起作用。我如何以及何时知道使用哪种 POSIX?当我尝试其他人建议的简单代码时,如果没有 _POSIX_SOURCE,它就不起作用。

[1]

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

void sa_handler(int signum)
{
printf("The signal has been replaced with this useless
string!\n");
exit(0);
}

int main(void)
{
struct sigaction sa = {.sa_handler = sa_handler};
int sigret = sigaction(SIGINT, &sa, NULL);
while(1);

return 0;
}

[2]

#define _POSIX_SOURCE
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

void sa_handler(int signum)
{
printf("The signal has been replaced with this useless
string!\n");
exit(0);
}

int main(void)
{
struct sigaction sa = {.sa_handler = sa_handler};
int sigret = sigaction(SIGINT, &sa, NULL);
while(1);

return 0;
}

当我编译第一个示例时,结果是这些错误消息。

sigaction.c: In function ‘main’:
sigaction.c:13:12: error: variable ‘sa’ has initializer but
incomplete type
struct sigaction sa = {.sa_handler = sa_handler};
^~~~~~~~~
sigaction.c:13:29: error: ‘struct sigaction’ has no member named
‘sa_handler’
struct sigaction sa = {.sa_handler = sa_handler};
^~~~~~~~~~
sigaction.c:13:42: warning: excess elements in struct initializer
struct sigaction sa = {.sa_handler = sa_handler};
^~~~~~~~~~
sigaction.c:13:42: note: (near initialization for ‘sa’)
sigaction.c:13:22: error: storage size of ‘sa’ isn’t known
struct sigaction sa = {.sa_handler = sa_handler};
^~
sigaction.c:14:18: warning: implicit declaration of function
‘sigaction’ [-Wimplicit-function-declaration]
int sigret = sigaction(SIGINT, &sa, NULL);
^~~~~~~~~

最佳答案

when I read the manual for sigaction (man 2 sigaction) there is nothing about _POSIX_SOURCE in it

来自man sigaction :L

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

来自future_test_macros(7) :

_POSIX_SOURCE
Defining this obsolete macro with any value is equivalent to
defining _POSIX_C_SOURCE with the value 1.

Since this macro is obsolete, its usage is generally not doc‐
umented when discussing feature test macro requirements in
the man pages.

因此 _POSIX_SOURCE 相当于 _POSIX_C_SOURCE 1 并且已过时。

How and when do I know which POSIX is the be used?

来自man future_test_macros :

Specification of feature test macro requirements in manual pages
When a function requires that a feature test macro is defined, the
manual page SYNOPSIS typically includes a note [....]

因此,您应该检查您感兴趣的功能/特性的手册页中的概要部分。例如 man sigaction :

sigaction(): _POSIX_C_SOURCE
siginfo_t: _POSIX_C_SOURCE >= 199309L

因此,您需要为sigaction()定义_POSIX_C_SOURCE,并且_POSIX_C_SOURCE大于或等于199309的值> 对于siginfo_t

关于c - 如何使用 sigaction()?结构 sigaction 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54718687/

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