FORTRAN混合編程筆記

FORTRAN混合編程筆記

一、C++調用FORTRAN靜態連接庫
  • 首先將FORTRAN編譯成靜態連接庫(***.lab)文件;
  • 在VS中建立一個C++工程文件
  • 建立一個f2c.h頭文件,主要目的是將FORTRAN的變量類型和C++的變量類型作一個轉換
/* f2c.h  --  Standard Fortran to C header file */

/**  barf  [ba:rf]  2.  "He suggested using FORTRAN, and everybody barfed."

    - From The Shogakukan DICTIONARY OF NEW ENGLISH (Second edition) */

    /* 2008 Feb 18  James M Anderson  --MPIfR  --edit the standard f2c.h header
                                        to select just what is needed for basic
                                        FORTRAN interaction in C, as the standard
                                        f2c.h is incompatible with C++ <vector> */

#ifndef F2C_INCLUDE
#define F2C_INCLUDE

typedef int integer;
typedef unsigned int uinteger;
typedef char *address;
typedef short int shortint;
typedef float real;
typedef double doublereal;
typedef struct { real r, i; } complex;
typedef struct { doublereal r, i; } doublecomplex;
typedef int logical;
typedef short int shortlogical;
typedef char logical1;
typedef char integer1;
#ifdef INTEGER_STAR_8   /* Adjust for integer*8. */
typedef long long longint;      /* system-dependent */
typedef unsigned long long ulongint;    /* system-dependent */
#endif

/* Extern is for use with -E */
#ifndef Extern
#define Extern extern
#endif

/* procedure parameter types for -A and -C++ */



#endif /* F2C_INCLUDE */
  • 在函數聲明頭文件中開頭和結尾使用如下宏定義
#ifdef __cplusplus
extern "C" {
#endif
    extern 函數返回類型 函數名(函數參數);
    ...
#ifdef __cplusplus
}
#endif
  • 在函數定義文件前添加以下宏定義,用於調用靜態連接庫中的子函數時使用。
#ifndef FTN_NAME
#define FTN_NAME(a)                               a
#endif

如須要調用靜態連接庫中一個名爲iristec的函數時則能夠寫成FTN_NAME(iristec)();c++

  • 在進行函數傳值的時候必定要注意原FORTRAN程序中變量的類型,若是是指針則要傳變量地址進去。
  • 注意函數定義文件的文件後綴必須是.c,不然沒法編譯成功。
二、C++編譯動態連接庫(***.DLL)
  • 函數聲明時前面加上__declspec(dllexport)
相關文章
相關標籤/搜索