Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling 32bit matlab application on 64 bit machine (c++)

I am currently building a 32-bit MatLab-engine application in c++ on a 64 bit machine, with 64-bit MatLab installed. However, I do have all the dll's and library files in 32-bit for the MatLab engine. The library files and dll's are loaded correctly (I can compile and start the application without getting any errors I would get when I use the 64-bit dll's/libs), but the 32-bit dll's apparently launch the 64-bit matlab executable, so my program crashes as soon as I try to do something with the engine. Is there a way I can make my application launch the 32-bit matlab executable instead of the 32-bit one?

Thanks in advance!

like image 818
Tiddo Avatar asked Dec 12 '11 10:12

Tiddo


People also ask

Is MATLAB 32 or 64-bit?

Direct link to this answer If you are running MATLAB without the desktop interface, the information appears in the command window when MATLAB starts. This will return win64 for 64-bit MATLAB and win32 for 32-bit MATLAB.

Does MATLAB support 32-bit?

There are a handful of known issues with running 32-bit MATLAB on 64-bit Windows: MATLAB Parallel Server may fail when both 32-bit MATLAB and 64-bit MATLAB are installed on the same computer. Real-Time Windows Target will not run. Data Acquisition Toolbox cannot connect to parallel ports.

Is compiled MATLAB code faster?

Compiling can accelerate your code by a factor 2, with some luck. Exploiting the underlying maths and improving the MATLAB code can give you a fctor of 100. You find some examples in the forum with 200 and 1000 times faster code in pure Matlab.


1 Answers

It is possible, but it's extremely messy: the whole mbuild/deploytool system is a piece of cr*p in my opinion. First problem with deploytool.bat is that, although having a '-win32' option, that has no effect whatsoever when deploytool is not invoked from the 32bit install directory. Second problem is that the mbuild options are shared for 32 and 64 bit versions so they have to be specified manually as else the wrong compiler options are used.

Here are some things I did to compile both 32bit and 64bit from a 64bit windows machine with VS2010 installed.

  • you have to install both 32bit and 64bit matlab versions
  • you'll have to do everything from the command line
  • you can never edit your .prj files via the deploytool ui because it screws up all manual changes made to them. (well, that is actually a benefit since now at least you'll be able to store them in a VCS)
  • point to the correct compiler options by adding <param.c.cpp.options.file> to the prj under the 'configuration` section (see below)
  • build by manully giving the full path to the deploytool.bat of the 32 bit installation

options file config in prj:

<deployment-project>
  <configuration ....>
    ....
    <param.c.cpp.options.file>${MATLAB_ROOT}\bin\win32\mbuildopts\msvc100compp.bat</param.c.cpp.options.file>
    ....

Note that output dir etc will be the same for the 32bit and 64bit versions. In practice, if you have to do this for multiple projects this becomes totally unmanagable. So I have an msbuild script to make life easier: basically in the prj file I replace everything platform dependent (output dir, matlab root dir, options file location) by macros, then let msbuild copy the prj and do a regex find/replace of the macros with values depending on platform. This allows using the same prj for both platforms.

Update

After a few major changes to our projects we found that eventually the hassle of dealing with the matlab prj files was not worth it. Instead, we greatly simplified everything by invoking mcc directly and feed it with all files belonging to a project. Here is the relevant msbuild code; some error checking skipped for clarity:

<Target Name="BuildMatlabProject">
  <PropertyGroup Condition="$(MlPlatform)=='x86'">
    <MlMatlabBinDir>$(MlMatlabx86Dir)\bin\win32</MlMatlabBinDir>
  </PropertyGroup>
  <PropertyGroup Condition="$(MlPlatform)=='x64'">
    <MlMatlabBinDir>$(MlMatlabx64Dir)\bin\win64</MlMatlabBinDir>
  </PropertyGroup>
  <ItemGroup>
    <MlMFiles Include="$(MlMatlabProjDir)\*.m"/>
    <MlMResources Include="$([System.IO.Directory]::GetDirectories(&quot;$(MlMatlabSrcDir)&quot;))"/>
  </ItemGroup>
  <PropertyGroup>
    <MlMresourcseString Condition="@(MlMResources)!=''"> -a @(MlMResources, ' -a ')</MlMresourcseString>
  </PropertyGroup>
  <RemoveDir Directories="$(MlOutDir)" ContinueOnError="true"/>
  <MakeDir Directories="$(MlOutDir)"/>
  <Exec Command="$(MlMatlabBinDir)\mcc -W cpplib:$(MlOutputName)_$(MlPlatform)
 -T link:lib -d $(MlOutDir) -f $(MlMatlabBinDir)\mbuildopts\msvc100compp.bat
 -w enable:specified_file_mismatch -w enable:repeated_file -w enable:switch_ignored
 -w enable:missing_lib_sentinel -w enable:demo_license -v
 @(MlMFiles, ' ') $(MlMresourcseString)"/>
</Target>

It needs these properties:

  • MlPlatform: x86 to build 32 bit, x64 to build 64 bit
  • MlMatlabx86Dir: path to matlab 32bit install dir
  • MlMatlabx64Dir: path to matlab 64bit install dir
  • MlMatlabProjDir: path to 'project' dir with m-files to compile
  • MlMatlabSrcDir: path with extra source m-files
  • MlOutDir: output directory
  • MlOutputName: output name
like image 193
stijn Avatar answered Oct 06 '22 04:10

stijn