【问题标题】:Teensy's #include <TimeLib.h> got overridden by ArduinoTeensy 的 #include <TimeLib.h> 被 Arduino 覆盖
【发布时间】:2019-10-18 23:27:06
【问题描述】:

在 Arduino C+ 中,我想避免使用 32 位签名 time_t 类型时出现 2038 年溢出问题,因此我想专门使用 Time.h 来自 Teensy(或 TimeLib.h,我正在为 Arduino 1.8.7 上的 Teensy 3.5 编写代码)。

但IDE似乎忽略了Teensy的Time.h,其中time_t定义为:

typedef unsigned long time_t;

我发现无论我包含什么,我使用的 time_t 类型都编译为“long int”。这段代码表明:

time_t t = "ABC";

编译器将显示 time_t 实际上在某处定义为 long int

invalid conversion from 'const char*' to 'time_t {aka long int}' [-fpermissive]

我什至尝试将 Teensy 的 Time 文件夹 (https://github.com/PaulStoffregen/Time) 复制到我的草图文件夹中,但没有成功:

#include "Time\TimeLib.h"

如何确保我在 Arduino 中使用的是无符号的 32 位 time_t? 我也希望当我调用 now() 时,返回 unsigned long time_t 的是 Teensy 的 now(),而不是内置的 long int time_t

提前致谢!

【问题讨论】:

    标签: c++ arduino time.h time-t teensy


    【解决方案1】:

    在 teensy TimeLib.h 中定义为:

    #if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc
    typedef unsigned long time_t;
    #endif
    

    sys/_types.h 将其定义为:

    #define _TIME_T_    long        /* time() */
    typedef _TIME_T_    __time_t;
    

    在几个地方用作:

    #if !defined(__time_t_defined) && !defined(_TIME_T_DECLARED)
    typedef _TIME_T_    time_t;
    #define __time_t_defined
    #define _TIME_T_DECLARED
    #endif
    

    所以它被忽略了并不是一个谜。否则由于类型冲突,您将无法编译。

    【讨论】:

    • 谢谢,现在我可以看得更清楚了。有没有办法覆盖 sys/_types.h 中的定义?
    • @Dave 取决于使用情况。如果您想使用 TimeLib,最好将 time_t 更改为 utime_t 或其他类似的东西。或者可能将其隐藏到单独的命名空间中
    猜你喜欢
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 2018-04-16
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多