Fortran subroutine in a c++ code

Fortran subroutine in a c++ code

Hello,

I am trying to call fortran subroutine in a c code. I have Microsoft visual studio and intel visual fortran composer XE. 

When I right click on my fortran file then property. I see Item Type: not in the generation. And I don't see any fortran type file.

How to successfully compile? The intel c + + compiler is required?

My error is "error LNK2019: symbole externe non résolu _FACT@4 référencé dans la fonction _main" for the file main.obj

I used an exemple of microsoft website: 

/*     File CMAIN.C   */

#include <stdio.h>

extern int __stdcall FACT (int n);
extern void __stdcall PYTHAGORAS (float a, float b, float *c);

main()
{
    float c;
    printf("Factorial of 7 is: %d\n", FACT(7));
    PYTHAGORAS (30, 40, &c);
    printf("Hypotenuse if sides 30, 40 is: %f\n", c);
}

C    File FORSUBS.FOR
C
      INTEGER*4 FUNCTION Fact (n)
      INTEGER*4 n [VALUE]
      INTEGER*4 i, amt
      amt = 1
      DO i = 1, n
        amt = amt * i
      END DO
      Fact = amt
      END

      SUBROUTINE Pythagoras (a, b, c)
      REAL*4 a [VALUE]
      REAL*4 b [VALUE]
      REAL*4 c [REFERENCE]
      c = SQRT (a * a + b * b)
      END

source

Please help me!!

Thank you in advance.

 

6 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

You don't require Intel C++, but you must match the calling ABI.  stdcall was obsoleted over a decade ago, but (for 32-bit mode), ifort option /iface:cvf provides some compatibility (should work if your build procedure assumed the DEC or Compaq Windows Fortran).  

The usual reason for /iface:cvf is for compatibility with win32 ABI calls; if you don't use those, the default ABI of MSVC and ifort would be preferable.  You appear to have observed the ifort default of uppercase symbols without underscoring, so it looks like your build may follow CVF convention.

If you want to use the 32-bit Stdcall convention to call Fortran subroutines from C, compile the Fortran sources with the /iface:cvf option.

There is an entire chapter on mixed language programming in the Intel Fortran user manual.

ifort /MD /c /iface:cvf fsub.f
cl /MD cmain.c fsub.obj

You may use the Microsoft Visual C compiler or the Intel C compiler for the C part of your code.

You need to create separate projects for your C and Fortran files. It's OK to use MSVC, it doesn't need to be Intel C++. So an MS C/C++ project and an Intel Visual Fortran project. For this simple C/Fortran example, it's easier to start from empty projects.

There is more information in the Fortran Users Guide, see the section on Mixed Language Programming / Platform Specifics / Building Intel(R) Fortran/C Mixed-Language Programs (Windows* OS)

With the Intel Visual Fortran Compiler, it's simpler not to declare the Fortran functions as __stdcall on the C side. If you use the Fortran project for linking, then both C and Fortran libraries should be linked in automatically.

You can also practice by building the little MS example you quote from the command line. You can then easily run Dumpbin/symbols on the object files, to make sure that the symbols match.

See also the "C_calls_Fortran" solution from MixedLanguage.zip in the Samples/en_US/Fortran directory in the product.

Thank you to everyone.

The goal is to run a program made of fortran and c++ file. The program already run but in another OS ubuntu and another IDE eclipse.

I'll try with the included file in the Fortran directory. And I come back to you if I succeed or I stuck again

 

Laisser un commentaire

Veuillez ouvrir une session pour ajouter un commentaire. Pas encore membre ? Rejoignez-nous dès aujourd’hui