Main Content

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 by Using the Configuration Object

Customize a build by setting properties of the code generation configuration object. With these properties you can specify external file locations, custom source code, and other build parameters.

Custom Code PropertyDescription

CustomHeaderCode

Specify code to appear near the top of each C/C++ header file generated from your MATLAB code.

CustomInclude

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.

CustomLibrary

Specify a list of static library or object files to link with the generated code.

CustomSource

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 CustomInclude.

CustomSourceCode

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, 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

Configure Build by Using the MATLAB Coder App

  1. Open the MATLAB Coder App and proceed to the Generate Code step.

  2. On the Generate Code page, to open the Generate dialog box, click the Generate arrow .

  3. Click More Settings.

  4. On the Custom Code tab, choose your build configuration settings. Click Help to display information about the entry fields.

See Also

| | | | | |

Related Topics