How can I compile Fortran MEX-files in Microsoft Visual Studio?

5 views (last 30 days)
I would like to be able to compile Fortran MEX-files in Microsoft Visual Studio and want to know if there is an example available.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The following example was compiled in Visual Studio 2005 with Intel Fortran 9.1 integrated, and with example files from MATLAB 7.4 (R2007a). The steps are similar to the ones described in the documentation at
web([docroot,'/techdoc/matlab_external/f24338.html'])
Note that in the following lines MATLABROOT refers to the MATLAB install directory on your machine.
1. Create a project in Visual Studio 2005 of type Intel Fortran Project -> Dynamic Link Library, and name it for example yprime_fortran.
2. Insert your MEX source files (the example here uses the files yprimef.F and yprimefg.F from MATLABROOT\extern\examples\mex).
3. Create a .def file to export the MEX entry point. On the Project menu, click Add New Item and select Text File. Name the file module.def and include the following text
LIBRARY yprime_fortran.mexw32
EXPORTS _MEXFUNCTION@16
4. On the Project menu, click Properties for the project and make the following changes:
a. Under Fortran -> General -> Additional Include Directories, add the MATLAB include directory MATLABROOT\extern\include
b. Under Fortran -> General -> Preprocessor Definitions, add MATLAB_MEX_FILE as a preprocessor definition.
c. Under Fortran -> Preprocessor -> Preprocess Source File, choose Yes
d. Under Fortran -> External Procedures -> Calling Convention, choose 'STDCALL, REFERENCE'
e. Under Fortran -> External Procedures -> Name Case Interpretation, choose Upper Case
f. Under Linker -> General, change the output file extension to .mexw32 if you are building for a 32–bit platform or .mexw64 if you are building for a 64–bit platform.
g. Locate the .lib files for the compiler you are using under MATLABROOT\extern\lib\win32\microsoft or MATLABROOT\extern\lib\win64\microsoft. Under Linker -> Input -> Additional Dependencies, add libmx.lib and libmex.lib.
h. Under Linker -> Input -> Module Definition File, add the module definition (.def) file you created.
i. Under Debugging -> Command, copy the path to MATLAB.exe (for example, C:\Program Files\MATLAB\R2007a\bin\win32\MATLAB.exe)
5. Build the solution and run it using Debug -> Start Debugging. This will open a MATLAB window
6. Change to the directory containing the .mexw32 function and type
>> which yprime_fortran
Ensure that this points to the current directory.
7. Execute
>> yprime_fortran(1,1:4)
and you should see a result similar to the following
ans =
2.0000 8.9685 4.0000 -1.0947
The attached ZIP-file contains all the files including the Visual Studio project (except for the 2 .F files available in MATLABROOT\extern\examples\mex). You might need to change the directory structures to point to your files, as well as delete and add the source .F files from the correct location.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Products


Release

R2007a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!