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

F90 calling C vs Cpp

rahzan
Novice
632 Views
using cvf, from the sample files I have found this example of f90 calling C:
The c-routine is:
void pythagoras (float a, float b, float *c)
{*c = (float) (a*a + b*b); }
With the f90-interface:
MODULE CPROC
INTERFACE
SUBROUTINE PYTHAGORAS (a, b, res)
!DEC$ ATTRIBUTES C:: PYTHAGORAS
!DEC$ ATTRIBUTES REFERENCE :: res
REAL a, b, res
END SUBROUTINE
END INTERFACE
END MODULE
the f90 caller is:
USE CPROC
CALL PYTHAGORAS (3.0, 4.0, X)
TYPE *,X
read(*,*)
END
the question#1 is:
If I put the c-rouine in a file ending in .C it works, if it goes in a .CPP it says it cannot find the routine _PYTHAGORAS. Can anyone point out the proper attribute settings for cpp, if any?
here is question #2:
I have the msc++ v6 installed, but I cannot tell ifthe above projectis using it or just the mini c-compiler which comes with cvf6.6.
And still #3:
I've used cpp on occasion (but not across languages) but did not even know the msc compiler treated c distinctly, Is there a place one canlookup the difference between c and cpp in msc6?
Thanks.
0 Kudos
7 Replies
Steven_L_Intel1
Employee
632 Views
The key is that C++ does "name mangling", so you must add "C" to the extern declaration in order to prevent that. This is mentioned, I am pretty sure, in the mixed-language programming chapter of the Intel Fortran User Manual.
0 Kudos
rahzan
Novice
632 Views
Nearly everything in the docs shows cpp calling F90 and not the reverse.
0 Kudos
Steven_L_Intel1
Employee
632 Views
Quoting from the CVF Programmer's Guide:

Visual C++ uses the same calling convention and argument-passing techniques as C, but naming conventions are different because of Visual C++ decoration of external symbols. When the C++ code resides in a .cpp file (created when you select C/C++ file from the visual development environment), C++ name decoration semantics are applied to external names, often resulting in linker errors. The extern "C" syntax makes it possible for a Visual C++ module to share data and routines with other languages by causing Visual C++ to drop name decoration.

The following example declares prn as an external function using the C naming convention. This declaration appears in Visual C++ source code:

extern "C" { void prn(); }

To call functions written in Fortran, declare the function as you would in C and use a "C" linkage specification. For example, to call the Fortran function FACT from Visual C++, declare it as follows:

extern "C" { int __stdcall FACT( int n ); }

The extern "C" syntax can be used to adjust a call from Visual C++ to other languages, or to change the naming convention of Visual C++ routines called from other languages. However, extern "C" can only be used from within Visual C++. If the Visual C++ code does not use extern "C" and cannot be changed, you can call Visual C++ routines only by determining the name decoration and generating it from the other language. Such an approach should only be used as a last resort, because the decoration scheme is not guaranteed to remain the same between versions.

Use of extern "C" has some restrictions:

You cannot declare a member function with extern "C".
You can specify extern "C" for only one instance of an overloaded function; all other instances of an overloaded function have Visual C++ linkage.
For more information on the extern "C" linkage specification, see the Microsoft Visual C++ Language Reference.

Message Edited by sblionel on 03-03-2005 04:54 PM

0 Kudos
rahzan
Novice
632 Views
Thanks very much.
Seems like staying with C is the simpler solution.
Now if I only can figure out what Cpp can do more ir betterthan C which might be worth the additional trouble.
If anyone can help on sources to read on C v CPPfor computational purposes it will be appreciated.
0 Kudos
g_f_thomas
Beginner
632 Views
FYI, Numerical Recipies is available in C and C++. It should be revealing to compare and contrast the two editions.

Even better is Daoqi Yang's "C++ and Object Oriented Numeric Computing for Scientists and Engineers,"
Springer-Verlag, New York, 2001, ISBN: 0-387-98990-0

HTH,
Gerry T.
0 Kudos
rahzan
Novice
632 Views

Thanks.

I will look those up.

But here is a quickie for any takers: is there an equivalent to the f90 modules in C?

0 Kudos
g_f_thomas
Beginner
632 Views
To some extent, .h files.

Predating Fortran90/95/2003, Pascal-XSC had modules, an operator concept, functions and operators of arbitrary result type, overloading of functions, procedures and operators, dynamic arrays, access to subarrays, rounding control by the user, and accurate evaluation of expressions and automatic verification of results (not part of F90/95/2003).

Ciao,
Gerry T.
0 Kudos
Reply