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

Calling Fortran from C++

Scott_Hill
Beginner
1,308 Views
I am porting a program from MS Visual Studio 6 and CVF to MS Visual Studio 2010 and Intel Visual Fortran. Previously I used a .def file to share data between a C++ calling program and a Fortran DLL.Can I use a .def with VS 2010 and IVF? and if so can some one give me some directions. An example would be great. Thank you. Scott
0 Kudos
9 Replies
Steven_L_Intel1
Employee
1,308 Views
You certainly can if you wish - it's pretty much the same as with CVF, except that your C++ code has to go in a separate project. Just add the .DEF file to the Fortran project as a source file. Do be aware that CVF and Intel Fortran use different default calling and naming conventions.
0 Kudos
Scott_Hill
Beginner
1,308 Views
Steve,

Thank you for the reply.

How do I find the default calling and naming convention used by Intel Fortran? I have looked around, but I can't find anything.

Thank you.

Scott


0 Kudos
mecej4
Honored Contributor III
1,308 Views
See the Fortran documentation provided by Intel. There is an entire chapter on "Mixed Language Programming", and there are tables that lay out the calling conventions and match data types from C to Fortran.

In brief, Intel Fortran uses the "Cdecl" calling convention with string argument lengths passed at the end of the explicit argument list.

There is also a useful article: Migrating from Compaq-Visual Fortran that is worthwhile to consult.

I hope that you are aware that mixing C++ and Fortran is much harder than mixing C and Fortran.
0 Kudos
TimP
Honored Contributor III
1,308 Views
Default calling conventions of VS2010 (cdecl) are consistent with ifort. The portable way, for which you will find many examples, is with extern "C" in C++ and USE iso_c_binding in Fortran. This drops the use of string argument lengths.
0 Kudos
Carlos_C_7
Beginner
1,308 Views

Hi All,

I have a Fortran Code and I'd like to build my application into an Interface with C++ (or C#) by using Visual Studio 2008. I want to display my results from Fortran in one window by using C++ or C#. My question is: Which one is the best to do the communication with I/O files from Fortran?   (I use Intel Visual Fortran 11)

Thank you to all!

Carlos

0 Kudos
TimP
Honored Contributor III
1,308 Views

Ideally, C++ or C# would look the same on the Fortran side.  I'd be surprised if anyone has tried this comparison back to back.  I hope you don't make implementation decisions specific to these no longer supported releases of VS or ifort.

0 Kudos
Carlos_C_7
Beginner
1,308 Views

Thank you Tim!

But I didn´t understand when you said "no longer supported...". Is there something particular point that should I do in relation to VS2008? My decision in using VS2008 is relationed about money (I can not do an upgrade at the moment), I've been used VS2008 with VFComposer XE2011 since a few years ago.

0 Kudos
mecej4
Honored Contributor III
1,308 Views

You referred to VS2008 and IFort XE2011. Current versions are VS2013 and IFort XE2015.

Your question "C++ or C#. My question is: Which one is the best to do the communication with I/O files from Fortran?" does not make sense to me. If by "I/O files from Fortran" you mean unformatted Fortran files or some other file format specific to Fortran, you cannot read them in C++ or C# without writing code to handle low level stuff such as record markers, record length prefixes and suffixes, etc. Normally, C/C# code would communicate with Fortran subprograms, not files. Those Fortran subprograms could, of course, do I/O with Fortran unformatted files.

If, on the other hand, you mean text files, then there is nothing to distinguish text files written by a Fortran program from a text file written by any program written in any language.

0 Kudos
TimP
Honored Contributor III
1,308 Views

Files written and read with access='stream', as well as formatted sequential text files, are meant to be portable across programming languages.

If you can't afford VS2013 Professional, you probably qualify to use VS2013 Community, which works with ifort 2015 (unless possibly you have a discontinued version of Windows).

0 Kudos
Reply