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.