clibgen.generateLibraryDefinition
Create definition file for C++ library
Syntax
Description
The clibgen.generateLibraryDefinition function creates a
definition file with a .m file extension
used to generate a MATLAB® interface to a C++ library. Use this function to:
Select C/C++ files that define the interface. For guidance on which syntax to use, see "Files in Your Library" in Tips.
Select configurations used by the generate step.
Optionally specify configurations to use to define arguments.
Optionally specify compiler build configuration arguments.
After creating the definition file, you optionally can modify the contents to include functionality that the function cannot automatically define. For information about using a library definition file, see Define MATLAB Interface for C++ Library.
You need a C++ compiler that MATLAB supports. You must build the interface library using the same compiler that was used to build the C++ library. If your library is completely defined by source files (does not use a compiled library file), then you can choose any supported C++ compiler to build the interface library. For information about your C++ compilers, type:
mex -setup cpp
Call the build
function on the definition file to create the MATLAB interface file.
clibgen.generateLibraryDefinition(
creates a definition file defined by
InterfaceGenerationFiles,Libraries=LibraryFiles)InterfaceGenerationFiles and
LibraryFiles.
The name of the definition file
is define (since R2022b).
By default, libName.mlibName is the name of the first file
specified in InterfaceGenerationFiles. For example, if you
specify a header file named mylibrary.hpp, then the function
creates a definition file named definemylibrary.m. If you
specify more than one interface generation file, then you must use the
InterfaceName name-value argument to specify
libName.
clibgen.generateLibraryDefinition(
for a library defined by multiple header files, source files, and, if required,
compiled library files.InterfaceGenerationFiles,SupportingSourceFiles=SourceFiles)
clibgen.generateLibraryDefinition(
for a library completely defined by
InterfaceGenerationFiles)InterfaceGenerationFiles. If your library includes a
compiled library file, then you must specify a Libraries
argument.
clibgen.generateLibraryDefinition(___,
creates the file using one or more name-value arguments. Use this option with
any of the input argument combinations in the previous syntaxes.Name=Value)
Use the Configuration options to specify the output folder for the definition file or the interface namespace.
Use the C++ Library Settings to specify compiler build and link options.
Use the Definition Configurations to define all specific argument types in the library.
Examples
Input Arguments
Name-Value Arguments
Limitations
Saving
LibraryDefinitionobjectdefinelibNameinto a MAT-file is not supported.Avoid non-ASCII characters in folder and filenames, as some locale settings do not support those characters. For information about locale, see Set Locale and Display Language.
Tips
To recreate a library definition file, call
clibgen.generateLibraryDefinitionwith name-value argumentOverwriteExistingDefinitionFilesset totrue. When you use this option, the function deletes the files, including edits you might have made to the files.For troubleshooting information, see Troubleshooting C++ Library Definition Issues.
Your library might contain combinations of C/C++ header files, source files, and compiled library files. This table shows how to set the arguments to
clibgen.generateLibraryDefinitiondepending on what types of files define your library.Files in Your Library
InterfaceGenerationFiles
ArgumentName-Value Argument(s) Single CPP header file and import library file on Windows.
A.hppA.libimport library file in folderC:\Documents\MATLAB\
"A.hpp"Libraries="C:\Documents\MATLAB\A.lib"CPP header file and compiled object file on Linux.
A.hppA.soin folder~/MATLAB/
"A.hpp"Libraries="~/MATLAB/A.so"CPP header file and dynamic compiled library file on macOS.
A.hppA.dylibin folder$home/Documents/MATLAB
"A.hpp"Libraries="$home/Documents/MATLAB/A.dylib"Completely defined by CPP header and source files. No library files.
Header file
A.hppSource file
A.cpp
Example: Header and C++ Source Files
"A.hpp"SupportingSourceFiles="A.cpp"Multiple CPP header files, a source file, and a compiled library file. Create interface named
A.Header files
A.hppandB.hppSource file
A.cppCompiled library file
B.libinC:\Documents\MATLAB\
["A.hpp","B.hpp"]InterfaceName="A"a
Libraries="C:\Documents\MATLAB\B.lib"
SupportingSourceFiles="A.cpp"CPP header-only library. The library is completely defined in a header file and does not have a compiled library file.
A.hpp
Example: Header-Only HPP File
"A.hpp"Not applicable
Completely defined in a CPP source file and does not have a compiled library file.
A.cpp
"A.cpp"Not applicable
Completely defined by C header and source files. No library files.
A.hA.c
"A.h"SupportingSourceFiles="A.c"
CLinkage=trueSingle C header file and import library file on Windows.
A.hA.libinC:\Documents\MATLAB\
"A.h"Libraries="C:\Documents\MATLAB\A.lib"
CLinkage=trueThe CPP source file depends on C header and source files.
Source file
myCPP.cppHeader and source files
A.handA.cinC:\Documents\MATLAB\
"myCPP.cpp"SupportingSourceFiles="C:\Documents\MATLAB\A.c"The C library depends on a CPP source file.
Header and source files
A.handA.cinC:\Documents\MATLAB\Source file
myCPP.cpp
["C:\Documents\MATLAB\A.h", "myCPP.cpp"]InterfaceName="myCPP"
SupportingSourceFiles="C:\Documents\MATLAB\A.c"
CLinkage=trueLibrary defined by CPP and C header files with corresponding library files in
C:\Documents\MATLAB\.myCPP.hppmyCPP.libmyC.hmyC.lib
["myCPP.hpp","myC.h"]InterfaceName="myCPP"
Libraries=["C:\Documents\MATLAB\myCPP.lib","C:\Documents\MATLAB\myC.lib"]
CLinkage=trueThe C header file depends on a CPP header file.
A.hB.hppinC:\Documents\MATLAB\
"A.h"IncludePath="C:\Documents\MATLAB\"a Because you have multiple header files, you must set the
InterfaceNamename-value argument. For example, if you setInterfaceNameto"A", then when you call library functionfunctionnamefrom MATLAB, the syntax isclib.A.functionname.
Alternative Functionality
Live Task
Call the clibPublishInterfaceWorkflow function to use the Generate C++
Interface live task.
Version History
Introduced in R2019aSee Also
clibPublishInterfaceWorkflow | Generate C++
Interface | build | clibgen.buildInterface | doc