Microsoft FORTRAN 5.1 および Microsoft C/C++ 14.0 と、そのバージョンの FORTRAN に付属するリンカー (他の依存関係に使用する必要があります) がある場合、C 関数を作成して FORTRAN アプリケーションから呼び出すにはどうすればよいですか?
4256 次
3 に答える
3
ここにサンプル コードを含む MSDN の記事があります: C への FORTRAN 呼び出し
于 2008-12-09T23:37:21.917 に答える
1
2つの選択肢があります。
1)例を挙げて説明できます
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 <l;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
これはgccツールチェーンで動作します。名前が正しく一致するかどうかを確認するには、DLLとFortranオブジェクトコードでdumpbinを実行する必要があります。
他の方法も同様です。
//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
コンパイルコマンド
$>g++ -c ctest.c <br/>
$>gfortran -c ftest.f90 <br/>
$>gfortran *.o -lstdc++ -o test_prog <br/>
お役に立てれば
于 2009-02-27T13:19:04.570 に答える
0
遅刻しないよりはましかもしれません。Visual Studio 1 で ms fortran 5.1 を使用したことを覚えています。どの Visual Studio 14.0 が相当するかはわかりませんが、比較的最近のもので、13 年前のものではないと思います。5.1 は Windows 3 用の 16 ビット コンパイラであるため、この組み合わせは初心者向けではないと思います。
于 2009-07-31T17:06:50.293 に答える