从调用FORTRAN C $ C $Ç [英] Calling C Code from FORTRAN
本文介绍了从调用FORTRAN C $ C $Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于微软FORTRAN 5.1和Microsoft C / C ++ 14.0,使用随该版本FORTRAN的连接器沿(即必须用于其他依赖)我如何创建一个C函数,从FORTRAN应用程序调用它呢?
Given Microsoft FORTRAN 5.1 and Microsoft C/C++ 14.0, along with the linker that comes with that version of FORTRAN (that must be used for other dependencies) how do I create a C function and call it from the FORTRAN application?
推荐答案
您有两种选择。
1)我可以举例说明
You have two choices.
1) I can show with example
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 ++
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工具。你必须DUMPBIN对DLL和Fortran对象code,看是否正确名称匹配。
另一种方法是相似的:
This works w/gcc toolchain. You'll have to dumpbin on the DLL and fortran object code to see if the names match up correctly.
The other way is similar:
//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
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 ++
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/>
希望这有助于
这篇关于从调用FORTRAN C $ C $Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文