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

mixed-language (fortran calls C)

jfons
Beginner
612 Views
Hi,
I have a fortran subroutine that needs to call a C function. I'm using Intel Fortran Compiler Integration for Microsoft Visual Studio .NET 2003, version 8.1. I've looked all throughIntel's online documentation and searched the internet as well, but there is very limited supporton mixed-language integration for .NET. If anyone knows the process to call my C function from my fortran subroutine, I would really appreciate the help
Thanks,
Jaime
0 Kudos
11 Replies
jim_dempsey
Beginner
612 Views
For samples of FORTRAN calling C take a look at the ArrayVisualizer source code. I know this is not a tutorial but a lot can be gleaned from looking at other code that works.
Jim Dempsey
0 Kudos
Steven_L_Intel1
Employee
612 Views
The User Manual's chapter on mixed-language programming tells you what you need to know to declare the C routines properly and pass arguments. If you are using the VS.NET environment, you have to create a separate C++ static library project for your C code and set the project dependencies so that the library is a subproject of your Fortran application project.
0 Kudos
jfons
Beginner
612 Views
Thanks a lot for all your help. Just a couple obvious follow up questions:
Steve, I've been searching intel's site for the User's Manual and I've had no luck. Do you have a link to where I can find it? (I don't have a physical copy - the Intel Fortran Compiler is imaged on my work computer (CSC)).
Jim, the ArrayVisualizer source code? I would LOVE to check it out. What is it? Where is it? I searched all over intel's site and ran a couple google searches trying to find it. Please, let me know whereit is.I strongly agree with you: a lot can be gleaned from looking at other code that works. Looking at working code probably helps me more than a tutorial.Just please post some more detail so I can find out what and where ArrayVisualizer is.
Thanks again,
Jaime
0 Kudos
Steven_L_Intel1
Employee
612 Views
The Intel Fortran documentation for 8.1 is found by selecting Start..Programs..Intel Software Development Tools..Intel Fortran Compiler 8.1..Documentation Index. In 9.0, it's there and also listed under Help in Visual Studio.NET. On the web, the manuals are here, found as the Product Manuals link on the Intel Visual Fortran product page.
0 Kudos
jfons
Beginner
612 Views
Steve - Thank You. That is exactly what I was looking for.
0 Kudos
jbalster
Novice
612 Views

These instruction are for mixing C/C++ with F77. I have never used F90 so I don't know anything about it.

What I didn't see anyone post yet was some specific details on how to accomplish this. Now my case is the inverse, C/C++ calling F77, but I would expect it to be the same calling problems.

First, there are multiple calling conventions to choose from plus external name naming conventions. My projects are specifying that the external names are all uppercase. Another thing to look out for is passing of CHARACTER datatype. That is passed as 2 arguments, instead of one, the 2nd one being hidden, so I tell the compilier to put the hidden arguments AFTER the other arguments.

My F77 debug switches for IVF 9.0 are (yes, you asked about IVF 8.1, but I'm at 9.0, so I'm showing what I know):

Code:
Intel Visual Fortran 9.0
/nologo /Zi /Od /include:"." /include:".." /include:"...." /f77rtl /intconstant /warn:ignore_loc /warn:truncated_source /Qsave /assume:dummy_aliases /Qzero /names:uppercase /iface:cref /module:"./" /object:"./" /traceback /check:none /libs:static /threads /debug:full /dbglibs /c 


Microsoft C++ .NET 2003
/Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_SIMSYS_" /D "_ATL_STATIC_REGISTRY" /D "_MBCS" /FD /EHsc /MTd /GS /GR /Fp".Debug/codeMMSim.pch" /Fo".Debug/" /Fd".Debug/" /FR".Debug/" /W3 /nologo /c /Z7 /D WINVER="0x0400"


Linker
/OUT:"Debug/myprogram.exe" /INCREMENTAL /NOLOGO /NODEFAULTLIB:"msvcrtd.lib" /NODEFAULTLIB:"msvcprtd.lib" /DELAYLOAD:"OleAcc.dll" /DEBUG /PDB:".Debug/myprogram.pdb" /SUBSYSTEM:WINDOWS /MACHINE:X86 nafxcwd.lib myprogram.lib DelayImp.lib DelayImp.lib

Now your library and include paths will probably differ (I removed mine from this example as you won't have my libraries)

You may also have other switches different from me. For example, I am using /Qsave and /Qzero to initialize all my local variables to zero and make them static as if there was a SAVE statement in each routine.

Don't forget, FORTRAN likes to pass values other than CHARACTER by reference and array subscripts are reversed.

If you are calling FORTRAN fromC++ instead of C, then the header file defining the call needs to be wrapped in the following constructs:

Code:
#ifdef __cplusplus
extern "C" {
#endif

 . . . your function declarations preceeded with extern

#ifdef __cplusplus
}
#endif
Without this, C++ performs name mangling so that the linker cannot match up the routine names.
0 Kudos
Steven_L_Intel1
Employee
612 Views
In this context, it does not matter if the code is F77, F90, F95 or even F66....
0 Kudos
jim_dempsey
Beginner
612 Views
Jaime,
When you install Visual Fortran you have an option to install the Array Visualizer. Re-run the install to modify your installation to include the Array Visualizer code. On V9.0 this code will install into:
C:Program FilesIntelArray Visualizer
On V8.n the files were located in
C:Program FilesIntelFortranArray Visualizer
This "free-bee" saved me a ton of work in displaying 3D graphs during a simulation run of a finite element analysis. Some quirks to learn in how to use it but it was sure worth the time and effort to hook it into the application.
In my case the FE simulator was written as a consol program. Instead of rewriting it as a WinApp I simply hooked in the Array Visualizer (plus a little dialog box) and ta-da the consol app now has a graphical interface. Some buttons to push and a very nice 3D image of the system under test.
Jim Dempsey
0 Kudos
stefan_pantos
Beginner
612 Views
What is the correct way to pass strings to a c function from fortran? I have in interface and I want to use an assumed length string but it keeps telling me about having another argument. Say my c function is:

void func_name(char* string1, char* string2);

how would i write the interface for it? I was trying something like this

INTERFACE
SUBROUTINE func_name(string1, string2)
!DEC$ ATTRIBUTES C :: func_name
CHARACTER STRING1(*)
CHARACTER STRING2(*)
END SUBROUTINE
END INTERFACE

But it doesn't like this. I realise it's the string length which is the problem but I don't know what the solution is.

Thanks in advance,
Stefan
0 Kudos
Lorri_M_Intel
Employee
612 Views
INTERFACE
SUBROUTINE func_name(string1, string2)
!DEC$ ATTRIBUTES C :: func_name
CHARACTER (*) STRING1
!DEC$ ATTRIBUTES REFERENCE :: STRING1
CHARACTER (*) STRING2
!DEC$ ATTRIBUTES REFERENCE :: STRING2
END SUBROUTINE
END INTERFACE
Move the "(*)" after the CHARACTER not after the name, and give the arguments the 'reference' attribute.
With this particular declaration, the size is not passed. C doesn't care as long as you null-terminate the strings you are passing along. Fortran does NOT do that automatically for you.
- Lorri
0 Kudos
TimP
Honored Contributor III
612 Views
Until f2003 C compatibility is implemented, you must rely on the methods in the documentation which comes with the compiler. The usual method requires showing the implied string length parameters explicitly in the C function, even if you go to the trouble of inserting a CHAR(0) terminator in the string.
0 Kudos
Reply