如何解决编译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 举报,一经查实,本站将立刻删除。