编译Matlab功能块后如何获得UTC时间?使用带有coder.ceval的外部C函数?

问题描述

我正在编译Simulink功能块。在这个功能中,我想得到我的机器时间。我最初尝试使用包装在now中的MATLAB函数coder.extrinsic('now')。但是,代码生成不支持此语法。经过研究,我决定使用C代码检索机器时间,并将其作为MATLAB中的外部C函数调用。

我关注了these instructions。我可以毫无问题地编译示例。

当我尝试使用MATLAB R2014b(32位)编译文件时 我遇到此编译器错误:C:\PROGRA~2\MICROS~1\2017\COMMUN~1\VC\Tools\MSVC\1416~1.270\include\vcruntime.h(196) : error C2061: syntax error : identifier '__vcrt_bool'

但是,我可以使用MATLAB版本R2019(64位)进行编译,并且可以毫无问题地使用mex64生成的文件来获取机器时间。

最后我确实需要在较旧的R2014(32位)版本中进行编译。

文件getTime.c

#include "getTime.h"   
#include "time.h"      // C Standard Library (is it trully loaded by matlab at code generation ? )

double getTimeC(){
   
    struct timespec actualTime;       //  --------> Seems to be where the error occurs
    timespec_get(&actualTime,TIME_UTC);
    time_t secondes = actualTime.tv_sec;
    long nano = actualTime.tv_nsec;

    double nano_d = (double)nano;
    double secondes_d = (double)secondes;

    double timeUTC = secondes_d + nano_d / (1000*1000*1000) ;
    return timeUTC; 
}

文件getTime.h

#pragma once

#ifndef GETTIME_H
#define GETTIME_H

double getTimeC(); 

#endif 

文件call_getTime.m是MATLAB函数,它是C的调用函数:

function [ y ] = call_getTime() %#codegen

y = 0.0; %preallocation

if coder.target('MATLAB')
    fprintf('Function executed in Matlab \n');
    y = now;
else
    coder.updateBuildInfo('addIncludePaths','C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt');
    coder.updateBuildInfo('addIncludePaths','C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\shared');
    coder.updateBuildInfo('addIncludePaths','C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\cppwinrt');
    coder.updateBuildInfo('addIncludePaths','C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um');
    coder.updateBuildInfo('addIncludePaths','C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\winrt');    
    
    coder.updateBuildInfo('addIncludePaths','C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include');

    coder.updateBuildInfo('addSourceFiles','getTime.c');
    coder.cinclude('getTime.h')
    y = coder.ceval('getTimeC');
    fprintf('Fonction executed in C \n');
end
end

我用它来生成mex文件:

codegen -config:mex call_getTime

对于R2014b版本: 我必须使用以下命令显示Microsoft Visual Studio \ 2017 \的路径: 因为Matlab编码器自己找不到标题。我了解为什么要检索计算机时间,我需要Window Kit,但是我不明白为什么还需要给出Visual Studio路径。

对于R2019版本: 您无需使用coder.updateBuildInfo('addIncludePaths',它会由coder.updateBuildInfo('addIncludePaths'自动加载

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...