Problem with basics of including C++ code with coder (coder.ceval)

3 ビュー (過去 30 日間)
Arwel
Arwel 2021 年 2 月 17 日
編集済み: Walter Roberson 2021 年 2 月 18 日
Hi,
I'm trying to incorporate existing c++ functions into Matlab coder using coder.ceval, but am struggling to even get a basc example working. I've seen the C example here, but am struggling to get even a simple 'hello world working with c++.
I've written a functin 'helloWorld.cpp' as follows:
#include <iostream>
#include "helloWorld.h"
using namespace std;
double helloWorld(double inpt) {
cout << "Hello World!!!" << endl;
cout << "Input is " << inpt << endl;
return inpt;
}
And then made 'helloWorld.h' as follows:
#if defined __cplusplus
extern "C" {
#endif
#ifndef HELLOWORLD_H_
#define HELLOWORLD_H_
double helloWorld(double inpt);
#endif /* HELLOWORLD_H_ */
#if defined __cplusplus
}
#endif
My matlab function is then:
function out = helloWorld(inpt)
%#codegen
if coder.target('MATLAB')
fprintf('Hello World! \n');
fprintf('Input number is %d \n', inpt);
out = inpt;
else
out = 0;
source1 = 'helloWorld.cpp';
coder.updateBuildInfo('addSourceFiles',source1);
out = coder.ceval('helloWorld',inpt);
end
end
I tried to compile this using the coder app, calling it as:
inpt = 2.0; out = helloWorld(inpt);
But it fails, with the Target Build Log telling me:
helloWorld1.c: In function helloWorld:
helloWorld1.c:24:3: error: incompatible type for argument 1 of ‘helloWorld’
   return helloWorld(inpt);
   ^
helloWorld1.c:18:8: note: expected const struct emlrtStack *’ but argument is of type ‘real_T’
 real_T helloWorld(const emlrtStack *sp, real_T inpt)
        ^
helloWorld1.c:24:3: error: too few arguments to function ‘helloWorld’
   return helloWorld(inpt);
   ^
helloWorld1.c:18:8: note: declared here
 real_T helloWorld(const emlrtStack *sp, real_T inpt)
What am I doing wrong here?
Cheers,
Arwel

採用された回答

Darshan Ramakant Bhat
Darshan Ramakant Bhat 2021 年 2 月 18 日
I changed the MATLAB file name to avoid the clash. Also added coder.cinclude as suggested by Walter.
function out = helloWorldWrapper(inpt)
%#codegen
if coder.target('MATLAB')
fprintf('Hello World! \n');
fprintf('Input number is %d \n', inpt);
out = inpt;
else
out = 0;
source1 = 'helloWorld.cpp';
coder.cinclude('helloWorld.h');
coder.updateBuildInfo('addSourceFiles',source1);
out = coder.ceval('helloWorld',inpt);
end
end
I was able create mex using the below command
>> codegen helloWorldWrapper -args {0} -report
Code generation successful: View report
>> helloWorldWrapper_mex(5)
Hello World!!!
Input is 5
ans =
5
  1 件のコメント
Arwel
Arwel 2021 年 2 月 18 日
That's awesome Darshan - thanks very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB Coder についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by