Domanda

Dati Microsoft FORTRAN 5.1 e Microsoft C / C ++ 14.0, insieme al linker fornito con quella versione di FORTRAN (che deve essere utilizzata per altre dipendenze) come posso creare una funzione C e chiamarla dall'applicazione FORTRAN?

È stato utile?

Soluzione

Hai due scelte.
1) Posso mostrare con esempio

FORTRAN

program ftest
use iso_c_bindings
implicit none
interface
function saythis(a) ! should be subroutine if saythis returns void
import :: c_ptr
type(c_ptr), value :: a
end function saythis
end interface

character(len=80), target :: str
type(c_ptr) cstr
integer :: r

str='Hello World From Fortran' // C_NULL_CHAR
cstr=c_loc(str(1:1))
r=saythis(cstr)

C / C ++

#ifdef __cpluscplus
#include &ltl;cstdio>
using namespace std;
#else
#inlcude <stdio.h>
#endif

#ifdef __GNUC__
#define FORT(func) func ## _
#else
#define FORT(func) __stdcall func ## _
#endif

#ifdef __cpluscplus
extern "C" {
#endif
__declspec(dllexport) int FORT(sayit)(char* c)
{
return printf("%s\n",c);
}

#ifdef __cpluscplus
}
#endif

Funziona con toolchain gcc. Dovrai eseguire il dumpbin sulla DLL e sul codice oggetto fortran per vedere se i nomi corrispondono correttamente.  
L'altro modo è simile:

//This is for gcc toolchain
//you'll have to find the symbol conversion yourself
//I think that Visual Studio converts fortran names
//to ALL CAPS so instead of func => _func you'll need func => FUNC

FORTRAN

program ftest
integer aa,bb,cc
common/vari/aa,bb,cc

aa=7
bb=11
cc=0
call dosomething
call dosomethingelse(aa,bb,cc)

C / C ++

#ifdef __cplusplus
extern "C" {
#endif
int _dosomething();
int _dosomethingelse(int*,int*,int*); //all fortran is pass by reference
struct { int aa,bb,cc; } vari;
#ifdef __cplusplus
}
#endif

//function def's go here
//struct vari should be the same memory as common/vari block

COMPILA COMANDO

$>g++ -c ctest.c <br/>
$>gfortran -c ftest.f90 <br/>
$>gfortran *.o -lstdc++ -o test_prog <br/>

Spero che questo aiuti

Altri suggerimenti

C'è un articolo su MSDN con codice di esempio qui: FORTRAN chiama in C

Forse meglio tardi che mai. Ricordo di aver usato MS Fortran 5.1 con Visual Studio 1. Non sono sicuro di quale Visual Studio 14.0 sia equivalente, ma penso che sia relativamente recente e non abbia 13 anni. Penso che questa combinazione non sia un avviatore in quanto 5.1 è un compilatore a 16 bit per Windows 3.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top