Linker error compiling mex with mingw-w64

admin

Administrator
Staff member
I'm trying to setup Mingw-w64 as the mex compiler in MATLAB 2013a. My laptop has x86_64 architecture and runs windows 7. The program I want to compile uses c++11-style threading, so I'm using mingw-w64 version 4.9.0 with posix threads.

According to instruction I found <a href="http://dgleich.wordpress.com/2012/01/08/compiling-mex-files-with-mingw64/" rel="nofollow noreferrer">here</a> and <a href="https://stackoverflow.com/questions/8552580/using-gccmingw-as-matlabs-mex-compiler">here</a>, I modified my mexopts.bat file. The code seems to compile successfully, but the linker reports an error. Does anyone have suggestions what I might be doing wrong?

By the way, I tried using gnumex to setup the compiler, but that didn't work either.

Here's the output and error message that MATLAB gives:

Code:
&gt;mex -v Gomoku_mex.cpp
-&gt; Default options filename found in C:\Users\Bas\AppData\Roaming\MathWorks\MATLAB\R2013a 
-&gt; Options file = C:\Users\Bas\AppData\Roaming\MathWorks\MATLAB\R2013a\mexopts.bat
      MATLAB                 = C:\Program Files\MATLAB\R2013a
-&gt;    COMPILER               = x86_64-w64-mingw32-g++ 
-&gt;    Compiler flags: 
      COMPFLAGS           = -std=c++11 -fexceptions -I"C:\Program Files\MATLAB\R2013a\extern\include" 
      OPTIMFLAGS          = -O3 -fexpensive-optimizations -DNDEBUG 
      DEBUGFLAGS          = -g -Wall -Wextra 
      arguments           =  
      Name switch         = -o 
-&gt;    Pre-linking commands=  
-&gt;    LINKER              = x86_64-w64-mingw32-g++ 
-&gt;    Link directives: 
      LINKFLAGS           =  -shared mex.def -L"C:\Program Files\MATLAB\R2013a\bin\win64" -static-libstdc++ 
      LINKDEBUGFLAGS      =  -g -Wall 
      LINKFLAGSPOST       =  -lmex -lmx -lmat -lmwlapack -lmwblas 
      Name directive      = -o "Gomoku_mex.mexw64" 
      File link directive =  
      Lib. link directive =  
      Rsp file indicator  =  
-&gt;    Resource Compiler   =   
-&gt;    Resource Linker     =   
---------------------------------------------------------------- 

--&gt; x86_64-w64-mingw32-g++  -std=c++11 -fexceptions -I"C:\Program Files\MATLAB\R2013a\extern\include" -oC:\Users\Bas\AppData\Local\Temp\mex_r7jRw0\Gomoku_mex.obj -I"C:\Program Files\MATLAB\R2013a\extern\include" -I"C:\Program Files\MATLAB\R2013a\simulink\include" -O3 -fexpensive-optimizations -DNDEBUG -DMX_COMPAT_32 Gomoku_mex.cpp 

C:\Users\Bas\AppData\Local\Temp\cc4hwD3A.o:Gomoku_mex.cpp:(.text+0x9d1c): undefined reference to `mxGetPr' 
C:\Users\Bas\AppData\Local\Temp\cc4hwD3A.o:Gomoku_mex.cpp:(.text+0x9d83): undefined reference to `mxCreateDoubleScalar' 
C:/PROGRA~1/mingw-w64/x86_64-4.9.0-posix-seh-rt_v3-rev2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Bas\AppData\Local\Temp\cc4hwD3A.o: bad reloc address 0x0 in section `.pdata$_ZNKSt5ctypeIcE8do_widenEc' 
collect2.exe: error: ld returned 1 exit status 

  C:\PROGRA~1\MATLAB\R2013A\BIN\MEX.PL: Error: Compile of 'Gomoku_mex.cpp' failed. 

Error using mex (line 206)
Unable to complete successfully.

Edit: As extra information, this is my mexopts.bat file. I got this directly from one of the two links above and modified directory &amp; compiler names and added -std=c++11

Code:
set MATLAB=%MATLAB%
set PATH=%PATH%;C:\PROGRA~1\mingw-w64\x86_64-4.9.0-posix-seh-rt_v3-rev2\mingw64\bin
set MW_TARGET_ARCH=win64

rem ********************************************************************
rem Compiler parameters
rem ********************************************************************
set COMPILER=x86_64-w64-mingw32-g++
set COMPFLAGS=-std=c++11 -fexceptions -I"%MATLAB%\extern\include" 
set OPTIMFLAGS=-O3 -fexpensive-optimizations -DNDEBUG
set DEBUGFLAGS=-g -Wall -Wextra 
set NAME_OBJECT=-o 

rem ********************************************************************
rem Linker parameters
rem ********************************************************************
set PRELINK_CMDS1=echo EXPORTS &gt; mex.def &amp; echo mexFunction &gt;&gt; mex.def
set LINKER=x86_64-w64-mingw32-g++
set LINKFLAGS= -static-libstdc++ -shared mex.def -L"%MATLAB%\bin\win64" -L"%MATLAB%\extern\lib\win64\microsoft" 
set LINKFLAGSPOST= -lmex -lmx -lmat -lmwlapack -lmwblas 
set LINKOPTIMFLAGS=-O3 
set LINKDEBUGFLAGS= -g -Wall
set LINK_FILE=
set LINK_LIB=
set NAME_OUTPUT=-o "%OUTDIR%%MEX_NAME%%MEX_EXT%"
set RSP_FILE_INDICATOR=
set POSTLINK_CMDS1=del mex.def