coder.BuildConfig - Build context during code generation - MATLAB (original) (raw)

Main Content

Namespace: coder

Build context during code generation

Description

The code generator creates an object of this class to facilitate access to the_build context_. The build context encapsulates the settings used by the code generator including:

Use coder.BuildConfig methods in the methods that you write for the coder.ExternalDependency class.

Creation

The code generator creates objects of this class.

Methods

Examples

collapse all

Use coder.BuildConfig methods to access the build context in coder.ExternalDependency methods

This example shows how to use coder.BuildConfig methods to access the build context in coder.ExternalDependency methods. In this example, you use:

Write a class definition file for an external library that contains the functionadder.

%================================================================ % 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. %================================================================

classdef AdderAPI < coder.ExternalDependency %#codegen

methods (Static)
    
    function bName = getDescriptiveName(~)
        bName = 'AdderAPI';
    end
    
    function tf = isSupportedContext(buildContext)
        if  buildContext.isMatlabHostTarget()
            tf = true;
        else
            error('adder library not available for this target');
        end
    end
    
    function updateBuildInfo(buildInfo, buildContext)
        % Get file extensions for the current platform
        [~, linkLibExt, execLibExt, ~] = buildContext.getStdLibInfo();
        
        % Add file paths
        hdrFilePath = fullfile(pwd, 'codegen', 'dll', 'adder');
        buildInfo.addIncludePaths(hdrFilePath);

        % Link files
        linkFiles = strcat('adder', linkLibExt);
        linkPath = hdrFilePath;
        linkPriority = '';
        linkPrecompiled = true;
        linkLinkOnly = true;
        group = '';
        buildInfo.addLinkObjects(linkFiles, linkPath, ...
            linkPriority, linkPrecompiled, linkLinkOnly, group);

        % Non-build files for packaging
        nbFiles = 'adder';
        nbFiles = strcat(nbFiles, execLibExt);
        buildInfo.addNonBuildFiles(nbFiles,'','');
    end
    
    %API for library function 'adder'
    function c = adder(a, b)
        if coder.target('MATLAB')
            % running in MATLAB, use built-in addition
            c = a + b;
        else
            % Add the required include statements to the generated function code
            coder.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');
        end
    end
end

end

Extended Capabilities

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2013b