C/C++, FORTRAN, underscores, and GNU Autotools

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 11:53:28

As long as you have a compiler newer then the past several years, I recommend using the ISO C Binding to mix Fortran with other languages. Then you can skip the name mangling with underscores and similar compiler/platform dependent issues. If you have legacy FORTRAN 77 code that you don't want to alter, you could write a small Fortran 2003 glue routine between the C and the FORTRAN 77. Older instructions describe the previous method which required more understanding of the internal interfaces and was more compiler/platform dependent. For the new method, look at the gfortran manual chapter "Mixed Language Programming" and previous questions/answers here.

With Fortran code it is easier to link with gfortran because that brings in the Fortran runtime libraries. I think that the same applies to C++, so if you have both you will have to explicitly include the runtime library of one or the other.

P.S. Here is an example using the Fortran ISO C Binding:

function NGCD (na, nb) bind (C, name="NGCD")
   use iso_c_binding
   implicit none
   integer (c_int) :: ngcd
   integer (c_int), intent (in) :: na, nb
   integer (c_int) :: ia, ib, itemp
   ia = na
   ib = nb

   do while (ib /= 0)
      itemp = ia
      ia = ib
      ib = mod(itemp, ib)
   end do

   ngcd = ia

   return
end function NGCD

Compile/link with:

gcc -c main.c
gfortran main.o ngcd.f90

I never got a sufficient answer, so I cobbled together a temporary solution (ignores case in the FORTRAN code) and posted it on my forufus blog. I will have to work out the compiler switches in the future.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!