How can I mix external C and C++ files in a MATLAB Coder Project?

8 visualizaciones (últimos 30 días)
I am looking to generate C++ code using a MATLAB Coder Project and include some previously written C and C++ code in the build. I have seen that you can add externally authored C/C++ code to your build using the "coder.ceval" function. However, when I try to build code using the MATLAB Coder App, I get the following blocking issues:
  • An error message that says "The coder.ceval function is not supported in MATLAB"
  • A failure in the verification step. The C code builds fine but the C++ code fails with the compiler error:
error LNK2019: unresolved external symbol mySubtract referenced in function myFunc
  Hint on symbols that are defined and could potentially match:
 "int __cdecl myFunc(int,int)"
Is mixing C and C++ code supported? How can I do this?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 27 de Sept. de 2023
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
% Predeclare variable
Y = int32(0);
  % Add library and source files to BuildInfo object for compilation
coder.cinclude("myAdd.h");
coder.updateBuildInfo("addSourceFiles","myAdd.c");
  % Use coder.ceval to call the external library function
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.

Más respuestas (0)

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by