By default, when MATLAB® Coder™ generates a dynamic library (DLL):
The DLL is suitable for the platform that you are working on.
The DLL uses the release version of the C run-time library.
The DLL linkage conforms to the target language, by default, C. If you set the target language to C++, the linkage conforms to C++.
When the target language is C, the generated header
files explicitly declare the exported functions to be extern
"C"
to simplify integration of the DLL into C++ applications.
When an executable that uses the DLL runs, the DLL must be on the system path so that the executable can access it.
If you generate a DLL that uses dynamically allocated variable-size data, MATLAB Coder provides exported utility functions to interact with this data in the generated code. For more information, see Utility Functions for Creating emxArray Data Structures.
This example shows how to generate a C DLL from MATLAB code using the MATLAB Coder app.
Create the Entry-Point Functions
Write two MATLAB functions, ep1
and ep2
. ep1
takes
one input, a single scalar. ep2
takes two inputs
that are double scalars. In a local writable folder:
Create a MATLAB file, ep1.m
,
that contains:
function y = ep1(u) %#codegen y = u;
Create a MATLAB file, ep2.m
,
that contains:
function y = ep2(u, v) %#codegen y = u + v;
Create the Test File
In the folder that contains ep1.m
and ep2.m
,
create a MATLAB file, ep_test.m
, that calls ep1
and ep2
with
example inputs.
function [y, y1] = ep_test
y = ep1(single(2));
y1 = ep2(double(3), double(4));
Open the MATLAB Coder App
On the MATLAB Toolstrip Apps tab, under Code Generation, click the MATLAB Coder app icon.
The app opens the Select Source Files page.
Specify Source Files
On the Select Source Files page,
type or select the name of the entry-point function ep1
.
The app creates a project with the default name ep1.prj
in
the current folder.
To add ep2
to the list of entry-point
functions, click Add Entry-Point Function.
Type or select the name of the entry-point function ep2
.
Click Next to go to the Define Input Types step. The app analyzes the functions for coding issues and code generation readiness. If the app identifies issues, it opens the Review Code Generation Readiness page where you can review and fix issues. In this example, because the app does not detect issues, it opens the Define Input Types page.
Define Input Types
Because C uses static typing, at compile time, MATLAB Coder must determine the properties of all variables in the MATLAB files. You must specify the properties of all entry-point function inputs. From the properties of the entry-point function inputs, MATLAB Coder can infer the properties of all variables in the MATLAB files.
Specify a test file that MATLAB Coder can use to automatically define types:
Enter or select the test file ep_test.m
.
Click Autodefine Input Types.
The test file, eps_test.m
, calls the entry-point
functions ep1
and ep2
with
the example input types. MATLAB Coder infers that for ep1
,
input u
is single(1x1)
. For ep2
, u
and v
are double(1x1)
.
Click Next to go to the Check for Run-Time Issues step.
Check for Run-Time Issues
The Check for Run-Time Issues step generates a MEX file from your entry-point functions, runs the MEX function, and reports issues. This step is optional. However, it is a best practice to perform this step. You can detect and fix run-time errors that are harder to diagnose in the generated C code.
To open the Check for Run-Time Issues dialog
box, click the Check for Issues arrow
.
The app populates the test file field with ep_test
,
the test file that you used to define the input types.
Click Check for Issues.
The app generates a MEX function named ep1_mex
for ep1
and ep2
.
It runs the test file ep_test
replacing calls
to ep1
and ep2
with calls
to the MEX function. If the app detects issues during the MEX function
generation or execution, it provides warning and error messages. Click
these messages to navigate to the problematic code and fix the issue.
In this example, the app does not detect issues.
Click Next to go to the Generate Code step.
Generate C Code
To open the Generate dialog
box, click the Generate arrow
.
In the Generate dialog box, set Build
type to Dynamic Library
and Language to
C. Use the default values for the other project build configuration
settings.
Click Generate.
The app indicates that code generation succeeded. It displays
the source MATLAB files and generated output files on the left
side of the page. On the Variables tab, it displays
information about the MATLAB source variables. On the Target
Build Log tab, it displays the build log, including compiler
warnings and errors. By default, in the code window, the app displays
the C source code file, ep1.c
. To view a different
file, in the Source Code or Output
Files pane, click the file name.
On Microsoft® Windows® systems, MATLAB Coder generates
a C dynamic library, ep1.dll
, and supporting files,
in the default folder, codegen\dll\ep1
. It generates
the minimal set of #include
statements for header
files required by the selected code replacement library. On Linux®,
it generates a shared object (.so) file. On Mac, it generates
a dynamic library (.dylib) file. The DLL linkage conforms to the target
language, in this example, C. If you set the target language to C++,
the linkage conforms to C++. MATLAB Coder generates a standalone
C static library mcadd
in the codegen\lib\mcadd
folder.
To view the code generation report, click View Report.
Click Next to open the Finish Workflow page.
Review the Finish Workflow Page
The Finish Workflow page indicates that code generation succeeded. It provides a project summary and links to generated output.
This example shows how to generate a C dynamic
library from MATLAB code at the command line using the codegen
function.
Write two MATLAB functions, ep1
takes
one input, a single scalar, and ep2
takes two inputs,
both double scalars. In a local writable folder, create a MATLAB file, ep1.m
,
that contains:
function y = ep1(u) %#codegen y = u;
ep2.m
, that contains: function y = ep2(u, v) %#codegen y = u + v;
Generate the C dynamic library.
codegen -config:dll ep1 -args single(0) ep2 -args {0,0}
On Microsoft Windows systems, codegen
generates
a C dynamic library, ep1.dll
, and supporting files,
in the default folder, codegen/dll/ep1
. It generates
the minimal set of #include
statements for header
files required by the selected code replacement library. On Linux,
it generates a shared object (.so) file. On Mac, it generates
a dynamic library (.dylib) file. The DLL linkage conforms to the
target language, in this example, C. If you set the target language
to C++, the linkage conforms to C++.
Note: The default target language is C. To change the target language to C++, see Specify a Language for Code Generation. |