编译符合 Y2038 的旧 C 代码仍会产生 4 字节变量

发布于 2025-01-16 18:41:25 字数 745 浏览 6 评论 0原文

根据这个概述,为了编译符合Y2038的旧代码,我们只需要添加预处理器宏 __USE_TIME_BITS64gcc,但这似乎不适用于 Debian 12 的 ARMv7 主板(书虫):

#include <sys/types.h>
#include <sys/stat.h>

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    struct stat sb;

    printf("sizeof time_t: %zu\n", sizeof(time_t));
    printf("sizeof stat timestamp: %zu\n", sizeof(sb.st_atime));

    return 0;
}

time_t 仍然是 4 个字节:

root@debian:~# gcc -D__USE_TIME_BITS64 time.c -o time
root@debian:~# ./time
sizeof time_t: 4
sizeof stat timestamp: 4
root@debian:~#

glibc 是 2.33,我在这里做错了什么?

According to this overview in order to compile Y2038 conform old code, we just need to add the preprocessor macro __USE_TIME_BITS64 to gcc, but that does not seem to work on an ARMv7 board with Debian 12 (bookworm):

#include <sys/types.h>
#include <sys/stat.h>

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    struct stat sb;

    printf("sizeof time_t: %zu\n", sizeof(time_t));
    printf("sizeof stat timestamp: %zu\n", sizeof(sb.st_atime));

    return 0;
}

time_t is still 4 bytes:

root@debian:~# gcc -D__USE_TIME_BITS64 time.c -o time
root@debian:~# ./time
sizeof time_t: 4
sizeof stat timestamp: 4
root@debian:~#

glibc is 2.33, what am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月蹉跎了容颜 2025-01-23 18:41:25

根据这篇文章(现在有点旧了,其中的某些部分可能没有更相关):

...定义_TIME_BITS=64 将导致所有时间函数默认使用 64 位时间。 _TIME_BITS=64 选项是通过将标准函数和类型透明地映射到其内部 64 位变体来实现的。 Glibc 还会设置 __USE_TIME_BITS64,用户代码可以测试该值以确定 64 位变体是否可用。

据推测,这包括将 time_t 设为 64 位。

因此,如果您的 glibc 版本完全支持此功能,则看起来您设置了错误的宏。您需要:

-D_TIME_BITS=64

According to this post (which is getting a little old now, and some parts of which are probably no longer relevant):

... defining _TIME_BITS=64 would cause all time functions to use 64-bit times by default. The _TIME_BITS=64 option is implemented by transparently mapping the standard functions and types to their internal 64-bit variants. Glibc would also set __USE_TIME_BITS64, which user code can test for to determine if the 64-bit variants are available.

Presumably, this includes making time_t 64 bit.

So if your version of glibc supports this at all, it looks like you're setting the wrong macro. You want:

-D_TIME_BITS=64

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文