Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Solved a MEX File Compilation in OSX

Subject: Solved a MEX File Compilation in OSX

From: Thomas Clark

Date: 28 Dec, 2010 16:14:05

Message: 1 of 7


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=':'

Subject: Solved a MEX File Compilation in OSX

From: Robin

Date: 1 Apr, 2011 16:31:04

Message: 2 of 7

"Thomas Clark" wrote in message
> And the relevant part of my custom mexopts.sh file looks like this:

Just wanted to say thanks very much for this. Worked perfectly for me with R2010b and latest version of ifort. Together with the excellent Fortran 95 interface (in Matlab Central) it is a brilliant combination!

> Thanks to this post for finally showing the way...
> http://stackoverflow.com/questions/1700628/embed-python-in-matlab-mex-file-on-os-x

That was actually my question from some time ago! I felt a bit silly at the time so I'm glad it was useful to someone else (and then back to me again!)

Cheers

Robin

Subject: Solved a MEX File Compilation in OSX

From: Thomas Clark

Date: 10 Apr, 2011 20:14:04

Message: 3 of 7

Glad to be of help!


"Robin " <robince@gmail.com> wrote in message <in4uk8$9ch$1@fred.mathworks.com>...
> "Thomas Clark" wrote in message
> > And the relevant part of my custom mexopts.sh file looks like this:
>
> Just wanted to say thanks very much for this. Worked perfectly for me with R2010b and latest version of ifort. Together with the excellent Fortran 95 interface (in Matlab Central) it is a brilliant combination!
>
> > Thanks to this post for finally showing the way...
> > http://stackoverflow.com/questions/1700628/embed-python-in-matlab-mex-file-on-os-x
>
> That was actually my question from some time ago! I felt a bit silly at the time so I'm glad it was useful to someone else (and then back to me again!)
>
> Cheers
>
> Robin

Subject: Solved a MEX File Compilation in OSX

From: Arwel

Date: 8 Jul, 2011 13:20:12

Message: 4 of 7

Thanks so so much!
I have been teasring my hair out over this one!
But..this took me a long time to find. How about re-posting with a more direct title, something like 'Solved: Undefined Symbols problem with fortan mex on 64 bit OSX' or something?? :o)
Just a thought..... but thanks!
Arwel

Subject: Solved a MEX File Compilation in OSX

From: Thomas Clark

Date: 8 Jul, 2011 22:24:09

Message: 5 of 7

Glad to hear it worked; I've added your suggested keywords to the search tags :)

Tom


"Arwel " <a.v.hughes@rl.ac.uk> wrote in message <iv706c$2g0$1@newscl01ah.mathworks.com>...
> Thanks so so much!
> I have been teasring my hair out over this one!
> But..this took me a long time to find. How about re-posting with a more direct title, something like 'Solved: Undefined Symbols problem with fortan mex on 64 bit OSX' or something?? :o)
> Just a thought..... but thanks!
> Arwel

Subject: Solved a MEX File Compilation in OSX

From: Arwel

Date: 8 Jul, 2011 22:38:09

Message: 6 of 7

Ah thanks. But a false dawn for me, I'm afraid!
In the meantime, here's my sorry tale.....
http://www.mathworks.com/matlabcentral/newsreader/view_thread/310277#844777











"Thomas Clark" wrote in message <iv8029$1lf$1@newscl01ah.mathworks.com>...
> Glad to hear it worked; I've added your suggested keywords to the search tags :)
>
> Tom
>
>
> "Arwel " <a.v.hughes@rl.ac.uk> wrote in message <iv706c$2g0$1@newscl01ah.mathworks.com>...
> > Thanks so so much!
> > I have been teasring my hair out over this one!
> > But..this took me a long time to find. How about re-posting with a more direct title, something like 'Solved: Undefined Symbols problem with fortan mex on 64 bit OSX' or something?? :o)
> > Just a thought..... but thanks!
> > Arwel

Subject: Solved a MEX File Compilation in OSX

From: Thomas Clark

Date: 9 Jul, 2011 08:48:07

Message: 7 of 7

Actually, I think I've just found an alternative (and probably more robust) solution.

Looking at Intel Fortran Compiler Command Line Options...
http://software.intel.com/file/6335

.. I found an option for -nofor_main which looks as if it will solve the issue.

Tom

"Arwel " <a.v.hughes@rl.ac.uk> wrote in message <iv80sh$3m8$1@newscl01ah.mathworks.com>...
> Ah thanks. But a false dawn for me, I'm afraid!
> In the meantime, here's my sorry tale.....
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/310277#844777
>
>
>
>
>
>
>
>
>
>
>
> "Thomas Clark" wrote in message <iv8029$1lf$1@newscl01ah.mathworks.com>...
> > Glad to hear it worked; I've added your suggested keywords to the search tags :)
> >
> > Tom
> >
> >
> > "Arwel " <a.v.hughes@rl.ac.uk> wrote in message <iv706c$2g0$1@newscl01ah.mathworks.com>...
> > > Thanks so so much!
> > > I have been teasring my hair out over this one!
> > > But..this took me a long time to find. How about re-posting with a more direct title, something like 'Solved: Undefined Symbols problem with fortan mex on 64 bit OSX' or something?? :o)
> > > Just a thought..... but thanks!
> > > Arwel

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us