Linker error compiling mex with mingw-w64

2020-03-26 14:07发布

问题:

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 here and here, 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:

>mex -v Gomoku_mex.cpp
-> Default options filename found in C:\Users\Bas\AppData\Roaming\MathWorks\MATLAB\R2013a 
-> Options file = C:\Users\Bas\AppData\Roaming\MathWorks\MATLAB\R2013a\mexopts.bat
      MATLAB                 = C:\Program Files\MATLAB\R2013a
->    COMPILER               = x86_64-w64-mingw32-g++ 
->    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 
->    Pre-linking commands=  
->    LINKER              = x86_64-w64-mingw32-g++ 
->    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  =  
->    Resource Compiler   =   
->    Resource Linker     =   
---------------------------------------------------------------- 

--> 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 & compiler names and added -std=c++11

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 > mex.def & echo mexFunction >> 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

回答1:

Take the following configuration file that I'm using (you'll need to adjust the path pointing to MinGW-w64 location accordingly):

mingw_mexopts.bat

@echo off

set MATLAB=%MATLAB%
set MW_TARGET_ARCH=win64
set PATH=C:\MinGW-w64\mingw64\bin;%PATH%

set COMPILER=x86_64-w64-mingw32-g++
set COMPFLAGS=-c -m64 -mwin32 -mdll -Wall -std=c++11 -DMATLAB_MEX_FILE
set OPTIMFLAGS=-DNDEBUG -O2
set DEBUGFLAGS=-g
set NAME_OBJECT=-o

set LINKER=x86_64-w64-mingw32-g++
set LINKFLAGS=-shared -L"%MATLAB%\extern\lib\win64\microsoft" -L"%MATLAB%\bin\win64"
set LINKFLAGSPOST=-lmx -lmex -lmat
set LINKOPTIMFLAGS=-O2
set LINKDEBUGFLAGS=-g
set LINK_FILE=
set LINK_LIB=
set NAME_OUTPUT=-o "%OUTDIR%%MEX_NAME%%MEX_EXT%"

Next here is a simple MEX-function that uses C++11 threads:

test.cpp

#include "mex.h"
#include <vector>
#include <thread>

void say_hello(int tid) {
    mexPrintf("hello from %d\n", tid);
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    std::vector<std::thread> threads;
    for (int i=0; i<10; i++) {
        threads.push_back(std::thread(say_hello, i));
    }        
    for(auto& t : threads) {
        t.join();
    }
}

Finally we compile and run it in MATLAB:

>> mex -f mingw_mexopts.bat -largeArrayDims test.cpp

>> setenv('PATH', ['C:\MinGW-w64\mingw64\bin;', getenv('PATH')])

>> test
hello from 0
hello from 4
hello from 2
hello from 3
hello from 5
hello from 1
hello from 6
hello from 8
hello from 7
hello from 9

Note that if you're going to deploy this to another machine, you'll have to also copy a few dependent DLL's (you'll find them in MinGW bin folder), and place them next to the MEX-file. Use Dependency Walker to list them. In my case it was:

  • libstdc++-6.dll
  • libgcc_s_seh-1.dll
  • libwinpthread-1.dll

I am using GCC 4.8.2 with MATLAB R2014a running on 64-bit Windows.



回答2:

Note these error messages:

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'

The library search path for libmex, libmx, libmat, ... is not added in your link command. The directory in your script is the bin directory containing DLLs. That's not correct here.

LINKFLAGS  =  -shared mex.def -L"C:\Program Files\MATLAB\R2013a\extern\lib\win64\microsoft" -static-libstdc++