I have a MATLAB code and its using a C++ code.
When I try to build it using MATLAB Coder i am getting an error
Undefined function or variable 'nameOfC++file'.
How do I include / define it before building ?
PS : The matlab code works without defining this C++ file beforehand.
EDIT
How it is called
r = mlist(d, p);
I have two files, mlist.cpp and mlist.dll
Some suggestions to get things started:
Configure MATLAB Coder to generate C++ code to ensure consistent compilation and linking:
cfg = coder.config('lib');
cfg.TargetLang = 'C++';
codegen -config cfg ...
Include the needed header(s). In your MATLAB code for code generation use:
function y = example(x)
%#codegen
coder.cinclude('a_header.h');
coder.cinclude('<a_system_header>');
If necessary, add include paths to the config object, cfg
:
cfg.CustomInclude = '/directory/with/headers /other/directory';
Call your external function(s) using coder.ceval
:
function y = example(x)
%#codegen
coder.cinclude('a_header.h');
coder.cinclude('<a_system_header>');
% Assume y is a double scalar. Change this to match the return type
% of someExternalFunction
y = 0;
y = coder.ceval('someExternalFunction',x);
Pass any necessary C++ source files (.cpp
), object files (.o, .obj
), or libraries (.a, .so, .lib, .dylib
etc.) to the codegen command:
codegen -config cfg matlab_function_name source.cpp library.so ...
The documentation on external code integration shows other techniques for encapsulating dependencies on external code. Using these allows you to specify the necessary libraries, external source code, compiler flags, and other options from your MATLAB code being passed to MATLAB Coder.
That means that your code becomes self-contained so that the config object need not be modified and the call to codegen
need not include the source files, object files, and libraries.