从c++调用FORTRAN库的问题
Problems calling FORTRAN library from c++
我在c++中调用FORTRAN子程序时遇到问题,当函数放入用" arrcs "创建的库中时。
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. 90"o tt。和g++ tt。o testcpp。testcpp - lfortran"生成一个工作的可执行文件";0 - 0 testcpp - lfortran "崩溃
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
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 混合C++和 Fortran 链接问题
- 从Fortran调用C++(链接问题?)
- C++与Fortran混合程序的Makefile编译问题
- 从c++调用FORTRAN库的问题