gpt4 book ai didi

c - libc 包含不尊重源类型参数更改的守卫

转载 作者:行者123 更新时间:2023-12-05 00:45:09 27 4
gpt4 key购买 nike



如果我包括 time.h ,更改“源类型参数”并重新包含该 header ,难道不应该添加这些新定义吗?我知道这是由于包括 guard 而发生的。我的问题是:这是 libc 中的错误吗?难道它不能处理这个吗?

#include <time.h>
#define _XOPEN_SOURCE 600
#include <time.h>

static struct timespec t;

错误信息:
example.c:5:24: error: storage size of ‘t’ isn’t known
5 | static struct timespec t;
| ^

背景

我在使用 -std=c99 编译时发现了构建 Python 扩展的这种行为.如果我在包含之前包含标准库 Python.h由于缺少 POSIX 功能的定义,我会得到编译错误。如果我移动了 Python.h包括在其他一切之前,一切都很好。当然用 -std=gnu99 编译也有效。但我想找出错误发生的原因并将其提炼为上述代码示例。

这就引出了另一个问题。如果上述行为不是错误,则设置 _XOPEN_SOURCE标题中的类似源类型参数被认为是不好的做法? Python 是否应该在其头文件中删除该参数的设置,而是要求用户在编译期间定义它,或者使用 std=gnu99 ?

最佳答案

您所说的“源类型参数”称为功能测试宏,并且特别需要在包含 之前定义它们。任何 如果完全定义了标准头。

这是在 XSH 2.2.1 POSIX.1 Symbols 中指定的:

A POSIX-conforming application shall ensure that the feature test macro _POSIX_C_SOURCE is defined before inclusion of any header.

...

An XSI-conforming application shall ensure that the feature test macro _XOPEN_SOURCE is defined with the value 700 before inclusion of any header.



Linux man page for feature-test-macros 还明确说明了标准及其扩展的这一要求:

NOTE: In order to be effective, a feature test macro must be defined before including any header files



您不能重新定义和重新包含标题来更改内容,实际上在标题之间定义或取消定义/重新定义它们(即使是不同的、看似无关的标题)可能会完全破坏事情。

关于c - libc 包含不尊重源类型参数更改的守卫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58505893/

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