MATLAB: How to compile a Fortran program using MATLAB Engine in Visual Studio 2019

enginefortranintelMATLABparallelstudiovisual

I am trying to build a Fortran Engine program using the Intel Parallel Studio XE 2020 for Fortran with Visual Studio 2019. I am following the documentation page:
How can I do this in Visual Studio 2019?
 

Best Answer

Generally, you can use the MATLAB® Editor to write your engine application code and the mex command to build it, as outlined in Build Windows Engine Application.
If you prefer to use the Microsoft® Visual Studio® integrated development environment (IDE) to write your source code, you can still use the mex command to build it. However, to build your application with your IDE, follow the steps below. General guidelines for Microsoft® Visual Studio® and other IDEs, such as Xcode, can be found at Build Engine Applications with IDE.
Before you proceed:
  • Check that Visual Studio 2019 is supported with your MATLAB release. Note that:
    • Visual Studio 2019 family has been supported since R2019a.
  • The Intel Parallel Studio XE Fortran compilers are the only supported Fortran compilers for Windows.
    • After installing Intel Parallel Studio XE Fortran it will appear as an add-on in Visual Studio.
What follows is a guideline on how to build a Fortran Engine program using Visual Studio 2019 using the "fengdemo.F" source code as an example.
1.  Open Microsoft Visual Studio and select File > New > Project. Select Fortran from the All languages dropdown menu, then select Empty Project and Next.
2. Customize your Project name, Location, and Solution Name, then select Create.
3. In the Solution Explorer, right-click Source Files, then select and Add > New Item. Select Fortran Fixed-form File (.for), enter fengdemo.for as the name of the file and Add.
4. Copy the code from the source file: fengdemo.F, and paste it into the new file. This file may be obtained from matlabroot/extern/examples/eng_mat,where matlabroot is the MATLAB root directory, which may be determined by entering the following command:
>> matlabroot
at the MATLAB command prompt. You can learn more about this code at Call MATLAB Functions from Fortran Applications.
5. In the Solution Explorer, right-click on the project name and select Properties.  Make the following changes:
a. In the upper right corner, select Configuration Manager and change Active solution platform: to x64.
b. If this option is not listed, select <New...>
In the New Solution Platform window, select x64 and OK.
In the Configuration Manager select OK and Close.
c. Select Configuration Properties > Debugging > Environment and add PATH=matlabroot\bin\win64;%PATH%
d. Select Configuration Properties > Fortran > General > Additional Include Directories and add matlabroot\extern\include
e. Select Configuration Properties > Fortran > Preprocessor > Preprocess Source File and select Yes (/fpp)
f. Select Configuration Properties > Linker > General > Additional Include Directories and add matlabroot\extern\lib\win64\microsoft
g. Select Configuration Properties > Linker > Input > Additional Dependencies and add libmx.lib libeng.lib
h. Select OK to exit Property Pages.
6. Build the solution using Build > Build Solution.
7. To run the solution from Visual Studio, you must first check that MATLAB is registered as a COM Server .