从c ++调用FORTRAN库的问题 [英] Problems calling FORTRAN library from c++

查看:206
本文介绍了从c ++调用FORTRAN库的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



FORTRAN函数(tt.f90)是一个函数,是:
$ b $ pre $ 模块A
包含
子例程SubIF2(ii)
整数* 8, Intent(In):: ii
write(*,*)hello,ii
End Subroutine SubIF2
End Module A

一个c ++调用者(testcpp.cpp)代码是

 #包括< iostream> 
使用namespace std;
externC{
void __a_MOD_subif2(long long int * ii);
}
main(){
long long int ii = 5;
__a_MOD_subif2(& ii);
返回0; Fortran调用者(testf.f90)代码是
$ b



 程序测试
使用A
整数* 8 :: i = 1
调用SubIF2(i)
结束程序测试

makefile是

 $全部
gfortran -c tt.f90 $ b $ r $ 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:在函数`main'中:
testcpp.cpp :( .text + 0x18):未定义的对__a_MOD_subif2的引用
collect2:error:ld返回1退出状态

由于链接对fortran可执行文件有效,我在图书馆创建中看不到任何错误。



任何想法我在这里丢失??



谢谢很多。

注意:最终的fortran函数都是二进制的,所以调整Fortran代码(例如

解决方案

您必须在使用其中定义的符号的对象文件之后指定库,即,

  g ++ -o testcpp testcpp.o -L。 -ltt -lgfortran 


I experience problems calling a FORTRAN subroutine from C++ when the function is put into a library created with "ar rcs".

The FORTRAN routine (tt.f90) is:

Module A
contains
  Subroutine SubIF2(ii)
    Integer*8, Intent(In) :: ii
    write(*,*) "hello", ii
  End Subroutine SubIF2
End Module A

A c++ caller (testcpp.cpp) code is

#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;
}

A fortran caller (testf.f90) code is

Program test
  use A
  integer*8 :: i=1
  call SubIF2(i)
End Program test

the makefile is

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

While the "gfortran -o testf90 testf.o tt.a" and "g++ tt.o testcpp.o -o testcpp -lgfortran" yield a working executable, "g++ -L$(p) -ltt testcpp.o -o testcpp -lgfortran" crashes

testcpp.o: In function `main':
testcpp.cpp:(.text+0x18): undefined reference to `__a_MOD_subif2'
collect2: error: ld returned 1 exit status

Since linking works for the fortran executable, I cannot see anything wrong in the library creation.

Any idea what I missing here??

Thanks a lot.

Note: the final fortran function will all be binary, so adjusting the fortran code (e.g. iso_c_binding) is not an option.

解决方案

you have to specify the library after the object file which uses a symbol defined in it, i.e.,

g++ -o testcpp testcpp.o -L. -ltt -lgfortran

这篇关于从c ++调用FORTRAN库的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆