How to use MEX.BAT and the GNU Win32 tools in MATLAB 5

Ton van Overbeek tvoverbe@wk.estec.esa.nl
Wed Jul 9 07:34:00 GMT 1997


As promised here my setup to use the GNU Win32 tools from within
Matlab with the mex command (using mex.m and MEX.BAT).
The following things have to be in place on your Win95/WinNT system:
- The GNU Win32 tools have to be installed properly.
  I have used the b18 release.
  See http://www.cygnus.com/misc/gnu-win32
- The GNU tools have to be on the DOS path.
- Also the %MATLAB%\bin directory (where matlab.exe and mex.bat live)
  has to be on the DOS path. Otherwise mex.m cannot start mex.bat.
- You will probably need the patched version of cygwin.dll and
  libcygwin.a from Sergey Okhapkin. I had to use those for my initial
  (non-Matlab) DLL builds and never went back to the ones supplied
  with the Cygnus b18 release.
  Alternatively you might want to try to use the minimalist GNU Win32
  system which does not need cygwin.dll. I have not tried that.
  See http://www/fu.is.saga-u.ac.jp/~colin/gcc.html .
- You will also need peclean.exe to cleanup the DLL's produced by
  the linker. GNU Win32's ld leaves cruft in the unused part at the
  end of a section (especially on Win95). The DLL section with 
  relocation information is especially sensitive for this. If there is
  non-zero cruft immeidately after the usefull part of the .reloc
  section the DLL will not load. This program can be found in the
  gnu-win32 mailing list archive:
   http://www.cygnus.com/ml/gnu-win32/1997-Jul/0047.html .

When this is done, put all the *.sh and *.bat files attached to this
message in %MATLAB%\bin and you should be set to go.

I have successfully compiled and run the yprime, explore and mextest1
examples from %MATLAB%\extern\examples\mex.

Also the MAT and engine examples in %MATLAB%\extern\examples\eng_mat
compiled and ran successfully using the gw32engmatopts.bat options
file. Note that engwindemo.c has a nasty bug. buffer is declared as
char buffer[256] while later engOutputBuffer wants to use a size of
300. This will write past the end of the declared buffer. Result:
crash, access violation, ... So before compiling engwindemo.c increase
the declared size of buffer to 300.
Also both in engdemo.c and engwindemo.c the buffer should be zeroed
before each use to ensure a proper zero termination when the buffer
is filled by Matlab.

I hope this information is usefull to some of you who want to use the
GNU-Win32 tools for MEX file development.

Ton van Overbeek, tvoverbe@wk.estec.esa.nl
European Space Agency, ESTEC, Noordwijk, The Netherlands


More information about the Cygwin mailing list