|
After struggling all afternoon to compile a MEX file I've discovered a fix for a problem, and thought I'd post it in case anyone has the same.
I've been using intel fortran (yeah, yeah, unsupported, I know... can't remember the last time I used a supported compiler) - but I don't think the error was compiler specific.
I'm using R2010b on an Intel based Macbook Pro with (Snow Leopard) OSX 10.6.5
The error was:
Undefined symbols:
"_MAIN__", referenced from:
_main in for_main.o
ld: symbol(s) not found
The problem is related to the linker. By default, the mexopts.sh file for macs includes a linker flag
-undefined error
To overcome the error, alter that flag in the mexopts.sh file to:
-undefined dynamic_lookup
then OSX does something mysterious (! I can't find documentation !) to make it work.
Thanks to this post for finally showing the way...
http://stackoverflow.com/questions/1700628/embed-python-in-matlab-mex-file-on-os-x
I use a custom mexopts.sh file to do this. For anyone interested in using ifort or custom mexopts files, I compile using the following command:
mex -v -f mexoptsCustom.sh myFortranFile.F90
And the relevant part of my custom mexopts.sh file looks like this:
#----------------------------------------------------------------------------
;;
maci64)
#----------------------------------------------------------------------------
# StorageVersion: 1.0
# CkeyName: GNU C
# CkeyManufacturer: GNU
# CkeyLanguage: C
# CkeyVersion:
CC='gcc-4.0'
SDKROOT='/Developer/SDKs/MacOSX10.5.sdk'
MACOSX_DEPLOYMENT_TARGET='10.5'
ARCHS='x86_64'
CFLAGS="-fno-common -no-cpp-precomp -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
CFLAGS="$CFLAGS -fexceptions"
CLIBS="$MLIBS"
COPTIMFLAGS='-O2 -DNDEBUG'
CDEBUGFLAGS='-g'
#
CLIBS="$CLIBS -lstdc++"
# C++keyName: GNU C++
# C++keyManufacturer: GNU
# C++keyLanguage: C++
# C++keyVersion:
CXX=g++-4.0
CXXFLAGS="-fno-common -no-cpp-precomp -fexceptions -arch $ARCHS -isysroot $SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
CXXLIBS="$MLIBS -lstdc++"
CXXOPTIMFLAGS='-O2 -DNDEBUG'
CXXDEBUGFLAGS='-g'
#
# FortrankeyName: GNU Fortran
# FortrankeyManufacturer: GNU
# FortrankeyLanguage: Fortran
# FortrankeyVersion:
FC='ifort'
FFLAGS='-c -fast -parallel -fexceptions'
FC_LIBDIR=''
FC_LIBDIR2=''
FLIBS="$MLIBS"
FOPTIMFLAGS='-O3'
FDEBUGFLAGS=''
#
LD="ifort"
LDEXTENSION='.mexmaci64'
LDFLAGS="-fast -parallel"
LDFLAGS="$LDFLAGS -Wl,-twolevel_namespace -arch $ARCHS -Wl,-syslibroot,$SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
LDFLAGS="$LDFLAGS -undefined dynamic_lookup -bundle -Wl,-exported_symbols_list,$TMW_ROOT/extern/lib/$Arch/$MAPFILE"
LDFLAGS="$LDFLAGS"
LDDEBUGFLAGS=''
#
POSTLINK_CMDS=':'
|