Nп/п : 98 из 100
 От   : db                                  2:5075/128        30 сен 23 15:42:29
 К    : db                                                    30 сен 23 16:46:03
 Тема : Re: Calling a C++ function from Forttran
----------------------------------------------------------------------------------
                                                                                 
@MSGID: 1@dont-email.me> 8b6cbed6
@REPLY: 1@dont-email.me> e2827ca3
@REPLYADDR db <dieterhansbritz@gmail.com>
@REPLYTO 2:5075/128 db
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID: 1@dont-email.me>
@RFC-References: 1@dont-email.me>
@TZUTC: 0200
@PID: Mozilla/5.0 (X11; Linux x86_64; rv:102.0)
Gecko/20100101 Thunderbird/102.15.1
@TID: FIDOGATE-5.12-ge4e8b94
On 04.09.2023 16.43, db wrote:
> I see that rather than try to convert a C++ function to
> Fortran, it might be better to call it from a Fortran
> program. So I found a description of how to do it, and
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:

program MINI_TEST
   use ISO_C_BINDING
   implicit none
   integer, parameter :: dbl=selected_real_kind(14)
   real(dbl) :: MINI, arg, arg2
   interface
     function MINI (tbar)
       real(c_double) :: MINI
       real(c_double) :: arg
     end function MINI
   end interface
   read *, arg
   arg2 = MINI (arg)
   print `(" MINI returned =", f10.4)`, arg2
end program MINI_TEST

The function is

#include 
#include 
#include 
#include "math.h"
using namespace std;
const double mini(const double arg)
{
return arg * 2;
}


I know this works because I got a C++ calling program from
a friend, that called it and the result is correct.

I wrote a shell script to run this, using another suggestion

gfortran -o minitest.out minitest.f90 mini.cpp -lstdc++ -lm
./minitest.out << eoi
1.0
eoi
rm minitest.out

and ran that. The main problem is that it doesn`t recognise
the C++ type c_double:

minitest.f90:8:11:

     8 |       real(c_double) :: MINI
       |           1
Error: Parameter `c_double` at (1) has not been declared or is a 
variable, which does not reduce to a constant expression

(same for arg) plus a type mismatch arising from it.

So: can you suggest how to make this work? If so, then
I can implement that with the much longer actual function
I am trying to call from Fortran.


-- 
Dieter Britz

 --- Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
 * Origin: A noiseless patient Spider (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    
                                                                                
В этой области больше нет сообщений.

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