我如何使用 sigaction()?未定义结构 sigaction
How do I use sigaction()? struct sigaction is not defined
我正在做简单的 sigaction 示例来练习 C,但是当我尝试编译我的代码时,它声称 struct sigaction 没有不存在 [1].
当我检查我生成的一些旧代码时,我发现我在文件 [2] 的最顶部添加了一些 POSIX 字符串。但是当我阅读 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);
^~~~~~~~~
您需要为 _POSIX_C_SOURCE
定义一个正整数。对于 sigaction
,至少需要:
#define _POSIX_C_SOURCE 199309L
查看 documentation 要使用哪个 POSIX 版本。
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)):
_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?
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 [....]
所以您应该查看您感兴趣的 function/feature 手册页中的 SYNOPSIS 部分。例如 man sigaction:
sigaction(): _POSIX_C_SOURCE
siginfo_t: _POSIX_C_SOURCE >= 199309L
所以你需要为sigaction()
定义_POSIX_C_SOURCE
并且_POSIX_C_SOURCE
大于等于199309
的值对于siginfo_t
.
我正在做简单的 sigaction 示例来练习 C,但是当我尝试编译我的代码时,它声称 struct sigaction 没有不存在 [1].
当我检查我生成的一些旧代码时,我发现我在文件 [2] 的最顶部添加了一些 POSIX 字符串。但是当我阅读 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);
^~~~~~~~~
您需要为 _POSIX_C_SOURCE
定义一个正整数。对于 sigaction
,至少需要:
#define _POSIX_C_SOURCE 199309L
查看 documentation 要使用哪个 POSIX 版本。
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)):
_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?
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 [....]
所以您应该查看您感兴趣的 function/feature 手册页中的 SYNOPSIS 部分。例如 man sigaction:
sigaction(): _POSIX_C_SOURCE
siginfo_t: _POSIX_C_SOURCE >= 199309L
所以你需要为sigaction()
定义_POSIX_C_SOURCE
并且_POSIX_C_SOURCE
大于等于199309
的值对于siginfo_t
.