You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Vcpkg编译MXChip AZ3166固件时遇_gettimeofday未定义引用错误

MXChip AZ3166编译时time函数未定义错误解决

问题场景

Win11系统下已安装Vcpkg,使用VS2022以C语言编译MXChip AZ3166固件,调用time相关库函数获取时间戳/时间差时出现链接错误,错误信息如下:

FAILED: app/mxchip_azure_iot.elf 
  cmd.exe /C "cd . && C:\PROGRA~2\GNUARM~1\102021~1.10\bin\AR19DD~1.EXE -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -g3 -ffunction-sections -fdata-sections -fno-strict-aliasing -fno-builtin -fno-common -Wall -Wshadow -Wdouble-promotion -Werror -Wno-unused-parameter -O0 --specs=nano.specs -Wl,--gc-sections,-print-memory-usage    -TMXChip_AZ3166.ld -Wl,-Map=mxchip_azure_iot.map @CMakeFiles\mxchip_azure_iot.rsp -o app\mxchip_azure_iot.elf  && cd ."
  c:/progra~2/gnuarm~1/102021~1.10/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/progra~2/gnuarm~1/102021~1.10/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard\libg_nano.a(lib_a-gettimeofdayr.o): in function `_gettimeofday_r':
  gettimeofdayr.c:(.text._gettimeofday_r+0xe): undefined reference to `_gettimeofday'

相关代码:

#include <time.h>
time_t prior_t;

static UINT append_device_timestamp(NX_AZURE_IOT_JSON_WRITER* json_writer)
{
    time_t curr_t;
    double diff_t;

    printf("Calc Diff...\n");
    time(&curr_t);

    if (!prior_t)
    {
        diff_t = difftime(curr_t, prior_t);
        printf("Diff = %f\n", diff_t);
    }
    else
    {
        printf("prior_t was NULL");
    }
    
    prior_t = curr_t;
}

使用的GNU Arm Embedded Toolchain中time.h头文件内容:

/*
 * time.h
 * 
 * Struct and function declarations for dealing with time.
 */

#ifndef _TIME_H_
#define _TIME_H_

#include "_ansi.h"
#include <sys/cdefs.h>
#include <sys/reent.h>

#define __need_size_t
#define __need_NULL
#include <stddef.h>

/* Get _CLOCKS_PER_SEC_ */
#include <machine/time.h>

#ifndef _CLOCKS_PER_SEC_
#define _CLOCKS_PER_SEC_ 1000
#endif

#define CLOCKS_PER_SEC _CLOCKS_PER_SEC_
#define CLK_TCK CLOCKS_PER_SEC

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

#if __POSIX_VISIBLE >= 200809
#include <sys/_locale.h>
#endif

_BEGIN_STD_C

struct tm
{
  int   tm_sec;
  int   tm_min;
  int   tm_hour;
  int   tm_mday;
  int   tm_mon;
  int   tm_year;
  int   tm_wday;
  int   tm_yday;
  int   tm_isdst;
#ifdef __TM_GMTOFF
  long  __TM_GMTOFF;
#endif
#ifdef __TM_ZONE
  const char *__TM_ZONE;
#endif
};

clock_t    clock (void);
double     difftime (time_t _time2, time_t _time1);
time_t     mktime (struct tm *_timeptr);
time_t     time (time_t *_timer);
#ifndef _REENT_ONLY
char      *asctime (const struct tm *_tblock);
char      *ctime (const time_t *_time);
struct tm *gmtime (const time_t *_timer);
struct tm *localtime (const time_t *_timer);
#endif
size_t     strftime (char *__restrict _s,
                 size_t _maxsize, const char *__restrict _fmt,
                 const struct tm *__restrict _t);

#if __POSIX_VISIBLE >= 200809
extern size_t strftime_l (char *__restrict _s, size_t _maxsize,
              const char *__restrict _fmt,
              const struct tm *__restrict _t, locale_t _l);
#endif

char      *asctime_r    (const struct tm *__restrict,
                 char *__restrict);
char      *ctime_r  (const time_t *, char *);
struct tm *gmtime_r     (const time_t *__restrict,
                 struct tm *__restrict);
struct tm *localtime_r  (const time_t *__restrict,
                 struct tm *__restrict);

_END_STD_C

#ifdef __cplusplus
extern "C" {
#endif

#if __XSI_VISIBLE
char      *strptime (const char *__restrict,
                 const char *__restrict,
                 struct tm *__restrict);
#endif
#if __GNU_VISIBLE
char *strptime_l (const char *__restrict, const char *__restrict,
          struct tm *__restrict, locale_t);
#endif

#if __POSIX_VISIBLE
void      tzset     (void);
#endif
void      _tzset_r  (struct _reent *);

/* getdate functions */

#ifdef HAVE_GETDATE
#if __XSI_VISIBLE >= 4
#ifndef _REENT_ONLY
#define getdate_err (*__getdate_err())
int *__getdate_err (void);

struct tm * getdate (const char *);
/* getdate_err is set to one of the following values to indicate the error.
     1  the DATEMSK environment variable is null or undefined,
     2  the template file cannot be opened for reading,
     3  failed to get file status information,
     4  the template file is not a regular file,
     5  an error is encountered while reading the template file,
     6  memory allication failed (not enough memory available),
     7  there is no line in the template that matches the input,
     8  invalid input specification  */
#endif /* !_REENT_ONLY */
#endif /* __XSI_VISIBLE >= 4 */

#if __GNU_VISIBLE
/* getdate_r returns the error code as above */
int     getdate_r (const char *, struct tm *);
#endif /* __GNU_VISIBLE */
#endif /* HAVE_GETDATE */

/* defines for the opengroup specifications Derived from Issue 1 of the SVID.  */
#if __SVID_VISIBLE || __XSI_VISIBLE
extern __IMPORT long _timezone;
extern __IMPORT int _daylight;
#endif
#if __POSIX_VISIBLE
extern __IMPORT char *_tzname[2];

/* POSIX defines the external tzname being defined in time.h */
#ifndef tzname
#define tzname _tzname
#endif
#endif /* __POSIX_VISIBLE */

#ifdef __cplusplus
}
#endif

#include <sys/features.h>

#ifdef __CYGWIN__
#include <cygwin/time.h>
#endif /*__CYGWIN__*/

#if defined(_POSIX_TIMERS)

#include <signal.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Clocks, P1003.1b-1993, p. 263 */

int clock_settime (clockid_t clock_id, const struct timespec *tp);
int clock_gettime (clockid_t clock_id, struct timespec *tp);
int clock_getres (clockid_t clock_id, struct timespec *res);

/* Create a Per-Process Timer, P1003.1b-1993, p. 264 */

int timer_create (clockid_t clock_id,
    struct sigevent *__restrict evp,
    timer_t *__restrict timerid);

/* Delete a Per_process Timer, P1003.1b-1993, p. 266 */

int timer_delete (timer_t timerid);

/* Per-Process Timers, P1003.1b-1993, p. 267 */

int timer_settime (timer_t timerid, int flags,
    const struct itimerspec *__restrict value,
    struct itimerspec *__restrict ovalue);
int timer_gettime (timer_t timerid, struct itimerspec *value);
int timer_getoverrun (timer_t timerid);

/* High Resolution Sleep, P1003.1b-1993, p. 269 */

int nanosleep (const struct timespec  *rqtp, struct timespec *rmtp);

#ifdef __cplusplus
}
#endif
#endif /* _POSIX_TIMERS */

#if defined(_POSIX_CLOCK_SELECTION)

#ifdef __cplusplus
extern "C" {
#endif

int clock_nanosleep (clockid_t clock_id, int flags,
    const struct timespec *rqtp, struct timespec *rmtp);

#ifdef __cplusplus
}
#endif

#endif /* _POSIX_CLOCK_SELECTION */

#ifdef __cplusplus
extern "C" {
#endif

/* CPU-time Clock Attributes, P1003.4b/D8, p. 54 */

/* values for the clock enable attribute */

#define CLOCK_ENABLED  1  /* clock is enabled, i.e. counting execution time */
#define CLOCK_DISABLED 0  /* clock is disabled */

/* values for the pthread cputime_clock_allowed attribute */

#define CLOCK_ALLOWED    1 /* If a thread is created with this value a */
                           /*   CPU-time clock attached to that thread */
                           /*   shall be accessible. */
#define CLOCK_DISALLOWED 0 /* If a thread is created with this value, the */
                           /*   thread shall not have a CPU-time clock */
                           /*   accessible. */

/* Flag indicating time is "absolute" with respect to the clock
   associated with a time.  Value 4 is historic. */

#define TIMER_ABSTIME   4

/* Manifest Constants, P1003.1b-1993, p. 262 */

#if __GNU_VISIBLE
#define CLOCK_REALTIME_COARSE   ((clockid_t) 0)
#endif

#define CLOCK_REALTIME      ((clockid_t) 1)

/* Manifest Constants, P1003.4b/D8, p. 55 */

#if defined(_POSIX_CPUTIME)

/* When used in a clock or timer function call, this is interpreted as
   the identifier of the CPU_time clock associated with the PROCESS
   making the function call.  */

#define CLOCK_PROCESS_CPUTIME_ID ((clockid_t) 2)

#endif

#if defined(_POSIX_THREAD_CPUTIME)

/*  When used in a clock or timer function call, this is interpreted as
    the identifier of the CPU_time clock associated with the THREAD
    making the function call.  */

#define CLOCK_THREAD_CPUTIME_ID ((clockid_t) 3)

#endif

#if defined(_POSIX_MONOTONIC_CLOCK)

/*  The identifier for the system-wide monotonic clock, which is defined
 *  as a clock whose value cannot be set via clock_settime() and which
 *  cannot have backward clock jumps. */

#define CLOCK_MONOTONIC     ((clockid_t) 4)

#endif

#if __GNU_VISIBLE

#define CLOCK_MONOTONIC_RAW ((clockid_t) 5)

#define CLOCK_MONOTONIC_COARSE  ((clockid_t) 6)

#define CLOCK_BOOTTIME      ((clockid_t) 7)

#define CLOCK_REALTIME_ALARM    ((clockid_t) 8)

#define CLOCK_BOOTTIME_ALARM    ((clockid_t) 9)

#endif

#if defined(_POSIX_CPUTIME)

/* Accessing a Process CPU-time CLock, P1003.4b/D8, p. 55 */

int clock_getcpuclockid (pid_t pid, clockid_t *clock_id);

#endif /* _POSIX_CPUTIME */

#if defined(_POSIX_CPUTIME) || defined(_POSIX_THREAD_CPUTIME)

/* CPU-time Clock Attribute Access, P1003.4b/D8, p. 56 */

int clock_setenable_attr (clockid_t clock_id, int attr);
int clock_getenable_attr (clockid_t clock_id, int *attr);

#endif /* _POSIX_CPUTIME or _POSIX_THREAD_CPUTIME */

#ifdef __cplusplus
}
#endif

#endif /* _TIME_H_ */

问题原因

嵌入式系统无标准POSIX环境的系统时间实现,链接选项--specs=nano.specs调用的newlib nano库为精简体积,仅提供_gettimeofday等系统级函数的声明,未实现底层硬件相关逻辑。而标准C的time()函数最终依赖_gettimeofday,因此出现未定义引用错误。

解决方法

方法1:实现_gettimeofday系统调用

基于Azure RTOS系统时钟补充底层实现,代码如下:

#include <sys/time.h>
#include <nx_api.h>

int _gettimeofday(struct timeval *tv, void *tz)
{
    if (tv != NULL)
    {
        // 获取系统启动后运行时长(毫秒)
        ULONG milliseconds = nx_system_uptime_get();
        
        tv->tv_sec = milliseconds / 1000;
        tv->tv_usec = (milliseconds % 1000) * 1000;
    }
    return 0;
}

如果需要真实UTC时间,需先通过NTP或Azure IoT Hub完成时间同步,再替换此处的时间源。

方法2:直接使用Azure RTOS时钟函数

放弃标准C time库,改用Azure RTOS原生API计算时间差,更适配嵌入式场景:

#include <nx_api.h>
ULONG prior_ticks = 0;

static UINT append_device_timestamp(NX_AZURE_IOT_JSON_WRITER* json_writer)
{
    ULONG curr_ticks = nx_system_uptime_get();
    double diff_sec;

    printf("Calc Diff...\n");

    if (prior_ticks != 0)
    {
        diff_sec = (double)(curr_ticks - prior_ticks) / 1000.0;
        printf("Diff = %f\n", diff_sec);
    }
    else
    {
        printf("prior_ticks was not initialized");
    }
    
    prior_ticks = curr_ticks;
    return 0;
}

nx_system_uptime_get()返回系统启动后的毫秒数,直接计算差值后转换为秒即可,无需依赖标准time库。

方法3:修改链接选项使用完整newlib库

若无需精简固件体积,可移除链接选项中的--specs=nano.specs,改用完整newlib库。但此方法会增大固件大小,不推荐在资源有限的嵌入式设备使用。


额外代码逻辑修正

原代码中if (!prior_t)的判断存在问题:prior_t为time_t类型,初始值0对应1970年1月1日,并非未初始化状态。建议将prior_t初始化为0,判断prior_t == 0时跳过第一次差值计算,或初始化为-1区分未初始化状态。

内容的提问来源于stack exchange,提问作者Sergio Solorzano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 09:41:29