문제

Microsoft Fortran 5.1 및 Microsoft C/C ++ 14.0과 함께 해당 버전의 FORTRAN (다른 종속성에 사용되어야 함)과 함께 제공되는 링커와 함께 C 함수를 작성하고 Fortran 응용 프로그램에서 호출하는 방법은 무엇입니까?

도움이 되었습니까?

해결책

당신은 두 가지 선택이 있습니다.
1) 예제로 보여줄 수 있습니다

포트란

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

이것은 GCC 툴체인과 함께 작동합니다. 이름이 올바르게 일치하는지 확인하려면 DLL 및 Fortran 객체 코드를 덤프해야합니다.
다른 방법은 비슷합니다.

//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

포트란

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/>

도움이 되었기를 바랍니다

다른 팁

MSDN에 샘플 코드가있는 기사가 있습니다. Fortran은 c

결코 늦지 않는 것보다 늦을 것입니다. 나는 Visual Studio 1과 함께 MS Fortran 5.1을 사용한 것을 기억합니다. 어떤 Visual Studio 14.0이 동등한 지 확실하지 않지만 비교적 최근이며 13 세가 아니라고 생각합니다. 5.1은 Windows 3의 16 비트 컴파일러 이므로이 조합이 비 스타터라고 생각합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top