[英]Problems calling FORTRAN library from c++
將函數放入使用“ ar rcs”創建的庫時,從C ++調用FORTRAN子例程時遇到問題。
FORTRAN例程(tt.f90)是:
Module A
contains
Subroutine SubIF2(ii)
Integer*8, Intent(In) :: ii
write(*,*) "hello", ii
End Subroutine SubIF2
End Module A
一個C ++調用程序(testcpp.cpp)代碼是
#include <iostream>
using namespace std;
extern"C" {
void __a_MOD_subif2(long long int *ii);
}
main(){
long long int ii=5;
__a_MOD_subif2(&ii);
return 0;
}
fortran調用者(testf.f90)代碼為
Program test
use A
integer*8 :: i=1
call SubIF2(i)
End Program test
該makefile是
p=/PathToMyWorkDirectory
all:
gfortran -c tt.f90
ar rcs libtt.a tt.o
g++ -c testcpp.cpp
gfortran -c testf.f90
-gfortran -o testf90 testf.o tt.a
-g++ tt.o testcpp.o -o testcpp -lgfortran
-g++ -L$(p) -ltt testcpp.o -o testcpp -lgfortran
clean:
-rm *.o *.mod
-rm testf90
-rm testcpp
盡管“ gfortran -o testf90 testf.o tt.a”和“ g ++ tt.o testcpp.o -o testcpp -lgfortran”會生成一個可運行的可執行文件,而“ g ++ -L $(p)-ltt testcpp.o -o testcpp -lgfortran”崩潰
testcpp.o: In function `main':
testcpp.cpp:(.text+0x18): undefined reference to `__a_MOD_subif2'
collect2: error: ld returned 1 exit status
由於鏈接適用於fortran可執行文件,因此在庫創建中我看不到任何錯誤。
知道我在這里錯過了什么嗎?
非常感謝。
注意:最終的fortran函數將全部為二進制,因此不能選擇調整fortran代碼(例如iso_c_binding)。
您必須在目標文件后指定使用該庫中定義的符號的庫,即
g++ -o testcpp testcpp.o -L. -ltt -lgfortran
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.