Using "coder.ceval" in the MATLAB Coder App:
The function "coder.ceval" is not supported for MATLAB scripts and is intended only to be used for code generation workflows. The MATLAB Coder App runs the function in MATLAB as an initial step, which is why some users report errors when moving from using the "codebuild" function to using the Coder App. To prevent the error message that "coder.ceval" is not supported in MATLAB, you should modify your function so that it includes a call to "coder.target". For example, say you wanted to generate code for your own addition function using an external custom library, the MATLAB function to be used should look like the following:
function Y = myAddition(A, B)
if coder.target("MATLAB")
Y = A + B;
else
Y = int32(0);
coder.cinclude("myAdd.h");
coder.updateBuildInfo("addSourceFiles","myAdd.c");
Y = coder.ceval("myAdd", int32(A), int32(B));
end
end
Linker Error when Mixing C and C++ Code
Mixing C and C++ code is supported by compilers and, by extension, it can be done with MATLAB Coder. However, in accordance with the ISO C standard, you may need to make some modifications to your source code to make it compatible for compilation in the alternative language. This is a limitation of the C and C++ languages rather than the MATLAB Coder, so more information on how to do this can be found at the following link:
As an example of this, say that you have a "subtraction" function that you now also wish to generate code from. It is a C++ function but you are using a C compiler (as the MATLAB Coder App does in its verification steps). The header file for the "mySubtract" library may originally look as follows:
#ifndef MYCPPFUNC
#define MYCPPFUNC
int mySubtract(int a, int b);
#endif
You should modify this with preprocessor checks for the target language as follows:
#ifndef MYCPPFUNC
#define MYCPPFUNC
#ifdef __cplusplus
extern "C" {
#endif
int mySubtract(int a, int b);
#ifdef __cplusplus
}
#endif
#endif
Both the C and C++ files described in this article can now be called from within the function using "coder.ceval". An example of this, "MixCandCPP.zip" is attached. Please step through the MATLAB Coder App Projects for "myCustomAdd", "myCustomSubtract", and "myCustomMaths" to see the full workflow.