سؤال

ونظرا مايكروسوفت FORTRAN 5.1 و Microsoft C / C ++ 14.0، جنبا إلى جنب مع رابط الذي يأتي مع هذا الإصدار من FORTRAN (التي يجب أن تستخدم لتبعيات أخرى) كيف يمكنني إنشاء دالة C والذي يطلق عليه من تطبيق FORTRAN؟

هل كانت مفيدة؟

المحلول

لديك خيارين.
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 &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

وهذا يعمل ث / toolchain دول مجلس التعاون الخليجي. سيكون لديك لDUMPBIN على DLL وفورتران رمز الكائن لمعرفة ما إذا تطابق الأسماء بشكل صحيح.  
والطريقة الأخرى هي مشابهة:

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

وCOMPILE COMMAND

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

ويساعد هذا الأمل

نصائح أخرى

وهناك مقال على MSDN مع نموذج التعليمات البرمجية هنا: FORTRAN يدعو إلى C

وربما متأخرا أفضل من عدمه. أتذكر باستخدام MS فورتران 5.1 مع استوديو البصرية 1. أنا لست متأكدا مما الاستوديو المرئي 14.0 ما يعادل ولكن أعتقد أنه حديث نسبيا وليس من العمر 13 عاما. وأعتقد أن هذا المزيج هو غير كاتب كما هو مترجم 5.1 16BIT للنوافذ 3.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top