To integrate external code with generated C/C++ code, you must specify the locations of your external source files, header files, and libraries to MATLAB® Coder™.
You can specify the file locations:
In a class definition file, when you derive a class
from coder.ExternalDependency
In your MATLAB code using the coder.updateBuildInfo
function
In the project settings dialog box
From the command line
In the configuration object
When you derive a class from coder.ExternalDependency
,
you write a method updateBuildInfo
that specifies
the locations of the external files required for the build. See coder.ExternalDependency
.
In your MATLAB code, you can call coder.updateBuildInfo
to
specify the locations of external files. See coder.updateBuildInfo
.
On the Generate Code page, to
open the Generate dialog box, click the Generate arrow
.
In the Generate dialog box, set the Build Type to one of the following:
Source Code
Static Library
Dynamic Library
Executable
Click More Settings.
On the Custom Code tab, under Custom C-code to include in generated files, specify Source file and Header file. Source file specifies that the code appear at the top of generated C/C++ source files. Header file specifies that the code appear at the top of generated header files.
Custom Code Property | Description |
---|---|
Under Additional files and directories to be built, provide an absolute path or a path relative to the project folder. | |
Include directories | Specifies a list of folders that contain custom header, source, object, or library files. Separate list items with a semicolon. |
Source files | Specifies additional custom C/C++ files to be compiled with the MATLAB file. Separate list items with a semicolon. |
Libraries | Specifies the names of object or library files to be linked with the generated code. Separate list items with a semicolon. |
Under Custom C-code to include in generated files | |
Source file | Specifies code to appear at the top of generated C/C++ source files. |
Header file | Specifies custom code to appear at the top of generated header files |
When you compile MATLAB function with MATLAB Coder,
you can specify custom C/C++ files — such as source, header,
and library files — on the command line along with your MATLAB file.
For example, suppose you want to generate an embeddable C code executable
that integrates a custom C function myCfcn
with
a MATLAB function myMfcn
that has no
input parameters. The custom source and header files for myCfcn
reside
in the folder C:\custom
. You can use the following
command to generate the code:
codegen C:\custom\myCfcn.c C:\custom\myCfcn.h myMfcn
You can specify custom C/C++ files by setting custom code properties on configuration objects.
Define a configuration object, as described in Creating Configuration Objects.
For example:
cc = coder.config('lib');
Set one or more of the custom code properties.
Custom Code Property | Description | |
---|---|---|
| Specifies a list of folders that contain custom header, source, object, or library files.
| |
| Specifies additional custom C/C++ files to be compiled with the MATLAB file. | |
| Specifies the names of object or library files to be linked with the generated code. | |
| Specifies code to insert at the top of each generated C/C++ source file. | |
| Specifies custom code to insert at the top of each generated C/C++ header file. |
For example:
cc.CustomInclude = 'C:\custom\src C:\custom\lib'; cc.CustomSource = 'cfunction.c'; cc.CustomLibrary = 'chelper.obj clibrary.lib'; cc.CustomSourceCode = '#include "cgfunction.h"';
Compile the MATLAB code specifying the code generation configuration object.
Note: If you generate code for a function that has input parameters, you must specify the inputs. Primary Function Input Specification |
codegen -config cc myFunc
Call custom C/C++ functions.
From... | Call... |
---|---|
C/C++ source code | Custom C/C++ functions directly |
MATLAB code, compiled on the MATLAB Coder path | Custom C/C++ functions using coder.ceval . |
For example, from MATLAB code:
... y = 2.5; y = coder.ceval('myFunc',y); ...