This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

How to build Matlab 5 MEX files with the GNU-Win32 tools


I finally succeeded to build 32-bit MEX files for Matlab 5 using
the free GNU Win32 tools (http://www.cygnus.com/misc/gnu-win32).

The main problem was that the linker in the b18 release leaves
cruft in the unused parts of the various sections of the dll.
This seems to occur mostly on Win95. I do not have the problem
on our NT system (NT3.51 with Citrix WinFrame multi-user extension).
Digging in the gnu-win32 mailing-list archives I found various
references to the problem. The recently posted peclean.exe by
William Greathouse solves it. See
http://www.cygnus.com/ml/gnu-win32/1997-Jul/0047.html.
Attached is the script which (for me) successfully builds
the yprime example. See 'Getting Started' in the API Guide,
pp. 1-9 -- 1--10.

I am also working on a mexopts.bat and associated scripts so
you can build MEX files from within matlab.

Ton van Overbeek, tvoverbe@wk.estec.esa.nl
#!/bin/sh
#  Example Script to compile and link a MEX file (relocatable DLL)
#    File(s) that make up the DLL : yprime.c.
#  Prerequisites:
#   - GNU tools on the path
#   - Availability of peclean.exe 
#     (See http://www.cygnus.com/ml/gnu-win32/1997-Jul/0047.html
#      A quick fix for the DLL .reloc problem)
#
# ***Fill in your path to libcygwin.a here (with no trailing slash)***
LIBPATH=/gnuwin32/b18/H-i386-cygwin32/i386-cygwin32/lib
# ***Fill in your path to the matlab 5 root here (with no trailing slash)***
MATLAB=/win32app/matlab5
# ***Fill in the complete path to the peclean executable ***
PECLEAN=/usr/local/bin/peclean

# Compile source file(s):
echo 'Compiling file(s) ...'
gcc -c -DMATLAB_MEX_FILE -I$MATLAB/extern/include yprime.c

# Create fixup.o which is needed to terminate the
# import list and the relocation section in the dll.
cat > fixup.c << EOF
/* Copied from winsup/dcrt0.cc in the cygwin32 source distribution. */
	asm(".section .idata\$3\n" ".long 0,0,0,0, 0,0,0,0");
/* Next one is needed to properly terminate the .reloc section in the dll */
	asm(".section .reloc\n" ".long 0,8");
EOF
gcc -c fixup.c

# Create a very minimalist startup routine for the dll.
# C copy of winsup/init.cc in the cygwin32 source distribution.
cat > init.c << EOF
#include <windows.h>

int WINAPI dll_entry (HANDLE h,
		     DWORD reason,
		     void *ptr)
{
  switch (reason)
    {
    case DLL_PROCESS_ATTACH:
      break;
    case DLL_PROCESS_DETACH:
      break;
    case DLL_THREAD_ATTACH:
      break;
    case DLL_THREAD_DETACH:
      break;
    }
  return 1;
}
EOF
gcc -c init.c

# Dlltool in the b18 release does not like .def files to start
# with a line 'LIBRARY <libname>'.
# Therefore this workaround: 
#  extract library name from line 1 into variable libname
#  and feed dlltool with a .def file with the 1st line stripped
#  so the .def file starts with the line 'EXPORTS'
echo 'Creating MATLAB import library ...'
libname=`head -1 $MATLAB/extern/include/matlab.def | sed -e 's/^LIBRARY //'`
tail +2l $MATLAB/extern/include/matlab.def > temp.def
dlltool --def temp.def --dllname $libname --output-lib libmatlab.a
rm temp.def

# Make .def file:
echo EXPORTS > yprime.def
echo mexFunction >> yprime.def

# Link DLL.
echo 'Linking DLL ...'
ld --base-file yprime.base --dll -o yprime.dll  yprime.o init.o fixup.o \
 libmatlab.a $LIBPATH/libcygwin.a -e _dll_entry@12
dlltool --dllname yprime.dll --def yprime.def --base-file yprime.base \
 --output-exp yprime.exp
ld yprime.exp --dll -o yprime.dll yprime.o init.o fixup.o \
 libmatlab.a  $LIBPATH/libcygwin.a -e _dll_entry@12

# Clean up
echo 'Cleaning up ...'
$PECLEAN yprime.dll
rm yprime.o init.c init.o fixup.c fixup.o libmatlab.a \
 yprime.def yprime.exp yprime.base

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]