自訂函數 > 使用 Fortran > 在自訂函數中使用 Fortran
  
在自訂函數中使用 Fortran
建立 Fortran 程式庫
1. 在 Compaq Visual Fortran 6.6B 中建立新的 Fortran Static Library 專案,然後將您的檔案新增至專案。
2. 「Build」功能表下,按一下「Set Active Configuration」,然後選取「Release」
3. 編譯及建構。您的 Fortran 專案的 Release 子資料夾現在會包含 [myfortranlib].lib
修改自訂函數的 C 程式碼
當您擁有 Fortran 程式庫之後,必須在 C 程式碼中新增下列程式碼:
extern void __stdcall FORTRANFUNCTION(const double *array1, const double *scalar1 [, etc.]);
// Since FORTRAN typically expects arguments by reference, arguments are passed
// as pointers. To pass values, the FORTRAN code must contain a compiler directive
// telling the function to expect a value rather than an address.
LRESULT mcadfunction(LPCOMPLEXARRAY array1, LPCCOMPLEXSCALAR scalar1, etc.)

// this defines the function before FUNCTIONINFO, using the same variable names
// called by the external FORTRAN function.
{
// some error checking goes here, followed by the
// actual call to the FORTRAN function. For example,
FORTRANFUNCTION(&array1->hReal[0][0], &scalar1->real [, etc.]);

// Either the function call must be in UPPERCASE, or you will have to set
// Settings->FORTRAN->External procedures->External name implementation
// to "Upper case" in your FORTRAN compiler. Any other C functionality follows...
return 0;

}
連結程式庫
1. 在 MS Visual C++ 或 Visual Studio 現有的 C 專案中,選取「連結器」標籤,然後從「種類」功能表中選取「輸入」。在清單結尾的 Object/library modules 文字方塊中輸入 [myfortranlib].lib
2. 「Ignore libraries」文字方塊中輸入 libc.lib
3. 「Additional Library Path」文字方塊中輸入 [myfortranlib].lib 檔案的路徑,例如 C:\temp
4. 編譯及建構 DLL。
其他資訊
Fortran 程式碼應以獨立的執行緒形式來執行,而不是直接以函數形式來執行。如此一來,若終止流程 (例如按下 Esc),即可終止整個執行緒,而不需要確認哪個 Fortran 副程式目前正在執行中。
您可以使用特殊函數取代 Fortran 程式碼中的所有 STOP 語句,該特殊函數會將錯誤代碼傳回呼叫 C,以正常終止執行緒。
若 Fortran 程式碼包含 PRINT 至主控台語句,可將主控台重新指派給檔案進行轉換,以輸出至檔案,而不是螢幕。若 Fortran 常式終止並發生錯誤,可編寫 C++ front-end 程式讀取此記錄檔,並在視窗中顯示其內容。例如:
SUBROUTINE INIT_STDOUT ()
c this subro which redirects FORTRAN output to file
use dfwin
integer res

c CALL close_stdout
res=SETENVQQ("FOR_PRINT=C:\FORT_OUT.TXT")

c PRINT *, 'Print a character to initialize '
PRINT *,' '
RETURN
END
若想分送基於 Fortran 的 DLL,執行的電腦必須安裝適當的 Fortran 執行階段程式庫。您可以使用下列 Fortran 執行階段程式庫:
DFORRT.DLL
DFORRTD.DLL
DFORMD.DLL
DFORMDD.DLL
DFDLG100.DLL
MSVCRT.DLL
若缺少這些 DLL,就會產生錯誤。這些 DLL 會與您的 Fortran 編譯器封裝在一起。您必須在 Windows/system32/WINNT/system32/ 目錄中安裝這些 DLL。