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

6 vues (au cours des 30 derniers jours)
Arwel
Arwel le 17 Fév 2021
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

Réponse acceptée

Darshan Ramakant Bhat
Darshan Ramakant Bhat le 18 Fév 2021
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

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB Coder dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by