Compiling 64bit MathLink code under cygwin on Windows 7

I am aware that building MathLink executables is not without its challenges.

However, I am trying to compile the MathLink example 'addtwo' from a makefile within cygwin on a 64 bit Windows 7 machine.

I'm using cycgwin as I already have a substantial codebase built in this environment, and I have tried the following compilers:

1. The default gcc compiler. This would not compile, as the -mno-cygwin flag has been removed.
2. i686-w64-mingw32-gcc. This compiles but generates undefined references for the MathLink functions.
3. x86_64-w64-mingw32-gcc. This appears to compile and link correctly.

In all three cases the command line flags to the compiler are the same for compile and link:

${CC} -c -mno-cygwin -mwin32 -I${INCS} $<${CC} -I${INCS} addtwo.o addtwotm.o -L${LIBS} -lML64i3m -mwindows -o $@  Although the third configuration compiles and links, it won't run correctly. When the prebuilt addtwo.exe is run from the command line, it puts up a dialogue box requesting the link parameters as expected. Mine will very briefly put up a dialogue box that is minimised, and then exit. Unfortunately gdb does not recognise the file format of the executable built with x86_64-w64-mingw32-gcc. Any insights will reduce hair loss, and I can supply whatever additional information is relevant. • I suppose the first two cases are working (or rather not working) as expected. Regarding the third, how did you get libML64i3m.a? MinGW-w64's ld can't link against MS-COFF import libraries as far as I know so you have to create your own from the associated .dll using gendef and dlltool. – Oleksandr R. Mar 26 '13 at 16:07 • x86_64-w64-mingw32-gcc seems to be referencing the import library, as there are no error messages at link time. I wasn't aware of the COFF format issue, so I'll look into it. Thanks. – Nick Name Mar 26 '13 at 17:40 • Wait... are you linking to the Cygwin import library in SystemFiles/Links/MathLink/DeveloperKit/Windows/CompilerAdditions/cygwin/lib? This is in GNU-COFF format, but is for the 32-bit MathLink library (because Cygwin is a 32-bit environment). If it links at all, it certainly won't work correctly at run time since a 64-bit program can't load a 32-bit .dll. – Oleksandr R. Mar 26 '13 at 18:19 • It's ml64i3m.lib in SystemFiles\Links\MathLink\DeveloperKit\Windows-x86-64\CompilerAdditions\mldev64\lib (Mathematica 9.0) – Nick Name Mar 26 '13 at 20:02 1 Answer Suppose you create a file addtwo.tm : :Begin: :Function: addtwo :Pattern: AddTwo[i_Integer, j_Integer] :Arguments: { i, j } :ArgumentTypes: { Integer, Integer } :ReturnType: Integer :End: :Evaluate: AddTwo::usage = "AddTwo[x, y] gives the sum of two machine integers x and y." int addtwo( int i, int j) { return i+j; } int __stdcall WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine, int nCmdShow) { char buff[512]; char FAR * buff_start = buff; char FAR * argv[32]; char FAR * FAR * argv_end = argv + 32; if( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1; MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start); return MLMain( argv_end - argv, argv); }  Into the same directory you put this Makefile : # Makefile for addtwo under Windows 7 and cygwin # this works for http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting %20 Win64/Personal%20Builds/rubenvb/gcc-4.8-release/x86_64-w64-mingw32-gcc-4.8.0-cygwin_rubenvb.tar.xz/download # # suppose you installed mingw64 (e.g. from the above link) to cygwin's /opt ( you may need to install 7zip from http://www .7-zip.org/ ) # # Do this from a "Cygwin Terminal", a bash shell only once: # cd "/cygdrive/c/Program Files/Wolfram Research/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Windows-x86-64/SystemAdditions/" # /usr/bin/gendef ml64i3.dll # /opt/mingw64/bin/x86_64-w64-mingw32-dlltool.exe --as-flags=--64 -m i386:x86-64 -k --input-def ml64i3.def --output-lib ml64i3n.lib # mv ml64i3n.lib "/cygdrive/c/Program Files/Wolfram Research/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Windows-x86-64/CompilerAdditions/mldev64/lib" # # then # # before running make, do: # PATH=/opt/mingw64/bin:/opt/mingw64:/usr/local/bin:/usr/bin:/cygdrive/c/Program\ Files/Wolfram\ Research/Mathematica/9.0 # # now, just cd to the directory where this Makefile and addtwo.tm is and run make CPP = "/opt/mingw64/bin/x86_64-w64-mingw32-gcc.exe" CADDSDIR = /cygdrive/c/Program\ Files/Wolfram\ Research/Mathematica/9.0/SystemFiles/Links/MathLink/DeveloperKit/Windows-x86-64/CompilerAdditions/mldev64 INCDIR =${CADDSDIR}/include
LIBDIR = ${CADDSDIR}/lib EXTRACFLAGS = "-mwindows" MPREP =${CADDSDIR}/bin/mprep.exe
all : ${BINARY} addtwo: addtwotm.o${CPP}  ${EXTRACFLAGS} -I${INCDIR} addtwotm.o -L${LIBDIR} -lml64i3n -o$@

.c.o :
${CPP} -c -static-libgcc -I${INCDIR} $< addtwotm.c : addtwo.tm${MPREP} $? -o$@

clean :
rm *.o *.exe *tm.c


and follow the instructions, then it all works on Windows 7 with Mathematica 9:

Credits: Oleksandr's nice post here

• Maybe it would be good to include a note that some of the spaces have to be changed to tabs in the Makefile (one of make's peculiarities...) – Szabolcs Mar 26 '13 at 22:37
• @RolfMertig. Wow, many thanks for a complete solution. All done and works like a dream. Thanks also to OleksandrR for kicking things off and Szabolcs for pointing out the makefile changes. – Nick Name Mar 26 '13 at 23:58