Skip to content

Instantly share code, notes, and snippets.

@gyk
Created October 7, 2014 17:07
Show Gist options
  • Save gyk/0a03b0a56b3b646f54cf to your computer and use it in GitHub Desktop.
Save gyk/0a03b0a56b3b646f54cf to your computer and use it in GitHub Desktop.
Compile mexw64 by VC++ 2012 & Matlab 2010b on Windows 8

Compile .mexw64 by cl.exe from VS 2012 Express for Desktop & Matlab 2010b on 64-bit Windows 8

Here is the note for how I configured Matlab to build mex C/C++ extensions on 64-bit Windows 8:
At first, mex failed to find a compiler:

>> mex -setup
Please choose your compiler for building external interface (MEX) files: 
 
Would you like mex to locate installed compilers [y]/n? y
 
Select a compiler: 
 
[0] None 

And there existed no mexopts.bat in %USERPROFILE%\AppData\Roaming\MathWorks\MATLAB\R2010b.
According to some posts (this one for example) on Matlab Answers, I had to install Microsoft SDK 7.1.
Surprisingly, when I checked the path it seemed SDK 7.1 had already been installed:

C:\Program Files (x86)\Microsoft SDKs\Windows>dir /B
v7.0A
v7.1A
v8.0
v8.0A
v8.1
v8.1A

[Solution]
Take the following steps:

  1. Manually copy %MATLAB%\R2010b\bin\win64\mexopts\msvc100opts.bat to %USERPROFILE%\AppData\Roaming\MathWorks\MATLAB\R2010b\mexopts.bat;

  2. Since the long path with space C:\Program Files\... is annoying, make a new folder named D:\!\Path and add it to the %PATH% environment variable;

  3. Now create a soft link in command window (admin):

     mklink /D VC11 "D:\Program Files (x86)\Microsoft Visual Studio 11.0"
    
  4. Edit mexopts.bat -- add/update the following 3 lines:

     set VSINSTALLDIR=D:\!\Path\VC11
     set LINKERDIR='.registry_lookup("SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1A" , "InstallationFolder").'
     set PATH=%VSINSTALLDIR%\VC\bin\x86_amd64;%PATH%;
    
  5. Run mex -v somecode.cpp and somecode.mexw64 will be built.

Attached is the mexopts.bat file. Just copy it to the directory and it might work.

@echo off
REM Put it into `%USERPROFILE%\AppData\Roaming\MathWorks\MATLAB\R2010b`.
REM It is based on msvc100opts.bat, but please ignore "VC2010" or whatever
REM in the comments below. My compiler is VC++ 2012 actually.
rem MSVC100OPTS.BAT
rem
rem Compile and link options used for building MEX-files
rem using the Microsoft Visual C++ compiler version 10.0
rem
rem $Revision: 1.1.6.1.4.1 $ $Date: 2010/07/14 18:43:32 $
rem Copyright 2007-2009 The MathWorks, Inc.
rem
rem StorageVersion: 1.0
rem C++keyFileName: MSVC100OPTS.BAT
rem C++keyName: Microsoft Visual C++ 2010
rem C++keyManufacturer: Microsoft
rem C++keyVersion: 10.0
rem C++keyLanguage: C++
rem
rem ********************************************************************
rem General parameters
rem ********************************************************************
set MATLAB=%MATLAB%
set VSINSTALLDIR=D:\!\Path\VC11
set VCINSTALLDIR=%VSINSTALLDIR%\VC
rem In this case, LINKERDIR is being used to specify the location of the SDK
set LINKERDIR='.registry_lookup("SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1A" , "InstallationFolder").'
set PATH=%VCINSTALLDIR%\bin\amd64;%VCINSTALLDIR%\bin;%VCINSTALLDIR%\VCPackages;%VSINSTALLDIR%\Common7\IDE;%VSINSTALLDIR%\Common7\Tools;%LINKERDIR%\bin\x64;%LINKERDIR%\bin;%MATLAB_BIN%;%PATH%
set PATH=%VSINSTALLDIR%\VC\bin\x86_amd64;%PATH%;
set INCLUDE=%VCINSTALLDIR%\INCLUDE;%VCINSTALLDIR%\ATLMFC\INCLUDE;%LINKERDIR%\include;%INCLUDE%
set LIB=%VCINSTALLDIR%\LIB\amd64;%VCINSTALLDIR%\ATLMFC\LIB\amd64;%LINKERDIR%\lib\x64;%MATLAB%\extern\lib\win64;%LIB%
set MW_TARGET_ARCH=win64
rem ********************************************************************
rem Compiler parameters
rem ********************************************************************
set COMPILER=cl
set COMPFLAGS=/c /Zp8 /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologo /MD
set OPTIMFLAGS=/O2 /Oy- /DNDEBUG
set DEBUGFLAGS=/Z7
set NAME_OBJECT=/Fo
rem ********************************************************************
rem Linker parameters
rem ********************************************************************
set LIBLOC=%MATLAB%\extern\lib\win64\microsoft
set LINKER=link
set LINKFLAGS=/dll /export:%ENTRYPOINT% /LIBPATH:"%LIBLOC%" libmx.lib libmex.lib libmat.lib /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /manifest /incremental:NO /implib:"%LIB_NAME%.x" /MAP:"%OUTDIR%%MEX_NAME%%MEX_EXT%.map"
set LINKOPTIMFLAGS=
set LINKDEBUGFLAGS=/debug /PDB:"%OUTDIR%%MEX_NAME%%MEX_EXT%.pdb"
set LINK_FILE=
set LINK_LIB=
set NAME_OUTPUT=/out:"%OUTDIR%%MEX_NAME%%MEX_EXT%"
set RSP_FILE_INDICATOR=@
rem ********************************************************************
rem Resource compiler parameters
rem ********************************************************************
set RC_COMPILER=rc /fo "%OUTDIR%mexversion.res"
set RC_LINKER=
set POSTLINK_CMDS=del "%LIB_NAME%.x" "%LIB_NAME%.exp"
set POSTLINK_CMDS1=mt -outputresource:"%OUTDIR%%MEX_NAME%%MEX_EXT%;2" -manifest "%OUTDIR%%MEX_NAME%%MEX_EXT%.manifest"
set POSTLINK_CMDS2=del "%OUTDIR%%MEX_NAME%%MEX_EXT%.manifest"
set POSTLINK_CMDS3=del "%OUTDIR%%MEX_NAME%%MEX_EXT%.map"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment