编译符合 Y2038 的旧 C 代码仍然会导致 4 字节变量

Compiling old C code Y2038 conform still results in 4 byte variables

根据this overview为了编译符合Y2038的旧代码,我们只需要将预处理器宏__USE_TIME_BITS64添加到gcc,但这似乎不适用于带有 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,我这里做错了什么?

根据this post(现在有点老了,其中一些部分可能不再相关):

... 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.

据推测,这包括制作 time_t 64 位。

因此,如果您的 glibc 版本完全支持此功能,那么您似乎设置了错误的宏。你想要:

-D_TIME_BITS=64