科學計算領域常將核心演算法用 Fortran 編寫(數值計算效能優、陣列操作直觀),再編譯為 DLL 供 VC++ 主程式呼叫。本文完整記錄從 Fortran 建立 DLL 到 VC++ 專案整合、呼叫慣例匹配、除錯要點的全流程。
test1。test1.f90)! test1.f90 - 匯出函數供 C/C++ 呼叫
! 使用 C 呼叫慣例,避免 Fortran 名稱修飾
! --- 無回傳值的子程式 ---
subroutine test1(a, b)
!DEC$ ATTRIBUTES C, DLLEXPORT :: test1
integer, intent(in) :: a, b
integer :: sum
sum = a + b
! 結果可透過引數回傳或寫入全域/模組變數
return
end subroutine test1
! --- 有回傳值的整數四則運算 ---
function add(a, b) result(res)
!DEC$ ATTRIBUTES C, DLLEXPORT :: add
integer, intent(in) :: a, b
integer :: res
res = a + b
end function add
function subtract(a, b) result(res)
!DEC$ ATTRIBUTES C, DLLEXPORT :: subtract
integer, intent(in) :: a, b
integer :: res
res = a - b
end function subtract
function multiply(a, b) result(res)
!DEC$ ATTRIBUTES C, DLLEXPORT :: multiply
integer, intent(in) :: a, b
integer :: res
res = a * b
end function multiply
function divide(a, b) result(res)
!DEC$ ATTRIBUTES C, DLLEXPORT :: divide
integer, intent(in) :: a, b
integer :: res
if (b == 0) then
res = 0 ! 或設錯誤碼
else
res = a / b
end if
end function divide
!DEC$ ATTRIBUTES C, DLLEXPORT :: func_name:
C:使用 C 呼叫慣例(參數由右至左壓棧、呼叫者清理棧、函數名不修飾)。DLLEXPORT:匯出符號至 DLL。test1.dll、test1.lib(導入庫)、test1.obj。使用 dumpbin /exports test1.dll 或 Dependency Walker 確認匯出名稱完全符合 Fortran 定義的小寫名稱(test1, add, subtract…),未被轉為大寫或加底線。

checktest1。test1.dll、test1.lib(或 test1.obj)。test1.dll、test1.lib 複製到專案的 Debug/ 目錄(或輸出目錄),否則執行時會提示「找不到 DLL」。#include "stdafx.h"
#include
using namespace std;
// 1. 宣告外部函數:extern "C" 防止 C++ 名稱修飾
// __cdecl 對應 Fortran !DEC$ ATTRIBUTES C
// 參數為「值傳遞」int(對應 Fortran integer, intent(in))
// 注意:Fortran 預設傳址,但加上 C 屬性後改為傳值!
extern "C" __cdecl void test1(int a, int b);
extern "C" __cdecl int add(int a, int b);
extern "C" __cdecl int subtract(int a, int b);
extern "C" __cdecl int multiply(int a, int b);
extern "C" __cdecl int divide(int a, int b);
int _tmain(int argc, _TCHAR* argv[])
{
int x = 10, y = 5;
// 呼叫無回傳值子程式
test1(x, y); // Fortran 端僅計算 sum,若需取回需改用指標/引用
// 呼叫有回傳值函數
cout << "add: " << add(x, y) << endl; // 15
cout << "subtract: " << subtract(x, y) << endl; // 5
cout << "multiply: " << multiply(x, y) << endl; // 50
cout << "divide: " << divide(x, y) << endl; // 2
return 0;
}
| 項目 | Fortran 端 | VC++ 端 | 說明 |
|---|---|---|---|
| 呼叫慣例 | !DEC$ ATTRIBUTES C |
extern "C" __cdecl |
雙方必須一致,否則棧平衡錯誤導致崩潰 |
| 函數名稱 | 小寫 add |
小寫 add |
C 慣例不修飾名稱,區分大小寫 |
| 整數參數 | integer, intent(in) :: a |
int a (傳值) |
Fortran 預設傳址,加 C 屬性後改傳值 |
| 回傳值 | function ... result(res) |
int func(...) |
整數/浮點數直接回傳;結構體需傳指標 |
| 字串 | character(len=*) + 隱藏長度參數 |
const char* + 額外長度參數 |
跨語言傳字串極易出錯,建議用定長緩衝區 |
| 陣列 | real, dimension(*) :: arr |
float* arr + 維度資訊 |
Fortran 陣列欄優先,C 列優先,傳遞需轉置或調整索引 |
| 錯誤現象 | 原因 | 解決 |
|---|---|---|
連結錯誤 LNK2019:無法解析的外部符號 _add |
||
| 執行時崩潰 / 參數值亂碼 | ||
| 執行時提示找不到 test1.dll | ||
| 除數為零未處理 |
float* + 維度參數,並在 Fortran 用 CFI_cdesc_t (Fortran 2003 C Interop) 互操作。BIND(C) 定義互通結構,確保記憶體佈局一致(#pragma pack / SEQUENCE)。PROCEDURE 指標呼叫。VC++ 呼叫 Fortran DLL 的核心在於呼叫慣例統一(C 慣例)、符號名稱精確對應、參數傳遞語義匹配(傳值/傳址)。遵循本文步驟與對照表,即可穩健跨語言整合,發揮 Fortran 數值計算優勢與 VC++ 介面/架構優勢的雙重紅利。