问题描述
我的项目遵循 POSIX.1-2008 标准,并且我想确保用户的实现符合该标准.
My project follows the POSIX.1-2008 standard and I would like to ensure that the user's implementation conforms to this standard.
到目前为止,我一直在使用 AC_DEFINE
定义 _POSIX_C_SOURCE
宏.
So far I've been using AC_DEFINE
to define the _POSIX_C_SOURCE
macro as specified in the POSIX standard.
AC_DEFINE([_POSIX_C_SOURCE], [200809L], [Define the POSIX version])
但是,由于这是一个简单的C预处理器 #define
宏,因此它无济于事,无法阻止不符合POSIX的实现编译.
However, since this is a simple C preprocessor #define
macro , it does nothing to prevent an implementation that isn't POSIX-compliant from compiling.
Autoconf 是否提供标准宏来检查实现是否符合特定POSIX标准?
Does Autoconf offer a standard macro to check for the implementation's conformance to a specific POSIX standard?
推荐答案
要可移植地检查用户的实现是否符合特定的POSIX标准,请使用 AC_EGREP_CPP
来检查 _POSIX_VERSION :
To portably check if the user's implementation conforms to a specific POSIX standard, use AC_EGREP_CPP
to check the existence and value of _POSIX_VERSION
:
AC_EGREP_CPP(posix_200809L_supported,
[#define _POSIX_C_SOURCE 200809L
#include <unistd.h>
#ifdef _POSIX_VERSION
#if _POSIX_VERSION == 200809L
posix_200809L_supported
#endif
#endif
],
[],
[AC_MSG_FAILURE([*** Implementation must conform to the POSIX.1-2008 standard.])]
)
这是由于POSIX做出的一些保证.
This works due to several guarantees made by POSIX.
我们首先将 _POSIX_C_SOURCE
设置为 200809L
.
#define _POSIX_C_SOURCE 200809L
POSIX.1-2008 指出,当应用程序包含 POSIX.1-2008 描述的标头时,以及此功能测试宏时( _POSIX_C_SOURCE
)定义为具有值 200809L
,然后 POSIX.1-2008要求在包含标头时显示的所有符号都应可见
.
POSIX.1-2008 states that when an application includes a header described by POSIX.1-2008, and when this feature test macro (_POSIX_C_SOURCE
) is defined to have the value 200809L
, then all symbols required by POSIX.1-2008 to appear when the header is included shall be made visible
.
因此,当我们在下一行中包含 unistd.h
时, POSIX.1-2008 所需的所有符号将出现在 unistd.h 将可见.
So when we include unistd.h
in the next line, all symbols required by POSIX.1-2008 to appear in unistd.h
will be made visible.
#include <unistd.h>
自 _POSIX_VERSION
必须显示在 unistd.h
中,现在也可以看到.
Since _POSIX_VERSION
is required to appear in unistd.h
, it too is now visible.
_POSIX_VERSION
为设置为 200809L
的值.
因此,我们现在要做的就是测试是否定义了 _POSIX_VERSION
,如果已定义,则将其设置为等于 200809L
的值.
So all we have to do now is test if _POSIX_VERSION
is defined, and if so, whether it's set to a value equal to 200809L
.
#ifdef _POSIX_VERSION
#if _POSIX_VERSION == 200809L
检查是否定义了 _POSIX_VERSION
有助于我们确定实施中是否支持任何 POSIX 标准.检查 _POSIX_VERSION
的值有助于我们缩小是否支持 POSIX 标准的特定版本.
Checking whether _POSIX_VERSION
is defined helps us determine if there is support for any POSIX standard on the implementation. Checking the value of _POSIX_VERSION
helps us narrow down whether a specific version of the POSIX standard is supported.
如果这两个条件均成立,则该实现几乎可以肯定支持 POSIX.1-2008 .
If both of these conditions are true, then the implementation almost certainly supports POSIX.1-2008.
这篇关于如何使用autoconf检查POSIX一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!