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

15,823

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
Share:
15,823
Tiddo
Author by

Tiddo

Updated on June 27, 2022

Comments

  • Tiddo
    Tiddo almost 2 years

    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!

  • Tiddo
    Tiddo over 12 years
    Wow... Thanks for your answer. I think I'm just going to uninstall MatLab 64-bit and install 32-bit. I don't really need to export to 64-bit, I was just hoping there was an easy way to compile to 32-bit without having to install 32-bit MatLab. By the way, I'm not using the deploytool manually at all. I just let vs2010 compile everything for me.
  • Tiddo
    Tiddo over 12 years
    I just read your answer a little better, but I still don't understand why my program doesn't work: I don't use any buildtools from matlab, I just include and link to the lib's and dll's, so my program can use the engine. As far as I can understand, the deploytool is simply just a tool which compiles a program with the correct settings for MEX files. However, I don't build a MEX file, and I manually configure the settings I need. But than it has to be possible in some way to set the settings in such a way that I CAN export to 32 bit without having to install a 32-bit version right?
  • stijn
    stijn over 12 years
    @Tiddo what libs do you use? In my 64 bit installation, there are only 64bit libs under extern/lib/win64 so they cannot possibly be used to build a 32bit version
  • Tiddo
    Tiddo over 12 years
    No, but at my university we have some computers with a 32-bit installation (that's why it needs to build for 32-bit) , so I copied it from there. Without those libs the program won't compile at all. And with the 64-bit DLL's it will compile, but the program will fail to start the engine. With the 32-bit dll's and lib's it wil start, but as I said in my first post it will fail as soon as I do something with it.
  • stijn
    stijn over 12 years
    in that case: are you sure all dlls the runtime uses are in your path? not just mclmcrt, but under the bin/win32 directory there are like 50 dlls, and all of them are different for 32 bit, and nearly all of them are used by the runtime engine
  • Tiddo
    Tiddo over 12 years
    All of them are in the path. Moreover, I even tried to delete the 64 bin dir from the path, but still the same problem. I suspect that the dll's look somewhere in the registry to launch the matlab exe, but I don't know how to change this
  • stijn
    stijn over 12 years
    I think you should at least install the 32bit Matlab runtime (not full matlab) to be able to run applications on it..
  • Tiddo
    Tiddo over 12 years
    I guess you're right... I also just noticed that the 32-bit exe displays an error message about the JVM when I run it separately, and the 64-bit exe doesn't. So I think it's just too deeply integrated in the system to be able to work around this problem.