Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

How to include c-file in Fortran code?

fortran_eli
Beginner
1,811 Views
Hi,
I'm a Fortran-newbie and need to include a file 'mat.h' in my Fortran code (ifort with Visual Studio). This file is provided by Mathworks to enable Fortran to commands for generating Matlab data-files (.mat).
'mat.h' ist expilcitly made to be included in C or Fortran code. I'm sure that in C it won't be to hard to do so. But with Fortran i get "bad precompiler" errors.
So my question is how to make ifort compile the mixed code properly?
Thank you very much and best regards,
Eli
0 Kudos
4 Replies
TimP
Honored Contributor III
1,811 Views
Quoting - fortran_eli
Hi,
I'm a Fortran-newbie and need to include a file 'mat.h' in my Fortran code (ifort with Visual Studio). This file is provided by Mathworks to enable Fortran to commands for generating Matlab data-files (.mat).
'mat.h' ist expilcitly made to be included in C or Fortran code. I'm sure that in C it won't be to hard to do so. But with Fortran i get "bad precompiler" errors.
So my question is how to make ifort compile the mixed code properly?
Thank you very much and best regards,
Eli
You'll need to refer to their documentation. The file which is included in Fortran must have Fortran syntax, so it can't be the same one which you include in C.
By the way, there are 2 common modes of include files in Fortran. There is the Fortran standard INCLUDE which doesn't take effect during pre-processing, and the cpp style #include which is expanded during pre-processing (a de facto standard, not a Fortran standard). So it is doubly important that you follow their examples.
0 Kudos
Steven_L_Intel1
Employee
1,811 Views

It is possible that they provide two different files named mat.h, one for Fortran and one for C. Would you please attach the mat.h you think is being used? If it mostly looks like Fortran but has lines starting with #, you need to enable the preprocessor in the Fortran project properties, Preprocessor property page.
0 Kudos
IanH
Honored Contributor III
1,811 Views
Quoting - fortran_eli
Hi,
I'm a Fortran-newbie and need to include a file 'mat.h' in my Fortran code (ifort with Visual Studio). This file is provided by Mathworks to enable Fortran to commands for generating Matlab data-files (.mat).
'mat.h' ist expilcitly made to be included in C or Fortran code. I'm sure that in C it won't be to hard to do so. But with Fortran i get "bad precompiler" errors.
So my question is how to make ifort compile the mixed code properly?
Thank you very much and best regards,
Eli

mat.h is only for C or C++ programs - it contains C source code such as function and type declarations that a Fortran compiler would not understand.

The name of the file you #include for fortran sources is "fintrf.h". This file only contains preprocessor "instruction" lines, such as "#define MWPOINTER INTEGER*4". You will need to enable the preprocessor in Intel Fortran in order to compile source files that have the relevant #include "fintrf.h" line.

Your Matlab installation and documentation includes a number of fortran source code examples.

(Rant about the lack of a modern Fortran interface into Matlab (almost) suppressed).

0 Kudos
fortran_eli
Beginner
1,811 Views
Quoting - IanH
Thank you all for your quick and very professional answers to my question.
The inclusion of the right file for Fortran code fintr.h (instead of mat.h which is for C) and setting up the ifort preprocessor and the linker in MS Visual Studio (switch on preprocessor, add directories of additional MS and Matlab libraries, add addit. dependencies) finally solved it. Mathworks really should improve their Matlab documantation in that topic - it was no help at all but confusing.
Cheers


0 Kudos
Reply