Configure Build for External C/C++ Code
To integrate your external C/C++ code with MATLAB®, you must provide the external files to the code generator. These files consist of source files, header files, object files, and library files that are used to build the generated code.
You can configure the build at the command line, within a function, or by setting code
generation configuration object properties. Specify files at the command line for a quick and
simple way to generate code. When you want to preconfigure a function for other projects and
code deployments, configure the build within the function. The configuration object provides a
standardized set of build properties. You can also specify external files by using the
MATLAB
Coder™ App, or by using a class derived from coder.ExternalDependency
. For
more information, see Develop Interface for External C/C++ Code.
Provide External Files for Code Generation
Suppose that you want to generate code for a function that uses
coder.ceval
to call the C function myCFn
. The
external source and header files for myCFn
reside in the folder
C:\custom
. Use this
command:
codegen myMatlabFn C:\custom\myCFn.c C:\custom\myCFn.h
Configure Build from Within a Function
This example shows how to configure the build for external C/C++ code from within a MATLAB® function. Configure the build within a function so that you can more easily integrate it with other projects.
Suppose that you have a top-level MATLAB function, myFn
:
function [out] = myFn(in) %#codegen y = mySubFn(in); out = y + 10; end
This function calls another function, mySubFn
, that uses the external C code foo.c
. By using coder.updateBuildInfo
and coder.cinclude
, you set all the necessary external code dependencies from within mySubFn
.
function [y] = mySubFn(x) %#codegen coder.cinclude('foo.h'); coder.updateBuildInfo('addSourceFiles', 'foo.c'); % Pre-initialize y to double type. y = 0; y = coder.ceval('foo',x); end
You can generate code containing mySubFn
without needing to configure additional build settings or specify external file inputs at the command line. To generate code for the top-level function myFn
, enter:
codegen myFn -args {5} -report
Code generation successful: To view the report, open('codegen/mex/myFn/html/report.mldatx')
Configure Build
Customize a build in the MATLAB Coder app by using code configuration parameters or at the command line by using properties of the code generation configuration object. With these settings, you can specify external file locations, custom source code, and other build parameters.
Code Generation Parameter | Configuration Object Property | Description |
---|---|---|
Header file |
| Specify code to appear near the top of each C/C++ header file generated from your MATLAB code. |
Additional include directories |
| Specify a list of include directories to add to the include path when compiling the generated code. Provide an absolute path or a path relative to the project folder. |
Additional libraries |
| Specify a list of static library or object files to link with the generated code. |
Additional source files |
| Specify a list of source files to compile and link with the generated
code. The build process looks for the source files first in the current folder and
then in the include folders that you specify in
|
Source file |
| Specify code to appear near the top of the generated C/C++ source file, outside of a function. Do not specify a C static function definition. |
For example, at the command line, declare a standalone code configuration object and specify these properties:
cfg = coder.config('lib'); cfg.CustomInclude = ["C:\custom\src", "C:\custom\lib"]; cfg.CustomSource = "cfunction.c"; cfg.CustomLibrary = ["chelper.obj", "clibrary.lib"]; cfg.CustomSourceCode = '#include "cgfunction.h"';
Apply the properties at the command line by using the
codegen
command with the -config
argument:
codegen -config cfg myMatlabFn
See Also
codegen
| coder.cinclude
| coder.updateBuildInfo
| coder.config
| coder.CodeConfig
| coder.MexCodeConfig
| coder.ExternalDependency