MATLAB 嵌入式 C 函数问题
MATLAB Emebedded C function issue
您好,我正在尝试通过 MEX 文件在 MATLAB 中使用 C 文件,但到目前为止还没有成功。我遵循了 link 中显示的教程:https://uk.mathworks.com/videos/integrating-matlab-into-your-cc-product-development-workflow-106742.html
我下载了文件并且工作得很好。现在我正在尝试编写自己的函数,它正确地生成了 MEX 文件,但是当我尝试测试该函数时,MATLAB 关闭了。文件如下所示:
另外我检查了以下可能的原因:
- 已验证 c 中的函数在这种情况下是正确的类型 int。
- 在 C 代码中使用指针而不只是变量。
- 在c函数中包含return。
- 初始化函数输出的参考值即y=int32(15);
- 在 C 代码中包含所有头文件,即
link 中的示例完美运行,我缺少什么?
c代码
#include <stdio.h>
int bimi(int* output);
int bimi(int* output){
int a = 0;
output=25;
return output;
}
MATLAB中的函数
function y = bimi() %#codegen
%coder.updateBuildInfo('addSourceFiles','bm.c');
y = int32(15);%coder.nullcopy(1);
coder.updateBuildInfo('addSourceFiles','bimi.c');
fprintf('Running Custom C Code...\n\n');
coder.ceval('bimi',coder.ref(y));%this bimi should match with the function name!
end
用于构建 MEX 文件的脚本
function build(target)
% BUILD is a build function for the Gaussian filter
%
% SYNTAX: build mex
% build lib
%
% Copyright 2014 The MathWorks, Inc.
% Entry point function:
entryPoint = 'bimi';%
% Configuration object:
cfg = coder.config(target);
% Custom source files:
cfg.CustomSource = 'bimi.c';
% Generate and Launch Report:
cfg.GenerateReport = true;
cfg.LaunchReport = false;
% Generate Code:
codegen(entryPoint,'-config', cfg)
end
测试脚本
%testing the c code embeded to matlab
chichi=bimi_mex();
实现此目的的最佳方法是非常小心文件名和函数名(这就是问题所在,简单、基本但需要时间来发现)。看看我执行的最后一个测试,它工作得很好:
文件是:
//bimi.c
#include "bimi.h"
extern void bimi(int* poly, int* polysz, int* output){
int i;
int a=*(polysz);
for(i=0;i<a;i++){
output[i]=2*poly[i];
}
}
.
//bimi.h
extern void bimi(int* poly, int* polysz, int* output);
.
//bimifunc.m
function y = bimifunc(poly2, polysz2) %#codegen
%coder.updateBuildInfo('addSourceFiles','bm.c');
y = coder.nullcopy(zeros(1,5,'int32'));%coder.nullcopy(1);
coder.updateBuildInfo('addSourceFiles','bimi.c');
fprintf('Running Custom C Code...\n\n');
coder.ceval('bimi',coder.ref(poly2),coder.ref(polysz2), coder.ref(y));%this bimi should match with the function name!
end
.
//build.m
function build(target)
% Entry point function:
entryPoint = 'bimifunc';%
%Example input
poly=zeros(1,5, 'int32');
polysz=int32(length(poly));
% Configuration object:
cfg = coder.config(target);
% Custom source files:
cfg.CustomSource = 'bimi.c';
cfg.CustomSourceCode = [ '#include "bimi.h"' ];
% Generate and Launch Report:
cfg.GenerateReport = true;
cfg.LaunchReport = false;
% Generate Code:
codegen(entryPoint,'-args', {poly, polysz},'-config', cfg)
end
.
//test.m
%testing the c code embeded to matlab
poly=ones(1,5, 'int32');
polysz=int32(length(poly));
chichi=bimifunc_mex(poly, polysz);
您好,我正在尝试通过 MEX 文件在 MATLAB 中使用 C 文件,但到目前为止还没有成功。我遵循了 link 中显示的教程:https://uk.mathworks.com/videos/integrating-matlab-into-your-cc-product-development-workflow-106742.html
我下载了文件并且工作得很好。现在我正在尝试编写自己的函数,它正确地生成了 MEX 文件,但是当我尝试测试该函数时,MATLAB 关闭了。文件如下所示:
- 已验证 c 中的函数在这种情况下是正确的类型 int。
- 在 C 代码中使用指针而不只是变量。
- 在c函数中包含return。
- 初始化函数输出的参考值即y=int32(15);
- 在 C 代码中包含所有头文件,即
link 中的示例完美运行,我缺少什么?
c代码
#include <stdio.h> int bimi(int* output); int bimi(int* output){ int a = 0; output=25; return output; }
MATLAB中的函数
function y = bimi() %#codegen
%coder.updateBuildInfo('addSourceFiles','bm.c');
y = int32(15);%coder.nullcopy(1);
coder.updateBuildInfo('addSourceFiles','bimi.c');
fprintf('Running Custom C Code...\n\n');
coder.ceval('bimi',coder.ref(y));%this bimi should match with the function name!
end
用于构建 MEX 文件的脚本
function build(target)
% BUILD is a build function for the Gaussian filter
%
% SYNTAX: build mex
% build lib
%
% Copyright 2014 The MathWorks, Inc.
% Entry point function:
entryPoint = 'bimi';%
% Configuration object:
cfg = coder.config(target);
% Custom source files:
cfg.CustomSource = 'bimi.c';
% Generate and Launch Report:
cfg.GenerateReport = true;
cfg.LaunchReport = false;
% Generate Code:
codegen(entryPoint,'-config', cfg)
end
测试脚本
%testing the c code embeded to matlab
chichi=bimi_mex();
实现此目的的最佳方法是非常小心文件名和函数名(这就是问题所在,简单、基本但需要时间来发现)。看看我执行的最后一个测试,它工作得很好:
文件是:
//bimi.c
#include "bimi.h"
extern void bimi(int* poly, int* polysz, int* output){
int i;
int a=*(polysz);
for(i=0;i<a;i++){
output[i]=2*poly[i];
}
}
.
//bimi.h
extern void bimi(int* poly, int* polysz, int* output);
.
//bimifunc.m
function y = bimifunc(poly2, polysz2) %#codegen
%coder.updateBuildInfo('addSourceFiles','bm.c');
y = coder.nullcopy(zeros(1,5,'int32'));%coder.nullcopy(1);
coder.updateBuildInfo('addSourceFiles','bimi.c');
fprintf('Running Custom C Code...\n\n');
coder.ceval('bimi',coder.ref(poly2),coder.ref(polysz2), coder.ref(y));%this bimi should match with the function name!
end
.
//build.m
function build(target)
% Entry point function:
entryPoint = 'bimifunc';%
%Example input
poly=zeros(1,5, 'int32');
polysz=int32(length(poly));
% Configuration object:
cfg = coder.config(target);
% Custom source files:
cfg.CustomSource = 'bimi.c';
cfg.CustomSourceCode = [ '#include "bimi.h"' ];
% Generate and Launch Report:
cfg.GenerateReport = true;
cfg.LaunchReport = false;
% Generate Code:
codegen(entryPoint,'-args', {poly, polysz},'-config', cfg)
end
.
//test.m
%testing the c code embeded to matlab
poly=ones(1,5, 'int32');
polysz=int32(length(poly));
chichi=bimifunc_mex(poly, polysz);