Best Practices for Using coder.ExternalDependency

Terminate Code Generation for Unsupported External Dependency

The isSupportedContext method returns true if the external code interface is supported in the build context. If the external code interface is not supported, do not return false. Instead, use error to terminate code generation with an error message. For example:

function tf = isSupportedContext(ctx)
    if  ctx.isMatlabHostTarget()
        tf = true;
        error('MyLibrary is not available for this target');

Parameterize Methods for MATLAB and Generated Code

Parameterize methods that call external functions so that the methods run in MATLAB. For example:

    % running in MATLAB, use built-in addition
    c = a + b;
    % running in generated code, call library function

Parameterize updateBuildInfo for Multiple Platforms

Parameterize the updateBuildInfo method to support multiple platforms. For example, use coder.BuildConfig.getStdLibInfo to get the platform-specific library file extensions.

    [~, linkLibExt, execLibExt, ~] = ctx.getStdLibInfo()
% Link files
linkFiles =  strcat('adder', linkLibExt);
buildInfo.addLinkObjects(linkFiles, linkPath, linkPriority, ...
    linkPrecompiled, linkLinkOnly, group);

See Also

| |

Related Examples

Was this topic helpful?