Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

向 MATLAB® Coder™ 编译过程添加自定义工具链

此示例说明如何注册和使用工具链来编译可执行文件。此示例使用 Intel® 编译器,但下面显示的概念和 API 可用于任何工具链。已注册的工具链可以从工具链列表中选择,并且将生成联编文件来使用该工具链编译代码。

关于 coderrand 函数

在此示例中,您将为 coderrand 函数生成代码。此 MATLAB® 函数只是在开区间 (0,1) 上基于标准均匀分布生成一个随机标量值。

type coderrand
function y = coderrand %#codegen

%   Copyright 2012 The MathWorks, Inc.

y = rand();

工具链信息

工具链是为指定的平台编译和链接生成的代码所需的工具集合。一个工具链包含多个工具,如编译器、链接器和存档器。这些工具中的每个工具都可以有多个选项,这些选项可以组合为各种配置,例如快速编译、快速运行、调试。工具链对象描述工具链的基本信息。工具链对象有描述上述所有内容的方法。该对象可以保存到 MATLAB 文件中,并在不同安装之间共享。

此示例使用工具链定义文件 intel_tc.m

tc = intel_tc
tc = 
###########################################################################
# Toolchain Name: Intel v14 | nmake makefile (64-bit Windows)
# Supported Toolchain Version: 14
# Toolchain Specification Format Version: 2023b
# Toolchain Specification Revision: 1.0
###########################################################################

# Supported Host Platform = win64
# Supported Languages = C/C++

# ---------------
# Setup/Cleanup
# ---------------
MATLAB Setup: (none)
MATLAB Cleanup: (none)
Shell Setup: 
	call %ICPP_COMPILER14%\bin\compilervars.bat intel64
Shell Cleanup: (none)

# ------------
# Attributes
# ------------
RequiresBatchFile        = true
RequiresCommandFile      = true
TransformPathsWithSpaces = true


# ----------------------------------------------------------------------
# Macros intrinsic to the toolchain or assumed to be defined elsewhere
# ----------------------------------------------------------------------
# ldebug
# conflags
# cflags

# --------
# MACROS
# --------
MW_EXTERNLIB_DIR    = $(MATLAB_ROOT)\extern\lib\win64\microsoft
MW_LIB_DIR          = $(MATLAB_ROOT)\lib\win64
CFLAGS_ADDITIONAL   = -D_CRT_SECURE_NO_WARNINGS
CPPFLAGS_ADDITIONAL = -EHs -D_CRT_SECURE_NO_WARNINGS
LIBS_TOOLCHAIN      = $(conlibs)
CVARSFLAG           = 

###########################################################################
# Build Tool: Intel C Compiler
###########################################################################

Language              : 'C'
OptionsRegistry       : {'C Compiler', 'CFLAGS'}
InputFileExtensions   : {Source}
OutputFileExtensions  : {Object}
DerivedFileExtensions : {}
SupportedOutputs      : {*}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<| |>OUTPUT_FLAG<||>OUTPUT<|'

# ---------
# Command
# ---------
CC = icl
CC_PATH =

# ------------
# Directives
# ------------
CompileFlag        = 
Debug              = -Zi
ErrorPattern       = 
FileNamePattern    = 
FileSeparator      = \
Include            = 
IncludeSearchPath  = -I
LineNumberPattern  = 
OutputFlag         = -Fo
PreprocessFile     = 
PreprocessorDefine = -D
WarningPattern     = 

# -----------------
# File Extensions
# -----------------
Header = .h
Object = .obj
Source = .c

###########################################################################
# Build Tool: Intel C/C++ Linker
###########################################################################

Language              : 'C'
OptionsRegistry       : {'Linker', 'LDFLAGS', 'Shared Library Linker', 'SHAREDLIB_LDFLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {'Executable', 'Shared Library'}
DerivedFileExtensions : {}
SupportedOutputs      : {coder.make.enum.BuildOutput.EXECUTABLE, coder.make.enum.BuildOutput.SHARED_LIBRARY}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<| |>OUTPUT_FLAG<||>OUTPUT<|'

# ---------
# Command
# ---------
LD = xilink
LD_PATH =

# ------------
# Directives
# ------------
Debug                    = 
FileSeparator            = \
Library                  = -L
LibrarySearchPath        = -I
LibrarySearchPathRuntime = 
OutputFlag               = -out:

# -----------------
# File Extensions
# -----------------
Executable     = .exe
Shared Library = .dll

###########################################################################
# Build Tool: Intel C++ Compiler
###########################################################################

Language              : 'C++'
OptionsRegistry       : {'C++ Compiler', 'CPPFLAGS'}
InputFileExtensions   : {Source}
OutputFileExtensions  : {Object}
DerivedFileExtensions : {}
SupportedOutputs      : {*}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<| |>OUTPUT_FLAG<||>OUTPUT<|'

# ---------
# Command
# ---------
CPP = icl
CPP_PATH =

# ------------
# Directives
# ------------
CompileFlag        = 
Debug              = -Zi
ErrorPattern       = 
FileNamePattern    = 
FileSeparator      = \
Include            = 
IncludeSearchPath  = -I
LineNumberPattern  = 
OutputFlag         = -Fo
PreprocessFile     = 
PreprocessorDefine = -D
WarningPattern     = 

# -----------------
# File Extensions
# -----------------
Header = .hpp
Object = .obj
Source = .cpp

###########################################################################
# Build Tool: Intel C/C++ Linker
###########################################################################

Language              : 'C++'
OptionsRegistry       : {'C++ Linker', 'CPP_LDFLAGS', 'C++ Shared Library Linker', 'CPP_SHAREDLIB_LDFLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {'Executable', 'Shared Library'}
DerivedFileExtensions : {}
SupportedOutputs      : {coder.make.enum.BuildOutput.EXECUTABLE, coder.make.enum.BuildOutput.SHARED_LIBRARY}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<| |>OUTPUT_FLAG<||>OUTPUT<|'

# ---------
# Command
# ---------
CPP_LD = xilink
CPP_LD_PATH =

# ------------
# Directives
# ------------
Debug                    = 
FileSeparator            = \
Library                  = -L
LibrarySearchPath        = -I
LibrarySearchPathRuntime = 
OutputFlag               = -out:

# -----------------
# File Extensions
# -----------------
Executable     = .exe
Shared Library = .dll

###########################################################################
# Build Tool: Intel C/C++ Archiver
###########################################################################

Language              : 'C'
OptionsRegistry       : {'Archiver', 'ARFLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {Static Library}
DerivedFileExtensions : {}
SupportedOutputs      : {coder.make.enum.BuildOutput.STATIC_LIBRARY}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<| |>OUTPUT_FLAG<||>OUTPUT<|'

# ---------
# Command
# ---------
AR = xilib
AR_PATH =

# ------------
# Directives
# ------------
Debug             = 
FileSeparator     = \
LibrarySearchPath = 
OutputFlag        = -out:

# -----------------
# File Extensions
# -----------------
Static Library = .lib

###########################################################################
# Build Tool: Download
###########################################################################

Language              : ''
OptionsRegistry       : {'Download', 'DOWNLOAD_FLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {}
DerivedFileExtensions : {}
SupportedOutputs      : {coder.make.enum.BuildOutput.EXECUTABLE}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<|'

# ---------
# Command
# ---------
DOWNLOAD =
DOWNLOAD_PATH =

# ------------
# Directives
# ------------
(none)

# -----------------
# File Extensions
# -----------------
(none)

###########################################################################
# Build Tool: Execute
###########################################################################

Language              : ''
OptionsRegistry       : {'Execute', 'EXECUTE_FLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {}
DerivedFileExtensions : {}
SupportedOutputs      : {coder.make.enum.BuildOutput.EXECUTABLE}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<|'

# ---------
# Command
# ---------
EXECUTE = $(PRODUCT)
EXECUTE_PATH =

# ------------
# Directives
# ------------
(none)

# -----------------
# File Extensions
# -----------------
(none)

###########################################################################
# Build Tool: NMAKE Utility
###########################################################################

Language              : ''
OptionsRegistry       : {'Make Tool', 'MAKE_FLAGS'}
InputFileExtensions   : {}
OutputFileExtensions  : {}
DerivedFileExtensions : {}
SupportedOutputs      : {*}
CommandPattern        : '|>TOOL<| |>TOOL_OPTIONS<|'

# ---------
# Command
# ---------
MAKE = nmake
MAKE_PATH =

# ------------
# Directives
# ------------
Comment                = #
DeleteCommand          = @del
DisplayCommand         = @echo
FileSeparator          = \
ImpliedFirstDependency = $<
ImpliedTarget          = $@
IncludeFile            = !include
LineContinuation       = \
MoveCommand            = @ren
ReferencePattern       = \$\($1\)
RunScriptCommand       = @cmd /C

# -----------------
# File Extensions
# -----------------
Makefile = .mk

###########################################################################
# Build Configuration : Faster Runs
# Description         : Minimize run time
###########################################################################

ARFLAGS              = /nologo 
CFLAGS               = $(cflags) $(CVARSFLAG) $(CFLAGS_ADDITIONAL) /c /O2 
CPPFLAGS             = $(cflags) $(CVARSFLAG) $(CPPFLAGS_ADDITIONAL) /c /O2 
CPP_LDFLAGS          = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) 
CPP_SHAREDLIB_LDFLAGS =
DOWNLOAD_FLAGS       =
EXECUTE_FLAGS        =
LDFLAGS              = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) 
MEX_CPPFLAGS         =
MEX_CPPLDFLAGS       =
MEX_CFLAGS           =
MEX_LDFLAGS          =
MAKE_FLAGS           = -f $(MAKEFILE) 
SHAREDLIB_LDFLAGS    = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) -dll -def:$(DEF_FILE) 

###########################################################################
# Build Configuration : Faster Builds
# Description         : Minimize compilation and linking time
###########################################################################

ARFLAGS              = /nologo 
CFLAGS               = $(cflags) $(CVARSFLAG) $(CFLAGS_ADDITIONAL) /c /Od 
CPPFLAGS             = $(cflags) $(CVARSFLAG) $(CPPFLAGS_ADDITIONAL) /c /Od 
CPP_LDFLAGS          = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) 
CPP_SHAREDLIB_LDFLAGS =
DOWNLOAD_FLAGS       =
EXECUTE_FLAGS        =
LDFLAGS              = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) 
MEX_CPPFLAGS         =
MEX_CPPLDFLAGS       =
MEX_CFLAGS           =
MEX_LDFLAGS          =
MAKE_FLAGS           = -f $(MAKEFILE) 
SHAREDLIB_LDFLAGS    = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) -dll -def:$(DEF_FILE) 

###########################################################################
# Build Configuration : Debug
# Description         : Build with debug information
###########################################################################

ARFLAGS              = /nologo $(ARDEBUG) 
CFLAGS               = $(cflags) $(CVARSFLAG) $(CFLAGS_ADDITIONAL) /c /Od $(CDEBUG) 
CPPFLAGS             = $(cflags) $(CVARSFLAG) $(CPPFLAGS_ADDITIONAL) /c /Od $(CPPDEBUG) 
CPP_LDFLAGS          = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) $(CPPLDDEBUG) 
CPP_SHAREDLIB_LDFLAGS =
DOWNLOAD_FLAGS       =
EXECUTE_FLAGS        =
LDFLAGS              = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) $(LDDEBUG) 
MEX_CPPFLAGS         =
MEX_CPPLDFLAGS       =
MEX_CFLAGS           =
MEX_LDFLAGS          =
MAKE_FLAGS           = -f $(MAKEFILE) 
SHAREDLIB_LDFLAGS    = $(ldebug) $(conflags) $(LIBS_TOOLCHAIN) -dll -def:$(DEF_FILE) $(LDDEBUG) 



save intel_tc tc

注册工具链

工具链通过 RTW.TargetRegistry 注册。要注册工具链,您也可以使用系统自动加载的 rtwTargetInfo

copyfile myRtwTargetInfoCustom.txt rtwTargetInfo.m
type rtwTargetInfo
function rtwTargetInfo(tr)
%RTWTARGETINFO Registration file for custom toolchains.

% Copyright 2012-2016 The MathWorks, Inc.

tr.registerTargetInfo(@loc_createToolchain);

end

% -------------------------------------------------------------------------
% Create the ToolchainInfoRegistry entries
% -------------------------------------------------------------------------
function config = loc_createToolchain

config(1)                       = coder.make.ToolchainInfoRegistry;
config(1).Name                  = 'Intel v14 | nmake makefile (64-bit Windows)';
config(1).FileName              = fullfile(fileparts(mfilename('fullpath')), 'intel_tc.mat');
config(1).TargetHWDeviceType    = {'*'};
config(1).Platform              =  {computer('arch')};

end

现在,您可以重置 TargetRegistry 以选取新的 rtwTargetInfo

RTW.TargetRegistry.getInstance('reset');

选择工具链

现在,您可以创建 config 对象,该对象配置为使用新工具链创建可执行文件。

cfg = coder.config('exe');
cfg.CustomSource = 'coderrand_main.c';
cfg.CustomInclude = pwd;
cfg.Toolchain = 'Intel v14';

如果您没有安装 Intel 编译器,您可以使用以下命令只生成代码和联编文件。

cfg.GenCodeOnly = true;

运行 codegen 以生成使用新工具链的代码和联编文件。

codegen -config cfg coderrand 
Code generation successful.

一旦 codegen 完成,并且您安装了 Intel 编译器,就可以使用 system('coderrand.exe') 来运行可执行文件。

清理

您可以重置 TargetRegistry 以删除您在上面注册的工具链。

delete ./rtwTargetInfo.m
RTW.TargetRegistry.getInstance('reset');