Main Content

coder.BuildConfig 类

命名空间: coder

代码生成期间的编译上下文

描述

代码生成器会创建一个此类的对象,以便于访问编译上下文。编译上下文封装代码生成器使用的设置,包括:

  • 目标语言

  • 代码生成目标

  • 目标硬件

  • 编译工具链

在为 coder.ExternalDependency 类编写的方法中使用 coder.BuildConfig 方法。

类属性

抽象
true

有关类属性的信息,请参阅类属性

创建对象

代码生成器创建此类的对象。

方法

全部展开

示例

全部折叠

此示例说明如何使用 coder.BuildConfig 方法来访问 coder.ExternalDependency 方法中的编译上下文。在此示例中,您使用:

  • coder.BuildConfig.isMatlabHostTarget 来验证代码生成目标是 MATLAB® 主机。如果主机不是 MATLAB,则报告错误。

  • coder.BuildConfig.getStdLibInfo 来获取链接时和运行时库文件扩展名。使用此信息来更新编译信息。

为包含函数 adder 的外部库编写一个类定义文件。

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

版本历史记录

在 R2013b 中推出