找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3482|回复: 0

qq空间老文章转载-精通Matlab与C/C++混合程序设计 第二版 排错

[复制链接]

307

主题

228

回帖

7335

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5585 个
贡献
253 次
宅之契约
0 份
在线时间
947 小时
注册时间
2014-1-25
发表于 2014-2-19 19:18:27 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
精通Matlab与C/C++混合程序设计 第二版 排错                 

4.2 Matlab M文件中调用C函数
matlab程序如下:
%保存为showdata.m文件
function showdata
y = zeros(1, 100); %将y分配为1 x 100的数组
for i = 1:100
   y(i) = getdata;
end
plot(y);
%保存为getdata.m文件
function y = getdata
%#external
%persistent定义与c语言的static类似。采用persistent定义的变量在函数调
%用结束的时候,其仍然保存在内存中,下次函数调用的时候仍然可以读取其值
persistent t;
if (isempty(t))
t = 0;
end
t = t + 0.05;
y = sin(t);

书上说mcc -m showdata.m getdata.m可生成getdata_external.h,可经我测试发现并无此文件,尝试了半天,发现mcc -W lib:getdata getdata.m可以生成该文件,难道我的matlab版本太新了?
步骤如下:
① mcc -m showdata.m        mcc -W lib:getdata getdata.m
    ②编写getdata_external.c:
#include "getdata_external.h"
bool MW_CALL_CONV getdata(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
        static double t=0.0;
        t=t+0.5;
        plhs[0]=mxCreateDoubleScalar(sin(t));
}
③ mbuild showdata_main.c showdata_mcc_component_data.c getdata_external.c -output showdata.exe
此时showdata.exe生成



Could not access the MCR component cache错误解决           
今天在处理matlab和c++混合联编时,在本机生成exe并放入目标主机(我的在vmware里xp sp3)
步骤如下:
①matlab中的showdata.m:
function showdata
y=zeros(1,100);
for i=1:100
    y(i)=getdata;
end
plot(y);

在matlab中用mcc -m showdata.m 命令编译成showdata.exe

②将matlab\runtime\win32下的文件考入目标机中,和showdata.exe放一起或者放入system32文件夹下
注意,如果这步不正确,会提示could not find version 7.13 of the MCR.....

在目标机中安装mcr,即matlab运行库,安装文件在matlab目录下,位于:
\matlab\toolbox\compiler\deploy\win32\ MCRInstaller.exe
安好后会生成一堆dll(bin\win32),他们可以放到system32下,也可以吧showdata考入其中
注意,如果这步不正确,会提示My Own Exception:Fatal error loading library ...\libmx.dll Error:找不到指定的模块

这些完成以后,第一次运行exe的确生成了matlab那种的正弦图像,可是我在vmware里把xp重启以后,发现又不行了,提示:
Could not access the MCR component cache这个错误可很牛逼了,google里找了好久才找到:
MCR_CACHE_ROOTMATLAB stand alone executables may fail with Could not access the MCR component cache. error message because MATLAB is unable to access the MCE cache directory, which it expects to be in your home directory.
This can be avoided by pointing the MCR_CACHE_ROOT variable to a non-existent directory, e.g.
export MCR_CACHE_ROOT=/tmp/emptydirNote: these environment variables can be set from the Condor submit script, i.e. environment = MCR_CACHE_ROOT=/tmp/emptydir;MATLAB_PREFDIR=/tmp/emptydir


就是在系统变量里重建一个 MCR_CACHE_ROOT变量,并设置为一个无效目录即可
最后问题成功解决


今天在处理matlab和c++混合联编时,在本机生成exe并放入目标主机(我的在vmware里xp sp3)
步骤如下:
①matlab中的showdata.m:
function showdata
y=zeros(1,100);
for i=1:100
    y(i)=getdata;
end
plot(y);

在matlab中用mcc -m showdata.m 命令编译成showdata.exe

②将matlab\runtime\win32下的文件考入目标机中,和showdata.exe放一起或者放入system32文件夹下
注意,如果这步不正确,会提示could not find version 7.13 of the MCR.....

在目标机中安装mcr,即matlab运行库,安装文件在matlab目录下,位于:
\matlab\toolbox\compiler\deploy\win32\ MCRInstaller.exe
安好后会生成一堆dll(bin\win32),他们可以放到system32下,也可以吧showdata考入其中
注意,如果这步不正确,会提示My Own Exception:Fatal error loading library ...\libmx.dll Error:找不到指定的模块

这些完成以后,第一次运行exe的确生成了matlab那种的正弦图像,可是我在vmware里把xp重启以后,发现又不行了,提示:
Could not access the MCR component cache这个错误可很牛逼了,google里找了好久才找到:
MCR_CACHE_ROOTMATLAB stand alone executables may fail with Could not access the MCR component cache. error message because MATLAB is unable to access the MCE cache directory, which it expects to be in your home directory.
This can be avoided by pointing the MCR_CACHE_ROOT variable to a non-existent directory, e.g.
export MCR_CACHE_ROOT=/tmp/emptydirNote: these environment variables can be set from the Condor submit script, i.e. environment = MCR_CACHE_ROOT=/tmp/emptydir;MATLAB_PREFDIR=/tmp/emptydir


就是在系统变量里重建一个 MCR_CACHE_ROOT变量,并设置为一个无效目录即可
最后问题成功解决



回复

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-19 12:05 , Processed in 0.042021 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表