如何使用autoconf检查POSIX一致性 [英] How to check for POSIX-conformance with autoconf
问题描述
我的项目遵循 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标准,请使用
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 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.