Thread Subject: gfortran and maci64

Subject: gfortran and maci64

From: Dave

Date: 1 Oct, 2009 23:27:04

Message: 1 of 6

I recently needed to create some 64-bit mex builds that included C++ and Fortran code on Mac OS X. Here's what worked for me:

1) Install gfortran 4.2 from http://r.research.att.com/tools/ and install Xcode 3.1 from Apple's Developer Tools website

2) Note that gfortran installs in '/usr/local/bin' and places libraries in '/usr/local/lib', so you may need to add the former to your bash profile if calling it outside of Matlab

3) run 'mex -setup' from the matlab command prompt and specify '1' for:

  1: /Applications/MATLAB_R2009b.app/bin/gccopts.sh :
      Template Options file for building gcc MEX-files

4) Include the following modifications to the 'maci64' section of the
 '~/.matlab/R2009b/mexopts.sh' file:

            FC='/usr/local/bin/gfortran'
            FC_LIBDIR='/usr/local/lib/'
            FC_LIBDIR2='/usr/local/lib/gcc/i686-apple-darwin8/4.2.3/x86_64/'
            FOPTIMFLAGS='-O5 -funroll-loops -ftree-vectorize'

5) That's it, now perform a preliminary test with the following:
cd /Applications/MATLAB_R2009b.app/extern/examples/mex;
%-----Test C complier-----:
mex yprime.c
% Test your *.mexmaci64 file:
yprime(1,1:4)
% ans =
% 2 8.9685 4 -1.0947
%-----Test Fortran complier-----:
mex yprimef.F yprimefg.F
% Test your *.mexmaci64 file:
yprimef(1,1:4)
% ans = 2 8.9685 4 -1.0947


Good luck,
Dave

Subject: gfortran and maci64

From: Tim Davis

Date: 3 Oct, 2009 21:27:01

Message: 2 of 6

I have Xcode 3.2, not 3.1 as you have,
so I tried this: http://r.research.att.com/gfortran-42-5646.pkg
but when I compile, it complains that -lgfortran can't be found ... even when I
change FC_LIBDIR and FC_LIBDIR to point to the right place,
/usr/lib/gcc/i686-apple-darwin10/4.2.1/x86_64

In addition, gfortran complains that the flag -fbackslash is not defined. That's easy to fix - I just deleted the flag. But I can't get gfortran to find -lgfortran, even though I know the exact path where it lives.

Any ideas? (I'm new to the Mac, being mostly a Linux geek).

I also note that the default mexopts.sh for the 64-bit Mac uses gcc-4.0, not merely gcc (I have OS 10.6.1, Snow Leopard, on a 2009 MacBook Air). Why would the default gcc on the Mac be an older version? And one that doesn't match the gfortran you installed (4.2)?
Should I try gfortran 4.0 perhaps? Or use gcc not gcc-4.0 in mexopts.sh for my C compiler?

Fortran support is a minor issue for me, fortunately. It would be nice to get it working, though, as I do sometimes need it.

Subject: gfortran and maci64

From: Dave

Date: 3 Oct, 2009 22:47:01

Message: 3 of 6

Not sure I can be of much help as I'm new to this as well. I used http://r.research.att.com/gfortran-4.2.3.dmg instead of the experimental version for Xcode 3.2. You might try that one instead. Also, here's the complete maci64 section from my 'mexopts.sh' file...I only had to edit the lines beginning with ##. My main problem was it was using the wrong libgfortranbegin when building a 64-bit mex until I hard-coded the correct path. BTW, you may want to clear out the '/usr/local/lib/' directories before re-installing gfortran.



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='gfortran'
            FC='/usr/local/bin/gfortran'
            
            FFLAGS='-fexceptions -m64 -fbackslash'
                        
            ##FC_LIBDIR=`$FC -print-file-name=libgfortran.dylib 2>&1 | sed -n '1s/\/*libgfortran\.dylib//p'`
            FC_LIBDIR='/usr/local/lib/'
            
            ##FC_LIBDIR2=`$FC -print-file-name=libgfortranbegin.a 2>&1 | sed -n '1s/\/*libgfortranbegin\.a//p'`
            FC_LIBDIR2='/usr/local/lib/gcc/i686-apple-darwin8/4.2.3/x86_64/'
                       
            FLIBS="$MLIBS -L$FC_LIBDIR -lgfortran -L$FC_LIBDIR2 -lgfortranbegin"
                        
            ##FOPTIMFLAGS='-O'
            FOPTIMFLAGS='-O5 -funroll-loops -ftree-vectorize'
            
            FDEBUGFLAGS='-g'
#
            LD="$CC"
            LDEXTENSION='.mexmaci64'
            LDFLAGS="-Wl,-twolevel_namespace -undefined error -arch $ARCHS -Wl,-syslibroot,$SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
            LDFLAGS="$LDFLAGS -bundle -Wl,-exported_symbols_list,$TMW_ROOT/extern/lib/$Arch/$MAPFILE"
            LDOPTIMFLAGS='-O'
            LDDEBUGFLAGS='-g'
#
            POSTLINK_CMDS=':'

Subject: gfortran and maci64

From: Tim Davis

Date: 4 Oct, 2009 11:31:01

Message: 4 of 6

That worked, at least for the yprimef example - thanks!

It failed when I tried to use the %val construct, however. With RBio from the SuiteSparse package on my web page (http://www.cise.ufl.edu/research/sparse):

Compiling 64-bit version of RBio.
RBread_mex_64.f:129.28:

     $ mkind, skind, %val (Ap), %val (Ai), %val (Cx), nzeros,
                           1
Error: Kind of by-value argument at (1) is larger than default kind

I think %val is still expecting to convert 32-bit integers to 32-bit pointers.

This package is written in Fortran because it reads and writes files that include embedded Fortran IO format specifications. I do have a workaround ... to port the package to C. I've had hassles with %val, and Fortran in general, on other platforms, anyway.

I'm surprised that Apple's Xcode doesn't include Fortran.

Subject: gfortran and maci64

From: Dave

Date: 4 Oct, 2009 12:48:00

Message: 5 of 6

"Tim Davis" <davis@cise.ufl.edu> wrote in message
> I think %val is still expecting to convert 32-bit integers to 32-bit pointers.

Don't know much about this...I'm using a mex file that mixes C & Fortran (not written, only compiled by me) and the *.m file that calls the *.mex* passes integers to it using the int32 command.

> I'm surprised that Apple's Xcode doesn't include Fortran.
It's unfortunate. But knowing their philosophy I guess it's not too surprising as Fortran has little to do with creating Cocoa (or Iphone) apps. I'm still overwhelmed that OS X supports Matlab, not to mention a 64-bit version.

Dave

Subject: gfortran and maci64

From: Andrea Strzelec

Date: 27 Oct, 2009 21:00:18

Message: 6 of 6

Thanks Dave, your solution helped me get past Matlab's inability to find gfortran. However, I can't get past the next roadblock - mentioned by a previous poster as only a flag error (I tried removing the flag, it didn't help)

After changing the mexops.sh file, when I try to mex yprimef.F, I get the following error:
>> cd /Applications/MATLAB_R2009b.app/extern/examples/mex;
>> mex yprimef.F yprimefg.F
cc1: error: unrecognized command line option "-fbackslash"

    mex: compile of ' "yprimef.F"' failed.

??? Error using ==> mex at 221
Unable to complete successfully.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
mac Andrea Strzelec 27 Oct, 2009 17:04:04
mex gfortran Andrea Strzelec 27 Oct, 2009 17:04:04
64bit Dave 1 Oct, 2009 19:29:04
mac Dave 1 Oct, 2009 19:29:04
gfortran Dave 1 Oct, 2009 19:29:04
os x Dave 1 Oct, 2009 19:29:04
rssFeed for this Thread

Contact us at files@mathworks.com