Filter löschen
Filter löschen

Calling external (C++) static methods

2 Ansichten (letzte 30 Tage)
Jon
Jon am 21 Nov. 2023
Bearbeitet: Jon am 27 Nov. 2023
coder.ceval(fcn,arg1,arg2)
can be used to call external c functions from autocode generated by Matlab Coder.
Is there a way to call an external class's static method using ceval call an external class's static method?
The goal here is to generate compilable autocode (not code that will actually be run from matlab) -- so I just need matlab to emit the import and ClassName::MethodName(arg1, arg2) etc. Does fcn accept ClassName::MethodName type symantics?

Akzeptierte Antwort

Jon
Jon am 27 Nov. 2023
Well, I went ahead and just tried it -- and as it turns out, matlab will generate code that uses a namespace or class, as long as you include the header containing the declaration, and have the TargetLang set to C++
coder.cinclude('ExternalHeader.hpp');
coder.ceval('ExternalClass::staticMethod',arg1,arg2);

Weitere Antworten (1)

Varun
Varun am 27 Nov. 2023
Hi Jon,
I understand that you want to use “coder.ceval” call an external class's static method. The “coder.ceval” function in MATLAB Coder is primarily designed to call external C functions. It doesn't directly support calling external C++ class methods, including static methods.
However, you can achieve the desired functionality by creating a wrapper C function that calls the C++ class method and then calling that wrapper function using coder.ceval.
Here's a general outline of the steps:
Create a C++ wrapper function that calls the static method of the external class:
// WrapperFunction.cpp
#include "ExternalClass.h"
extern "C" void callStaticMethodWrapper(int arg1, int arg2) {
ExternalClass::staticMethod(arg1, arg2);
}
In MATLAB, use "coder.ceval" to call the wrapper function:
% MATLAB script
coder.ceval('callStaticMethodWrapper', arg1, arg2);
Please refer the following documentation to learn more about using "coder.ceval":
Hope this helps.
  1 Kommentar
Jon
Jon am 27 Nov. 2023
Bearbeitet: Jon am 27 Nov. 2023
Thanks. As it turns out, matlab will happily generate code that uses a namespace or class, as long as you include the header containing the declaration, and have the TargetLang set to C++
coder.cinclude('ExternalHeader.hpp');
coder.ceval('ExternalClass::staticMethod',arg1,arg2);

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by