1

I'm running 64 bit windows and 64 bit matlab. Following the instructions of an answer to a question on stackoverflow, I tried to configure a make file to compile my C function for use in matlab.

Here is the Makefile file:

MINGWPATH=/cygdrive/c/cygwin
CYGPATH=/cygdrive/c/cygwin

MATLABROOT=/cygdrive/c/Progra~1/MATLAB/R2011a
CC=i686-pc-mingw32-gcc
CFLAG= -Wall -m32 -O3 -Iinclude -I$(MATLABROOT)/extern/include $(SRC) $(LIBS) -o $(EXE)
MEXFLAG=-m64 -shared -DMATLAB_MEX_FILE -I$(MATLABROOT)/extern/include -Wl,--export-all-symbols $(LIBS) $(MEXSRC) -o $(MEXTGT).mexw64

LIBS= -Llib -L$(MATLABROOT)/bin/win64 -L$(MATLABROOT)/extern/lib/win64/microsoft -lmex -lmx -lmwlapack -lmwblas -leng
EXE= gopenfunction.exe
MEXTGT= gopenfunction.m
SRC= gopenfunction.c
MEXSRC = gopenfunction.c

all:$(EXE)

$(EXE):  $(SRC)
    $(CC) $(CFLAG) -ladvapi32 -luser32 -lgdi32 -lkernel32 -lmingwex -o $(EXE)

Output

$ make
i686-w64-mingw32-gcc -Wall -m32 -O3 -Iinclude -I/cygdrive/c/Progra~1/MATLAB/R2011a/extern/include gopenfunction.c -Llib -L/cygdrive/c/Progra~1/MATLAB/R2011a/bin/win64 -L/cygdrive/c/Progra~1/MATLAB/R2011a/extern/lib/win64/microsoft -lmex -lmx -lmwlapack -lmwblas -leng -o gopenfunction.exe -ladvapi32 -luser32 -lgdi32 -lkernel32 -lmingwex -o gopenfunction.exe
/cygdrive/c/Progra~1/MATLAB/R2011a/bin/win64/libmex.dll: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
Makefile:18: recipe for target 'gopenfunction.exe' failed
make: *** [gopenfunction.exe] Error 1

I also tried:

i686-w64-mingw32-gcc -m32 -shared -Iinclude -I"/cygdrive/c/Progra~1/MATLAB/R2011a/extern/include" -DMATLAB_MEX_FILE -o bla.mexw64 -Wl,--export-all-symbols *.c -L"/cygdrive/c/Progra~1/MATLAB/R2011a/bin/win64" -lmex -lmx -leng -lmat

I am using GCC because I cannot find VS2008 or 2010, only VS Community 2015 which doesn't work when I try to setup with mex via mex -setup

Note that I changed from -m64 to -m32 because otherwise I received the error below. I'm not entirely sure what it does.

$ i686-w64-mingw32-gcc -m64 -shared -Iinclude -I"/cygdrive/c/Progra~1/MATLAB/R2011a/extern/include" -DMATLAB_MEX_FILE -o bla.mexw64 -Wl,--export-all-symbols *.c -L"/cygdrive/c/Progra~1/MATLAB/R2011a/bin/win64" -lmex -lmx -leng -lmat
gopenfunction.c:1:0: sorry, unimplemented: 64-bit mode not compiled in
 #include "gclibo.h"

Visual Studio Follow-Up

After installing Microsoft Visual C++ the compiler option popped up but requires the SDK. Attempting to install the sdk results in an error 'visual studio 2010 must be installed.' I also installed .NET 4 and SDK for Windows 7 as per the supported compilers for MATLAB 2011 page.

>> mex -setup

Welcome to mex -setup.  This utility will help you set up  
a default compiler.  For a list of supported compilers, see  
http://www.mathworks.com/support/compilers/R2011a/win64.html 

Please choose your compiler for building MEX-files: 

Would you like mex to locate installed compilers [y]/n? y

Select a compiler: 
[1] Microsoft Visual C++ 2010 Express in C:\Program Files (x86)\Microsoft Visual Studio 10.0\ 

[0] None 

Compiler: 1

Please verify your choices: 

Compiler: Microsoft Visual C++ 2010 Express  
Location: C:\Program Files (x86)\Microsoft Visual Studio 10.0\ 

Are these correct [y]/n? y

***************************************************************************** 
  Error: Microsoft Visual C++ 2010 Express requires the Microsoft Windows 
         Software Development Kit (SDK), but the SDK cannot be found.  
         For more information about the required SDK, see:  
         http://www.mathworks.com/support/compilers/R2011a/win64.html 
***************************************************************************** 

??? Error using ==> mex at 208
Unable to complete successfully.

Cygwin Follow-Up

I changed the compiler but now there is an undefined reference to 'WinMain.'

$ make
x86_64-w64-mingw32-gcc -Wall -m64 -O3 -Iinclude -I/cygdrive/c/Progra~1/MATLAB/R2011a/extern/include gopenfunction.c -Llib -L/cygdrive/c/Progra~1/MATLAB/R2011a/bin/win64 -L/cygdrive/c/Progra~1/MATLAB/R2011a/extern/lib/win64/microsoft -lmex -lmx -lmwlapack -lmwblas -leng -o gopenfunction.exe -ladvapi32 -luser32 -lgdi32 -lkernel32 -lmingwex -o gopenfunction.exe
/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
/usr/src/debug/mingw64-x86_64-runtime-4.0.2-1/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2: error: ld returned 1 exit status
Makefile:18: recipe for target 'gopenfunction.exe' failed
make: *** [gopenfunction.exe] Error 1

edit - using the second method, I was able to compile using the new x86_64 compiler.

x86_64-w64-mingw32-gcc -m64 -shared -Iinclude -I"/cygdrive/c/Progra~1/MATLAB/R2011a/extern/include" -DMATLAB_MEX_FILE -o bla.mexw64 -Wl,--export-all-symbols *.c -L"/cygdrive/c/Progra~1/MATLAB/R2011a/bin/win64" -lmex -lmx -leng -lmat

edit2 - after investigating, the absence of -shared option is what causes the 'WinMain' error. I would really appreciate an explanation. I don't know anything about dlls, import libraries, shared libraries, etc, except that they are used at runtime. On the whole my problem has been solved and I thank everyone for their help.

$ x86_64-w64-mingw32-gcc -m64 -shared -Iinclude -I"/cygdrive/c/Progra~1/MATLAB/R2011a/extern/include" -o bla.mexw64 *.c -L"/cygdrive/c/Progra~1/MATLAB/R2011a/bin/win64" -lmex -lmx -leng -lmat

$ x86_64-w64-mingw32-gcc -m64 -Iinclude -I"/cygdrive/c/Progra~1/MATLAB/R2011a/extern/include" -o bla.mexw64 *.c -L"/cygdrive/c/Progra~1/MATLAB/R2011a/bin/win64" -lmex -lmx -leng -lmat
/usr/x86_64-w64-mingw32/sys-root/mingw/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
/usr/src/debug/mingw64-x86_64-runtime-4.0.2-1/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2: error: ld returned 1 exit status

edit3 - looking at the original makefile in the question linked at the start, all of the above is already there under the $(MEXTGT) target. I just didn't understand how that would be called or used so I foolishly deleted it.

Community
  • 1
  • 1
user2316667
  • 5,444
  • 13
  • 49
  • 71
  • `-m32` may have avoided an error, but it did not solve your problem. You **need** `-m64` to compile a MEX to load in 64-bit Matlab. Get a gcc with 64-bit mode enabled, or else use Visual Studio. You need a link for VS2010 Express Edition? See http://stackoverflow.com/questions/3061811/how-to-get-download-link-to-vs2010-express-for-use-in-a-download-manager – Ben Voigt Jul 22 '15 at 22:07
  • Or ask a question about getting `mex -setup` to work with VS 2015. Since it was just released, it's a great time for such a question, and there's [a history of having to rewrite mex configuration whenever a new compiler is released](http://stackoverflow.com/a/22567960/103167), so it will be well-received. – Ben Voigt Jul 22 '15 at 22:09
  • Since you are looking at that thread to setup GCC, scroll down and follow [this](http://stackoverflow.com/a/28490382/2778484) answer instead. It was added years after the others and (IMHO) is the most current and easiest way -- It's not just an answer, *it's what I do*. You just need R2014a or newer. – chappjc Jul 22 '15 at 22:19
  • That said, have you tried installing the native 64-bit mingw cygwin packages called mingw64-x86_64-* (you probably need to call x86_64-w64-mingw32-gcc.exe)? – chappjc Jul 22 '15 at 22:25
  • You could also install 64-bit cygwin – Jens Munk Jul 22 '15 at 22:54
  • @JensMunk I'm assuming he did. Even so, you still have to select the mingw64 packages that target 64-bit systems. The compiler he is calling (starting with i686-) targets 32-bit and that is also available on "64-bit cygwin". – chappjc Jul 22 '15 at 23:43
  • @Ben Voigt That's true, but people probably want to configure VS2015 for newer versions of matlab. Still, if you think it's still worth asking for R2011a let me know. Also, I appreciate the link and I updated my question with the result of installing Visual C++ (the link was not for visual studio -- apparently) – user2316667 Jul 23 '15 at 15:26
  • Regarding the VS2010 express + SDK 7.1 problem, get all the available updates and patches, then look at this known problem: http://stackoverflow.com/a/19105762/2778484 – chappjc Jul 23 '15 at 16:19

1 Answers1

1

Despite having "w64" in the file name, i686-w64-mingw32-gcc.exe generates 32-bit binaries.

To generate 64-bit binaries, use x86_64-w64-mingw32-gcc.exe. In the cygwin package installer, look for mingw64-x86_64-.

"mingw64" GCC toolchains in the list below. The first targets 32-bit systems and the second 64-bit. They are named this way because they are from the "GCC for Win64" project (cygwin is not the only source for these toolchains). Also get the C toolchains (not shown below).

enter image description here

Also, based on my experience with the standalone mingw64, you probably need to add the following compiler flags to what you have:

-m64 -mwin32 -mdll ...

And you do need -shared as a linker flag.

chappjc
  • 30,359
  • 6
  • 75
  • 132
  • I really appreciate it, I didn't know this. I updated my question with a new error with new compiler that targets 64 bit systems. – user2316667 Jul 23 '15 at 15:28
  • @user2316667 Sadly, I don't have time to try this out, but I think you need to add some switches to the compiler flags. Add these: `-m64 -mwin32 -mdll `. And yes, you do need `-shared` as a linker flag. See the flags in the [xml configuration file](https://github.com/chappjc/MATLAB/blob/master/MinGW/mex_C%2B%2B_mingw-w64.xml) I've made for newer MATLAB versions. Another option is to try use the [standalone mingw64](http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/4.9.2/threads-posix/seh/) (non-cygwin). – chappjc Jul 23 '15 at 16:11