Nп/п : 100 из 100
 От   : FortranFan                          2:5075/128        30 сен 23 09:53:10
 К    : db                                                    30 сен 23 19:56:02
 Тема : Re: Calling a C++ function from Forttran
----------------------------------------------------------------------------------
                                                                                 
@MSGID:
<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com> daeeece5
@REPLY: 1@dont-email.me> 8b6cbed6
@REPLYADDR FortranFan <parekhvs@gmail.com>
@REPLYTO 2:5075/128 FortranFan
@CHRS: CP866 2
@RFC: 1 0
@RFC-References: 1@dont-email.me>
1@dont-email.me>
@RFC-Message-ID:
<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com>
@TZUTC: -0700
@PID: G2/1.0
@TID: FIDOGATE-5.12-ge4e8b94
On Saturday, September 30, 2023 at 9:42:34 AM UTC-4, db wrote:

> On 04.09.2023 16.43, db wrote: 
> ..
> I askked this both here and in the C++ ng. I got a lot of different 
> advice, none of which worked. So I have now reduced it to a mini- 
> example. Here is the Fortran program that calls a C++ function: 
> ..

@db,

Please review the following illustration:

-+- begin console output ---
C:\temp>type c++.cpp
extern "C" const double mini(const double arg) {
   return arg*2;
}


C:\temp>type p.f90
   use, intrinsic :: iso c binding, only : c double
   interface
      function mini( arg ) result(r) bind(C, name="mini")
         import :: c double
         real(c double), value, intent(in) :: arg
         ! function result
         real(c double) :: r
      end function
   end interface
   real(c double) :: arg, arg2
   arg = 2.0
   arg2 = mini(arg)
   print `(" MINI returned =", f10.4)`, arg2
end

C:\temp>gfortran c++.cpp p.f90 -o p.exe

C:\temp>p.exe
 MINI returned =    4.0000
-+- end console output ---

Now, please note the following:

 1. extern "C" decoration of the C++ function. If you seek portable
and simpler interoperation of C++ code with Fortran and thus seek to
use the standard facilities in Fortran, then note the interoperation is
between a Fortran processor and a companion C processor only. It is NOT
with C++ directly. In C++ code, you achieve the companion C processor
semantics via *extern "C"*. Without *extern "C"*, you need to look into the
processor extensions and see how C++ and Fortran might work that processor
(e.g, GCC/gfortran system). That is, if at all they interoperate - note
there is no standard Fortran here.

 2. In Fortran code, look at the IMPORT statement in the INTERFACE
block. Note an INTERFACE block in Fortran generally is stand-alone, it has
no knowledge of the host unit except for what it`s informed. In lieu
of IMPORT, you can duplicate the USE statement in the INTERFACE block,
if you so choose. I don`t recommend it though. Bottom-line: an IMPORT
or USE is needed to inform the INTERFACE for this MINI function what
`c double` is.

 3. Consider using KINDs from ISO C BINDING module for the Fortran
objects that interoperate with the external procedures such as `MINI`. So
note `arg` and `arg2` can both be declared as real(c double). You can
use renaming facility in Fortran to shorten the kind name to `CD` or
some such, if you prefer.

 By the way, have you considered studying from books closely such as
Chapman on Fortran 2008 and Modern Fortran Explained? It appears you will
benefit yourself by doing so - you are getting stuck with very basic
semantics and syntax aspects of modern Fortran and you would do well by
putting aside a bit of time to read first to save yourself a lot of time
with debugging basic errors and engaging on online forums seeking help to
resolve them.
--- G2/1.0
 * Origin: usenet.network (2:5075/128)
SEEN-BY: 5001/100 5005/49 5015/255 5019/40 5020/715
848 1042 4441 12000
SEEN-BY: 5030/49 1081 5058/104 5075/128
@PATH: 5075/128 5020/1042 4441



   GoldED+ VK   │                                                 │   09:55:30    
                                                                                
В этой области больше нет сообщений.

Остаться здесь
Перейти к списку сообщений
Перейти к списку эх