主要内容

使用MSVC工具链在64位Windows平台上构建32位DLL

注意:要在安装了Microsoft®Visual Studio的Windows®上简单地生成32位DLL,请使用CMake工具链,如Microsoft Visual Studio Project 2017 | CMake(32位Windows)。本页记录了如何编写自定义工具链来更改编译器选项和集成新的编译器。有关使用CMake构建生成代码的更多信息,请参见配置CMake构建过程

注册并使用64位Windows®平台上的Microsoft®Visual C/ c++ (MSVC)工具链编译32位动态链接库(DLL)。本例使用Microsoft®编译器。但是,这些概念和编程接口也适用于其他工具链。一旦注册了工具链,就可以从工具链列表中选择它,代码生成器生成一个makefile来使用该工具链构建代码。工具链由几个工具组成,例如具有多个不同配置选项的编译器、链接器和归档器。工具链在指定的平台上编译、链接和运行代码。若要访问本示例使用的文件,请单击打开脚本

检查平台,确定MSVC版本

这段代码检查该平台是否受支持,以及您是否拥有受支持的Microsoft®Vi万博1manbetxsual C/ c++版本。的my_msvc_32bit_tc.m工具链定义可以使用Microsoft®Visual Studio 9.0、10.0、11.0、12.0、14.0或15.0版本。

如果您使用的不是Windows®平台,或者您没有受支持的Microsoft®Visual C/ c++版本,则本示例仅生成代码和一个万博1manbetx生成文件,而不运行生成的生成文件。

VersionNumbers = {“14.0”};占位符值如果~ispc 万博1manbetxsupportedCompilerInstalled = false;其他的installed_compilerconfigurations = mex.getCompilerConfigurations(“C”“安装”);MSVC_InstalledVersions = regexp({installed_compilers. exe)名称},Microsoft Visual C\+ + 20\d\d);MSVC_InstalledVersions = cellfun(@(a)~isempty(a), MSVC_InstalledVersions);如果~any(MSVC_InstalledVersions) 万博1manbetxsupportedCompilerInstalled = false;其他的VersionNumbers = {installed_compilers(MSVC_InstalledVersions).Version}';万博1manbetxsupportedCompilerInstalled = true;结束结束

函数用于动态链接库

动态链接库的示例函数,myMatlabFunction.m,将一个数乘以2。

函数y = myMatlabFunction(u)% myMatlabFunction:返回两次输入。The MathWorks, Inc.版权所有% # codegen断言(isa (u,“双”),“输入必须是‘double’。”);断言(all([1,1] == size(u)),输入必须是标量);Y = double(u + u);

创建并配置MSVC工具链

my_msvc_32bit_tc.m工具链定义函数接受一个包含Visual Studio版本号的参数。在这个例子中,创建和配置这个工具链的命令是:

tc = my_msvc_32bit_tc(VersionNumbers{end});保存my_msvc_32bit_tctc
执行“C: \用户文档\ aghosh \ \ ExampleManager \ aghosh.Bdoc22b.j2019614 \ coder-ex19875030 \ my_msvc_32bit_tc”……执行“C: \用户文档\ aghosh \ \ ExampleManager \ aghosh.Bdoc22b.j2019614 \ coder-ex19875030 \ my_msvc_32bit_tc”。

注册工具链

代码生成器在构建过程中使用工具链之前,必须先环球套票。TargetRegistry必须包含工具链注册。这个注册可以来自任何地方rtwTargetInfo.m在MATLAB路径下的文件。, MATLAB将加载一个新的注册环球套票。TargetRegistry重置。

创建rtwTargetInfo.m文件中对应的文本文件myRtwTargetInfo.txt

RTWTARGETINFO自定义工具链的注册文件。The MathWorks, Inc. tr.registerTargetInfo(@createToolchainRegistryFor32BitMSVCToolchain);结束  % ------------------------------------------------------------------------- % 创建ToolchainInfoRegistry条目  % ------------------------------------------------------------------------- 功能配置= createToolchainRegistryFor32BitMSVCToolchain配置(1)= coder.make.ToolchainInfoRegistry;配置(1)。的名字= 'Microsoft 32 Bit Toolchain | nmake makefile (64-bit Windows)'; config(1).FileName = fullfile(fileparts(mfilename('fullpath')), 'my_msvc_32bit_tc.mat'); config(1).TargetHWDeviceType = {'Intel->x86-32 (Windows32)','AMD->x86-32 (Windows32)','Generic->Unspecified (assume 32-bit Generic)'}; config(1).Platform = {'win64'}; end
拷贝文件myRtwTargetInfo.txtrtwTargetInfo.mRTW.TargetRegistry.getInstance (“重置”);

创建代码生成配置对象

若要生成32位的动态链接库(DLL),需要创建一个“dll”代码生成配置对象。指定“dll”指示链接器(工具链中的构建工具)使用“共享库”链接器命令。

CFG = code .config(“dll”);

配置32位硬件的代码生成

要成功地生成与32位硬件兼容的代码,所生成的代码必须使用正确的底层C类型(例如,int签署了字符,以及其他)。这些类型是类型定义大小类型的语句(例如,uint8int16,以及其他)。使用命令设置配置:

cfg.HardwareImplementation.ProdHWDeviceType =...'Generic->未指定(假设32位Generic)'

配置代码生成使用32位工具链

的名称工具链属性来匹配的名字你在rtwTargetInfo.m文件。

cfg。工具链=...微软32位工具链| nmake makefile(64位Windows)

选择详细状态报告

若要提供工具链用于构建DLL的编译器标志的确认,请选择详细状态报告。

cfg。Verbose = true;

决定是否只生成代码

未安装Microsoft®编译器时,代码生成器仅生成代码和makefile。安装受支持的编译器万博1manbetx后,代码生成器将构建32位二进制文件。

如果万博1manbetxsupportedCompilerInstalled cfg。GenCodeOnly = false;其他的cfg。GenCodeOnly = true;结束

生成代码并构建一个DLL

要使用工具链生成代码并构建DLL(如果启用了构建),在命令提示符处输入:

codegen配置cfgmyMatlabFunctionarg游戏{double(1.0)}
编译myMatlabFunction函数------------------------------------------------------------------------ ### 使用工具链:微软32位工具链| nmake makefile(64位Windows) # # # ' C: \ \ aghosh \用户文档\ ExampleManager \ aghosh.Bdoc22b.j2019614 \ \ myMatlabFunction \ myMatlabFunction_rtw coder-ex19875030 \ codegen \ dll。###构建myMatlabFunction: nmake -f myMatlabFunction_rtw。可所有的  ********************************************************************** ** Visual Studio 2019开发商命令提示符v16.4.5 * *版权(c) 2019年微软公司  ********************************************************************** [ vcvarsall.bat]环境初始化:“x64_x86”微软(R)计划维护实用程序版本14.24.28316.0版权(C)微软公司。版权所有。cl -c -nologo - gs - w4 - dwin32 - d_mt - mtd - d_crt_secure_no_warnings /O2 /Oy- - dmodel =myMatlabFunction @myMatlabFunction_rtw_comp。负责myMatlabFunction_data佛”。"C:\Users\aghosh\Documents\ExampleManager\aghosh. bdoc22b .j2019614\code -ex19875030\codegen\dll\myMatlabFunction\myMatlabFunction_data.c" myMatlabFunction_data.c cl - C -nologo - gs - w4 - dwin32 - d_mt - mtd - d_crt_secure_no_warnings /O2 /Oy- - dmodel =myMatlabFunction @myMatlabFunction_rtw_comp。负责myMatlabFunction_initialize佛”。"C:\Users\aghosh\Documents\ExampleManager\aghosh. bdoc22b .j2019614\code -ex19875030\codegen\dll\myMatlabFunction\myMatlabFunction_initialize.c" myMatlabFunction_initialize.c cl - C -nologo - gs - w4 - dwin32 - d_mt - mtd - d_crt_secure_no_warnings /O2 /Oy- - dmodel =myMatlabFunction @myMatlabFunction_rtw_comp. "负责myMatlabFunction_terminate佛”。"C:\Users\aghosh\Documents\ExampleManager\aghosh. bdoc22b .j2019614\code -ex19875030\codegen\dll\myMatlabFunction\myMatlabFunction_terminate.c" myMatlabFunction_terminate.c cl - C -nologo - gs - w4 - dwin32 - d_mt - mtd - d_crt_secure_no_warnings /O2 /Oy- - dmodel =myMatlabFunction @myMatlabFunction_rtw_comp. "负责myMatlabFunction佛”。\Users\aghosh\Documents\ExampleManager\aghosh. bdoc22b .j2019614\code -ex19875030\codegen\dll\myMatlabFunction\myMatlabFunction. C " myMatlabFunction. c# ##创建动态库".\myMatlabFunction.dll"…link /MACHINE:X86 /DEBUG /DEBUGTYPE:cv /INCREMENTAL:NO /NOLOGO kernel32。自由ws2_32。lib mswsock.lib advapi32.lib -dll -def:myMatlabFunction.def -out:.\myMatlabFunction.dll @myMatlabFunction_rtw.rsp Creating library .\myMatlabFunction.lib and object .\myMatlabFunction.exp ### Created: .\myMatlabFunction.dll ### Successfully generated all binary outputs. ------------------------------------------------------------------------ Code generation successful.

构建并运行可执行文件

如果安装了受支持的编译器版本万博1manbetx,则可以使用C main函数构建32位可执行文件。您可以使用可执行文件来测试生成的代码是否按预期工作。

Cfge = code .config(exe”);cfge。CustomInclude = pwd;cfge。CustomSource =“myMatlabFunction_main.c”;cfge。GenCodeOnly = cfg.GenCodeOnly;cfge。Verbose = true;cfge。工具链=...微软32位工具链| nmake makefile(64位Windows);codegen配置cfgemyMatlabFunctionarg游戏{double(1.0)}如果万博1manbetxsupportedCompilerInstalled暂停(5);%等待EXE生成系统(“myMatlabFunction 3.1416”);%期望输出:myMatlabFunction(3.1416) = 6.2832结束
编译myMatlabFunction函数------------------------------------------------------------------------ ### 使用工具链:微软32位工具链| nmake makefile(64位Windows) # # # ' C: \ \ aghosh \用户文档\ ExampleManager \ aghosh.Bdoc22b.j2019614 \ coder-ex19875030 \ codegen \ exe \ myMatlabFunction \ myMatlabFunction_rtw。###构建myMatlabFunction: nmake -f myMatlabFunction_rtw。可所有的  ********************************************************************** ** Visual Studio 2019开发商命令提示符v16.4.5 * *版权(c) 2019年微软公司  ********************************************************************** [ vcvarsall.bat]环境初始化:“x64_x86”微软(R)计划维护实用程序版本14.24.28316.0版权(C)微软公司。版权所有。cl -c -nologo - gs - w4 - dwin32 - d_mt - mtd - d_crt_secure_no_warnings /O2 /Oy- - dmodel =myMatlabFunction @myMatlabFunction_rtw_comp。负责myMatlabFunction_main佛”。"C:\Users\aghosh\Documents\ExampleManager\aghosh. bdoc22b .j2019614\code -ex19875030\myMatlabFunction_main.c" myMatlabFunction_main.c cl - C -nologo - gs - w4 - dwin32 - d_mt - mtd - d_crt_secure_no_warnings /O2 /Oy- - dmodel =myMatlabFunction @myMatlabFunction_rtw_comp。负责myMatlabFunction_data佛”。"C:\Users\aghosh\Documents\ExampleManager\aghosh. bdoc22b .j2019614\code -ex19875030\codegen\exe\myMatlabFunction\myMatlabFunction_data.c" myMatlabFunction_data.c cl - C -nologo - gs - w4 - dwin32 - d_mt - mtd - d_crt_secure_no_warnings /O2 /Oy- - dmodel =myMatlabFunction @myMatlabFunction_rtw_comp "负责myMatlabFunction_initialize佛”。"C:\Users\aghosh\Documents\ExampleManager\aghosh. bdoc22b .j2019614\code -ex19875030\codegen\exe\myMatlabFunction\myMatlabFunction_initialize.c" myMatlabFunction_initialize.c cl - C -nologo - gs - w4 - dwin32 - d_mt - mtd - d_crt_secure_no_warnings /O2 /Oy- - dmodel =myMatlabFunction @myMatlabFunction_rtw_comp. "负责myMatlabFunction_terminate佛”。"C:\Users\aghosh\Documents\ExampleManager\aghosh. bdoc22b .j2019614\code -ex19875030\codegen\exe\myMatlabFunction\myMatlabFunction_terminate.c" myMatlabFunction_terminate.c cl - C -nologo - gs - w4 - dwin32 - d_mt - mtd - d_crt_secure_no_warnings /O2 /Oy- - dmodel =myMatlabFunction @myMatlabFunction_rtw_comp. "负责myMatlabFunction佛”。"C:\Users\aghosh\Documents\ExampleManager\aghosh. bdoc22b .j2019614\code -ex19875030\codegen\exe\myMatlabFunction\myMatlabFunction. C ###创建独立可执行文件"C:\Users\aghosh\Documents\ examlabfunction \aghosh. bdoc22b .j2019614\code -ex19875030\myMatlabFunction.exe" link /MACHINE:X86 /DEBUG /DEBUGTYPE:cv /INCREMENTAL:NO /NOLOGO kernel32.lib ws2_32.lib mswsock.lib advapi32.lib -out:C:\Users\aghosh\Documents\ExampleManager\aghosh.Bdoc22b.j2019614\coder-ex19875030\myMatlabFunction.exe @myMatlabFunction_rtw.rsp ### Created: C:\Users\aghosh\Documents\ExampleManager\aghosh.Bdoc22b.j2019614\coder-ex19875030\myMatlabFunction.exe ### Successfully generated all binary outputs. ------------------------------------------------------------------------ Code generation successful. myMatlabFunction(3.1416) = 6.2832

可选步骤:注销工具链

要取消注册工具链,输入:

删除。/ rtwTargetInfo.mRTW.TargetRegistry.getInstance (“重置”);

相关的话题