在64位matlab上編譯32位的動態鏈接庫DLL文件

參考資料
https://www.mathworks.com/help/coder/ug/build-32-bit-dll-on-64-bit-windows®-platform-using-msvc-toolchain.html

完整代碼
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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章