Documentation

coder.ExternalDependency class

Package:coder
Superclasses:

Interface to external code

Description

coder.ExternalDependencyis an abstract class for developing an interface between external code and MATLAB®code intended for code generation. You can define classes that derive fromcoder.ExternalDependencyto encapsulate the interface to external libraries, object files, and C/C++ source code. This encapsulation allows you to separate the details of the interface from your MATLAB code.

To define a class derived fromcoder.ExternalDependency, create a subclass. For example:

classdefmyClass < coder.ExternalDependency

You must define all of the methods listed inMethods. These methods are static and are not compiled. The code generator invokes these methods in MATLAB after code generation is complete to configure the build for the generated code. TheRTW.BuildInfoandcoder.BuildConfigobjects that describe the build information and build context are automatically created during the build process. TheupdateBuildInfomethod provides access to these objects. For more information on build information customization, seeCustomize the Post-Code-Generation Build Process(MATLAB Coder).

You also define methods that call the external code. These methods are compiled. For each external function that you want to call, write a method to define the programming interface to the function. In the method, usecoder.cevalto call the external function.

Methods

getDescriptiveName Return descriptive name for external dependency
isSupportedContext Determine if build context supports external dependency
updateBuildInfo Update build information

Examples

collapse all

This example shows how to encapsulate the interface to an external C dynamic linked library usingcoder.ExternalDependency.

Write a functionadderthat returns the sum of its inputs.

functionc = adder(a,b)%#codegenc = a + b;end

Generate a library that containsadder.

codegen ('adder','-args',{-2,5},'-config:dll','-report')

Write the class definition fileAdderAPI.mto encapsulate the library interface.

%================================================================% This class abstracts the API to an external Adder library.% It implements static methods for updating the build information% at compile time and build time.%================================================================classdefAdderAPI < coder.ExternalDependency%#codegen方法(Static)functionbName = getDescriptiveName(~) bName ='AdderAPI';endfunctiontf = isSupportedContext(buildContext)ifbuildContext.isMatlabHostTarget() tf = true;elseerror('adder library not available for this target');endendfunctionupdateBuildInfo(buildInfo, buildContext)% Get file extensions for the current platform[~, linkLibExt, execLibExt, ~] = buildContext.getStdLibInfo();% Add file pathshdrFilePath = fullfile(pwd,'codegen','dll','adder'); buildInfo.addIncludePaths(hdrFilePath);% Link fileslinkFiles = strcat('adder', linkLibExt); linkPath = hdrFilePath; linkPriority =''; linkPrecompiled = true; linkLinkOnly = true; group =''; buildInfo.addLinkObjects(linkFiles, linkPath,...linkPriority, linkPrecompiled, linkLinkOnly, group);% Non-build files for packagingnbFiles ='adder'; nbFiles = strcat(nbFiles, execLibExt); buildInfo.addNonBuildFiles(nbFiles,'','');end%API for library function 'adder'functionc = adder(a, b)ifcoder.target('MATLAB')% running in MATLAB, use built-in additionc = a + b;else% Add the required include statements to the generated function codecoder.cinclude('adder.h'); coder.cinclude('adder_initialize.h'); coder.cinclude('adder_terminate.h'); c = 0;% Because MATLAB Coder generated adder, use the% housekeeping functions before and after calling% adder with coder.ceval.coder.ceval('adder_initialize'); c = coder.ceval('adder', a, b); coder.ceval('adder_terminate');endendendend

Write a functionadder_mainthat calls the external library functionadder.

functiony = adder_main (x1, x2)%#codegeny = AdderAPI.adder(x1, x2);end

Generate a MEX function foradder_main. The MEX Function exercises thecoder.ExternalDependency方法.

codegen ('adder_main','-args', {7,9},'-report')

Copy the library to the current folder using the file extension for your platform.

For Windows®使用:

copyfile(fullfile(pwd,'codegen','dll','adder','adder.dll'));

For Linux®使用:

copyfile(fullfile(pwd,'codegen','dll','adder','adder.so'));

Run the MEX function and verify the result.

adder_main_mex(2,3)

Introduced in R2013b

Was this topic helpful?