Rob*_*nes 10 ubuntu posix glibc g++
如何获取POSIX strerror_r而不是GNU版本?
我正在使用glibc 2.7版(基于其中的内容)在Ubuntu 8.04上使用g ++进行编译.
编辑
在上面的手册页中,它说:
glibc的功能测试宏要求(参见feature_test_macros(7)):
The XSI-compliant version of strerror_r() is provided if:
(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
Otherwise, the GNU-specific version is provided.
Run Code Online (Sandbox Code Playgroud)
If no feature test macros are explicitly defined, then the following feature
test macros are defined by default: _BSD_SOURCE, _SVID_SOURCE, _POSIX_SOURCE,
and _POSIX_C_SOURCE=200809L (200112L in glibc versions before 2.10; 199506L in
glibc versions before 2.4; 199309L in glibc versions before 2.1).
Run Code Online (Sandbox Code Playgroud)
所以我应该得到POSIX版本,但我得到的是GNU版本.
从标题string.h:
/* Reentrant version of `strerror'.
There are 2 flavors of `strerror_r', GNU which returns the string
and may or may not use the supplied temporary buffer and POSIX one
which fills the string into the buffer.
To use the POSIX version, -D_XOPEN_SOURCE=600 or -D_POSIX_C_SOURCE=200112L
without -D_GNU_SOURCE is needed, otherwise the GNU version is
preferred. */
Run Code Online (Sandbox Code Playgroud)
注意,使用GNU扩展的时候要小心,打开它们(_GNU_SOURCE)最后,包括你想影响(或战略上取消定义它)头前.但是,如果不使用GNU扩展,则无需担心.
一般情况下,如果从GNU POSIX的默认行为偏差时,你会看到在头一些意见,表示如何可以得到POSIX的行为.它也(通常)记录在glibc手册中,但并不总是适用于高度浓缩的手册页.
编辑
试试这个简单的测试:
#include <string.h>
#ifdef _GNU_SOURCE
#error "Something turned it on!"
#endif
Run Code Online (Sandbox Code Playgroud)
或者更直接
#ifdef _GNU_SOURCE
#undef _GNU_SOURCE
#endif
#include <string.h>
Run Code Online (Sandbox Code Playgroud)
如果_POSIX_C_SOURCE={version}已定义,则应具有POSIX版本,除非其他原因导致GNU版本受到青睐.
我唯一能想到的就是这样做_GNU_SOURCE.我确定这不是你的命令行标志,你会看到它.可能是包含的另一个库已将其打开.
这就是我要求POSIX实现受到青睐的扩展"棘手"的意思,即使你不是那个打开它们的人.
编辑
如果有什么东西在开启_GNU_SOURCE(我不记得是否有提升,我不会像我做的那样使用c ++),你可能想让它这样做.您可以--undef "[macro]" -U[macro]从命令行使用.但是,如果库代码如下所示,那将无效:
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <string.h>
#ifdef _GNU_SOURCE
#error "It didn't work"
#endif
int main(void)
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是,当您的代码实际包含时string.h,其他内容已经启用了扩展并包含它.包括警卫自然会阻止你包括它两次.
尽量明确地关闭_GNU_SOURCE,并包括string.h之前的任何东西.这可以防止其他库打开这些扩展.但是,没有它们,这些库可能无法运行.有些代码只是'期望'GNU行为,并且不包括对POSIX的回退.
我对图书馆代码感到类似的挫败感,但是没有asprintf().