To compile FORTRAN code into a MEX file with Intel Fortran Composer XE 2011 in Microsoft Visual Studio 2010, follow the ensuing steps.
This solution demonstrates compiling the example file 'timestwo.F'.
1. Set up a project in Visual Studio as follows:
a. Open Microsoft Visual Studio and navigate to 'File' -> 'New' -> 'Project'.
b. Select 'Intel Visual Fortran' -> 'Library' -> 'Dynamic-link Library'.
c. Following this, enter the name of the project and its location in the indicated text areas and click OK.
2. Add 'timestwo.F' to the project:
a. Copy the file 'timestwo.F' to the folder where you created the Visual Studio project. This file may be obtained from the following location:
$MATLABROOT\extern\examples\refbook
where $MATLABROOT is the MATLAB root directory, and may be determined by entering the command:
>> matlabroot
at the MATLAB command prompt.
b. In Visual Studio, right-click on the 'Source Files' folder in the 'Solution Explorer' and click 'Add' -> 'Existing Item...'.
c. Select 'timestwo.F' and click 'Add'.
3. Create the module definition file:
a. Right click on the project name, choose 'Add' -> 'New Item'.
b. Select 'Text File (.txt)'
c. Name the file 'module.def' (remove the .txt extension).
d. After creating it, the file should open in Visual Studio. Paste the following lines:
LIBRARY MYFILE
EXPORTS MEXFUNCTION
where MYFILE is the name of the MEX file to create, e.g. timestwo.mexw32
e. Save the file.
4. Right click on the project name and select 'Properties' in the solution explorer to open the project
properties. Once this window has opened, make the following changes:
a. Under 'Fortran' -> 'General', add the directory
$MATLABROOT\extern\include
to the field 'Additional Include Directories'.
b. Under 'Fortran' -> 'Preprocessor', choose 'Yes(/fpp)' for 'Preprocess Source File'.
c. Also under 'Fortran' -> 'Preprocessor', add the following to 'Preprocessor Definitions':
MATLAB_MEX_FILE
d. Under 'Fortran' -> 'External Procedures', set the 'Calling Convention' field to:
'C, REFERENCE (/iface:cref)'
e. Also under 'Fortran' -> 'External Procedures', set the 'Name Case Interpretation' field to:
'Upper Case (/names:uppercase)'.
f. Under 'Linker' -> 'General', change 'Output File' to '$(OutDir)\$(ProjectName).mexw32'.
g. Also under 'Linker' -> 'General', add the directory
$MATLABROOT\extern\lib\win32\microsoft
to the 'Additional Library Directories' field.
h. Under 'Linker' -> 'Input', add the following:
libmx.lib libmex.lib libmat.lib
to the 'Additional Dependencies' field.
i. Also under 'Linker' -> 'Input', add the file
module.def
to the 'Module Definition File' field.
j. Click 'Ok' to save and close the project properties.
e. Click 'Apply' -> 'Ok' to close the project properties.
5. Build the solution.
6. In MATLAB, change the current MATLAB directory to the directory in which the MEX file was
created.
7. Run the MEX file using the following command:
For instance, if you built 'timestwo' into a MEX file named 'timestwo.mexw32' run it as follows:
You should receive the following output:
4.600000
Best Answer