# Fortran compiled mex files with Matlab 2010b on Mac OS X

Although I personally have don’t need to compile any mex files in fortran for Matlab, another scientist here at NWRA does, so I thought I was post how to get this running for anyone who is interested.

The instructions are specific to the 64-bit Matlab 2010b on Mac OS X. According to MathWorks, we need to use gfortran version 4.3.

1. Download and install Xcode 3.2 from Apple (this will give you a c compiler).

4. Check out gcc version 4.3.4 with

svn co svn://gcc.gnu.org/svn/gcc/tags/gcc_4_3_4_release

5. Make an empty build folder for gcc, I called it “gcc-build”

6. Move both gmp and mpfr to the “gcc_4_3_4_release” folder and make sure they’re labeled exactly “gmp” and “mpfr”. At this stage I have a root folder called “gfortran” with,

gfortran/gcc_4_3_4_release gfortran/gcc_4_3_4_release/gmp gfortran/gcc_4_3_4_release/mpfr gfortran/gcc-build

7. From within the gcc-build folder run,

../gcc_4_3_4_release/configure --enable-languages=c,fortran --target=x86_64-apple-darwin -build=x86_64-apple-darwin --host=x86_64-apple-darwin

8. Compile gcc, gfortran, and the associated libraries (this will take a while),

make -j4

9. Finally you can install this with,

sudo make install

From within Matlab, run “mex-setup” and choose the gcc option. At this point you should be able to compile the yprimef example code included with Matlab.

## 9 thoughts on “Fortran compiled mex files with Matlab 2010b on Mac OS X”

1. alex says:

Thank you very much for your post.
It really helped me. I wanted desperately to compile a fortran program for random forest classification.
Thank you again. Keep posting interesting things.

Alex

2. Tee says:

Hi, I am having trouble with this. Where do i get GMP version 4.3.2.?? I can only see GMP 5.02.
Also, what does this mean? : 4. Check out gcc version 4.3.4 with svn co svn://gcc.gnu.org/svn/gcc/tags/gcc_4_3_4_release. I typed this in the terminal. Is this what I should be doing?
Step 6: Am i supposed to create a folder an call it gfortran? or should it already exist?

How do i Compile gcc, gfortran, and the associated libraries (this will take a while)?? From where??,

Do I type: make -j4 and sudo make install in terminal?

I am so lost and I need to run this fortran mex from matlab. Any help will be greaqtly appreciated. Thank you

3. Jeffrey J. Early says:

Tee,

I created the folder named gfortran for organizational purposes and put all the files in there. It’s a good idea to create one, but you can call it anything you want.

From the terminal, get yourself inside that directory, and then run that ‘svn’ command. It will then download gcc to that directory.

The ‘make’ command starts the compile — yes, you have to do it from the command line inside the right folder.

You will probably need to be at least somewhat comfortable with the command line to make this work… but I hope this helps!

Jeffrey

4. Tee says:

Thank you so much for your help. I have done all the steps except:

7. From within the gcc-build folder run,

../gcc_4_3_4_release/configure –enable-languages=c,fortran –target=x86_64-apple-darwin -build=x86_64-apple-darwin –host=x86_64-apple-darwin

To do this, am I in terminal?? I dont understand. The gcc-build folder is empty. How do i run this from there?

8. Compile gcc, gfortran, and the associated libraries (this will take a while),

make -j4

Do I type make-j4 in terminal to compile gcc,gfortran and associated libraries?? how exactly do i do this?

9. Finally you can install this with,

sudo make install

Do i just type “sude make install” in terminal??

5. Tee says:

Actually, these are the only steps i have left:

8. Compile gcc, gfortran, and the associated libraries (this will take a while),

make -j4

how do i do this?

9. Finally you can install this with,

sudo make install

Do i just type “sudo make install” in terminal??

6. Tee says:

Final questions:

For steps 8 & 9, should i be doing this only for the gcc-build directory?? Should i be in just that directory when i do those steps?? For some reason it is still not compiling

thanks

7. Arwel says:

Hi,
I followed these instructions, and the gcc side of things seemed to go as described. My problem was then using it within Matlab.

I typed ‘mex -setup’ and got the following…..

mex -setup

Options files control which compiler to use, the compiler and link command
options, and the runtime libraries to link against.

Using the ‘mex -setup’ command selects an options file that is
placed in ~/.matlab/R2010b and used by default for ‘mex’. An options
file in the current working directory or specified on the command line
overrides the default options file in ~/.matlab/R2010b.

To override the default options file, use the ‘mex -f’ command

The options files available for mex are:

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

2: /Applications/MATLAB_R2010bSP1.app/bin/mexopts.sh :
Template Options file for building MEX-files via the system ANSI compiler

0: Exit with no changes

Enter the number of the compiler (0-2):
1

/Applications/MATLAB_R2010bSP1.app/bin/gccopts.sh is being copied to
/Users/arwelhughes/.matlab/R2010b/mexopts.sh

**************************************************************************
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the new
http://www.mathworks.com/support/solutions/en/data/1-5C27B9/?solution=1-5C27B9
Building with the -largeArrayDims option enables the new API.
**************************************************************************

Then however, I tried to mex a fortran bit of code, and it didn’t seem to work…..

>> mex abeles_new_2.for

mex: compile of ‘ “abeles_new_2.for”‘ failed.

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

>>

Any idea why Matlab can’t find gfortran?? All the compile steps seemed to work fine. Do I need to change the mexopts.sh file to point to the new gfortran somehow?? (I should point out the I have mexed the above file with no problems under winxp and 32 bit Mac just fine in the past).
Many Thanks,
Arwel

8. María says:

I have done all the steps with no error messages. I assumed that gfortran 4.3 is included in gcc4.3.4 but this is the only thing about which I am not sure. If I should installl gfortran4.3 I was not able to find that version (but only last version 4.6.x) in the link provided by you.

Thanks a lot for your help

Then, after doing all steps and with my assumption taken on board I got the following error:
mex timestwo.F

mex: compile of ‘ “timestwo.F”‘ failed.

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

9. Maria says:

After the steps indicated by Jeffrey I did the following modifications in the mexopts.sh file under the section “maci64″. I am using matlabr2010b installed in my mac os x snowleopard. I also modified the system version (10.6 instead of 10.5) at the begining of the maci64 section.

# FortrankeyName: GNU Fortran

# FortrankeyManufacturer: GNU
# FortrankeyLanguage: Fortran
# FortrankeyVersion:
FC=’/usr/local/bin/gfortran’
FC_LIBDIR=’/usr/local/lib/’
FLIBS=”$MLIBS -L$FC_LIBDIR”
FOPTIMFLAGS=’-O5 -funroll-loops -ftree-vectorize’
LDDEBUGFLAGS=’-g’

It is working perfectly.

I would like to thank Jeffrey for the information of this web page and the clear description of the steps to be done.

Best regards,
María