delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1997/07/08/07:05:09

From: tvoverbe AT wk DOT estec DOT esa DOT nl (Ton van Overbeek)
Subject: How to build Matlab 5 MEX files with the GNU-Win32 tools
8 Jul 1997 07:05:09 -0700 :
Approved: cygnus DOT gnu-win32 AT cygnus DOT com
Distribution: cygnus
Message-ID: <33C2172E.41C67EA6.cygnus.gnu-win32@wk.estec.esa.nl>
Mime-Version: 1.0
X-Mailer: Mozilla 3.01 (X11; I; SunOS 4.1.3_U1 sun4m)
Original-Cc: gnu-win32 AT cygnus DOT com, chj AT lin DOT foa DOT se, wgreathouse AT smva DOT com
Original-Sender: owner-gnu-win32 AT cygnus DOT com

This is a multi-part message in MIME format.

--------------167EB0E72781E494446B9B3D
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

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 AT wk DOT estec DOT esa DOT nl

--------------167EB0E72781E494446B9B3D
Content-Type: text/plain; charset=us-ascii; name="build.sh"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="build.sh"

#!/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 AT 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 AT 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
--------------167EB0E72781E494446B9B3D--

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019