I am recently trying to maintain a few mex files in my Run-on-Stellaris-Launchpad Support Package project. The targeted users may use 32 or 64 bit of MATLAB, which means I need to compile the mex files with both architectures. I initially tried to find an easy way to do this kind of "cross-platform" build strictly inside my 64-bit MATLAB by playing with parameters or command line switches but found it is harder than I thought. After reading a few post online, I figured that there is no short-cut solution for this problem. The architecture switch in mex command simply does not work as intended. Pieces of code here and there from Mathworks are not that friendly to the idea of cross-platform compiling. Some library files for building a 32-bit mex file do not exist in 64-bit installation and have to be copied from a 32-bit version. With a bunch of patience, I finally gathered all pieces that solves this problem and now I can just specify the architecture when calling mex, and the right outcome is expected.
Two formats of the same solution are provided: Manual Patch offers more detail and Automatic Patch solves the problem faster which is ideal for people working for deadlines.
Here I am trying to show all details about what I have done to make it happen, assuming the installed MATLAB version is R2013a 64-bit and the goal is to build 32-bit mex file. If you are doing the opposite, carefully adapt the following steps by switching 32 and 64 bit.
1. Obtain a copy of 32-bit installation (we need 32-bit lib files and some other files). Copy <matlab_32bit>/bin/win32/mexopts and <matlab_32bit>/extern/lib/win32 folder to the same relative location under the 64-bit install.
2. Edit "mex_helper.m" so the architecture flag being passed in will not be ignored. Detailed steps is listed.
a) Insert the following lines at around line 50, just before the try statement.
pc_arch = computer('arch');
if ~isempty(intersect(varargin,'-win64'))
pc_arch = 'win64';
elseif ~isempty(intersect(varargin,'-win32'))
pc_arch = 'win32';
end
b) Replace "errCode = runCmdInShell(cmdtool, cmdargs)" with
"errCode = runCmdInShell(cmdtool, cmdargs, pc_arch)"
c) Change "function errCode = runCmdInShell(cmdtool, cmdargs)" to
"function errCode = runCmdInShell(cmdtool, cmdargs, arch)"
d) Modify the line "errCode = dos([ cmd ' -' computer('arch')])" to be
"errCode = dos([ cmd ' -' arch ])".
3. Edit "mex.pl" file under <matlabroot>/bin.
a) Find init_mex function by searching "sub init_mex" and replace the line
$OPTFILE_LONGNAME = "mexopts.bat";
with
$OPTFILE_LONGNAME = "mexopts_$ARCH.bat";
b) Find init_mbuild function by searching "sub init_mbuild" and replace the line
$OPTFILE_LONGNAME = "compopts.bat";
with
$OPTFILE_LONGNAME = "compopts_$ARCH.bat";
4. Edit "mexsetup.pm" under <matlabroot>/bin.
a) Insert a line "use File::Copy;" after "use File::Basename;" at line 129,
b) Add a line at around Line 292, after "my $presetmode = $destOptsFile;"
my $destOptsFileDefault = "";
c) Modify code at around line 333 and make it looks like the following
if ($tool_name eq "mex") {
$destOptsFile="$destOptsDir\\mexopts_$ENV{'ARCH'}.bat";
$destOptsFileDefault="$destOptsDir\\mexopts.bat";
} else {
$destOptsFile="$destOptsDir\\compopts_$ENV{'ARCH'}.bat";
$destOptsFileDefault="$destOptsDir\\compopts.bat";
}
d) Add the following lines at the end of install_options_files, just after "close(OPTIONSFILE);"
printf("\n\nDo you want to set [$ENV{'ARCH'}] as your default architecture? ");
if ($destOptsFileDefault ne "" && print_and_ask_yes_or_no(1) eq 1)
{
copy("$destOptsFile","$destOptsFileDefault")
}
5. Now you can setup your 32-bit and 64-bit compiler by run the following commands. If you have not installed the proper compiler by this time, you can always do this step again. Remember to answer yes to the default architecture question if it is the same as your MATLAB architecture.
a) mex -setup -win32
b) mex -setup -win64
6. You are good to go. Try the command "mex your_mex_src.c -win32" and "mex your_mex_src.c -win64" to get mex function that works for 32-bit MATLAB and 64-bit MATLAB.
Please extract the desired patch at the MATLAB main directory without the specific release version, e.g. "C:\Program Files\MATLAB". To make it safe, making a backup of the following files before applying patch is encouraged.
<matlabroot>/bin/mex.pl
<matlabroot>/bin/mexsetup.pm
<matlabroot>/toolbox/matlab/general/mex_helper.m
Patches for:
MATLAB R2013a 64-bit, to enable ability to cross-compile 32-bit mex files Download
MATLAB R2013a 32-bit, to enable ability to cross-compile 64-bit mex files Download