在64位matlab上编译32位的动态链接库DLL文件
完整代码
https://download.csdn.net/download/rmrgjxeivt/12277467
具体方法见代码
%% Check Platform and Determine MSVC Version
% This code checks that the platform is supported and that you have a
% supported version of Microsoft? Visual C/C++. The my_msvc_32bit_tc.m
% toolchain definition can use the Microsoft? Visual Studio versions 9.0,
% 10.0, 11.0, 12.0, 14.0, or 15.0. If you are not using a Windows?
% platform, or if you do not have a supported version of Microsoft? Visual
% C/C++, the example generates only code and a makefile, without running
% the generated makefile.
VersionNumbers = {'14.0'}; % Placeholder value
if ~ispc
supportedCompilerInstalled = false;
else
installed_compilers = mex.getCompilerConfigurations('C', 'Installed');
MSVC_InstalledVersions = regexp({installed_compilers.Name}, 'Microsoft Visual C\+\+ 20\d\d');
MSVC_InstalledVersions = cellfun(@(a)~isempty(a), MSVC_InstalledVersions);
if ~any(MSVC_InstalledVersions)
supportedCompilerInstalled = false;
else
VersionNumbers = {installed_compilers(MSVC_InstalledVersions).Version}';
supportedCompilerInstalled = true;
end
end
%% Create and Configure an MSVC Toolchain
tc = my_msvc_32bit_tc(VersionNumbers{end});
save my_msvc_32bit_tc tc;
%% Register the Toolchain
copyfile myRtwTargetInfo.txt rtwTargetInfo.m
RTW.TargetRegistry.getInstance('reset');
%% Create Code Generation Configuration Object
cfg = coder.config('dll');
%% Configure Code Generation for 32-bit Hardware
cfg.HardwareImplementation.ProdHWDeviceType = ...
'Generic->Unspecified (assume 32-bit Generic)';
%% Configure Code Generation to Use the 32-bit Toolchain
cfg.Toolchain = ...
'Microsoft 32 Bit Toolchain | nmake makefile (64-bit Windows)';
%% Select Verbose Status Reporting
cfg.Verbose = true;
%% Determine Whether to Generate Code Only
if supportedCompilerInstalled
cfg.GenCodeOnly = false;
else
cfg.GenCodeOnly = true;
end
%% Generate Code and Build a DLL
% 在这里修改输入输出定义,函数名等参数
codegen -config cfg myMatlabFunction -args { double(1.0) };
%% Optional Step: Unregister the toolchain
% delete ./rtwTargetInfo.m
% RTW.TargetRegistry.getInstance('reset');
%% Build and Run an Executable
% cfge = coder.config('exe');
% cfge.CustomInclude = pwd;
% cfge.CustomSource = 'myMatlabFunction_main.c';
% cfge.GenCodeOnly = cfg.GenCodeOnly;
% cfge.Verbose = true;
% cfge.Toolchain = ...
% 'Microsoft 32 Bit Toolchain | nmake makefile (64-bit Windows)';
% codegen -config cfge myMatlabFunction -args { double(1.0) };
% if supportedCompilerInstalled
% pause(5); %wait for EXE to get generated
% system('myMatlabFunction 3.1416'); % Expected output: myMatlabFunction(3.1416) = 6.2832
% end