Nп/п : 93 из 100
 От   : pehache                             2:5075/128        21 сен 23 12:47:18
 К    : db                                                    21 сен 23 15:51:02
 Тема : Re: Calling a C++ function from Forttran
----------------------------------------------------------------------------------
                                                                                 
@MSGID:
55d33e0d
@REPLY: 1@dont-email.me> 5e70ea2c
@REPLYADDR pehache <pehache.7@gmail.com>
@REPLYTO 2:5075/128 pehache
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID:

@RFC-References: 1@dont-email.me>
<klobumFfoflU1@mid.individual.net> 1@dont-email.me> <kls9koF4ootU1@mid.individual.net>
1@dont-email.me> 1@dont-email.me> 1@dont-email.me>
@TZUTC: 0000
@PID: Nemo/0.999a
@TID: FIDOGATE-5.12-ge4e8b94
Le 08/09/2023 ? 15:15, db a ?crit :
> On 08.09.2023 14.52, db wrote:
>> On 07.09.2023 11.49, db wrote:
>>> On 06.09.2023 23.35, pehache wrote:
>>>> Le 06/09/2023 ? 14:24, db a ?crit :
>>>>> On 05.09.2023 11.50, pehache wrote:
>>>>>> Le 04/09/2023 ? 16:43, db a ?crit :
>>>>>>> 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
>>>>>
>>>>> It resulted in a Forytran program that calls a Fortran function
>>>>> that calls the C++ function IBAND (exists as iband.cpp). Here is
>>>>> the code (sorry for the length)
>>>>>
>>>>> module STUFF
>>>>>    integer, parameter :: dbl=selected_real_kind(14)
>>>>>    integer, parameter :: qud=selected_real_kind(30)
>>>>> end module STUFF
>>>>> program JBAND_TEST
>>>>>    use STUFF;   implicit none
>>>>>
>>>>>    real(dbl) :: curr, FORTJBAND
>>>>>    real(qud) :: tbar
>>>>>    do
>>>>>       read *, tbar
>>>>>       if (tbar < 0) exit
>>>>>       curr = FORTJBAND (tbar)
>>>>>       print `(" curr =", f10.4)`, curr
>>>>>    enddo
>>>>> end program JBAND_TEST
>>>>> function FORTJBAND (tbar)
>>>>>    use STUFF;   implicit none
>>>>>    interface
>>>>>      function JBAND (tbar) bind(c)
>>>>>      import JBAND, c_long_double
>>>>>      import tbar, c_long_double
>>>>>      end function JBAND
>>>>>    end interface
>>>>>    real(dbl) :: FORTJBAND
>>>>>    real(qud) :: tbar, JBAND
>>>>>    FORTJBAND = JBAND (tbar)
>>>>> end function FORTJBAND
>>>>
>>>> 1) You are missing the ISO_C_BINDING module
>>>> 2) I don`t get the syntax with the "import"
>>>> 3) you don`t need to declare JBAND in addition to the interface
>>>>
>>>> =======================================
>>>> function FORTJBAND (tbar)
>>>>     use ISO_C_BINDING
>>>>     use STUFF
>>>>     implicit none
>>>>     interface
>>>>       function JBAND (tbar) bind(c)
>>>>         real(c_lonf_double) :: JBAND
>>>>         real(c_lonf_double) :: tbar
>>>>       end function JBAND
>>>>     end interface
>>>>     real(dbl) :: FORTJBAND
>>>>     real(qud) :: tbar
>>>>     FORTJBAND = JBAND (tbar)
>>>> end function FORTJBAND
>>>> =======================================
>>>>
>>>>>
>>>>> I then wrote a script for running it, reproducing what the link
>>>>> tells me:
>>>>>
>>>>> cpp -o jband.o jband.cpp
>>>>> gfortran -o jbandtest.o jbandtest.f90
>>>>> gfortran jband.o jbandtest.o
>>>>> ./a.out << eoi
>>>>> 1.0
>>>>> 10.0
>>>>> -1
>>>>> eoi
>>>>>
>>>>> When I run this, I get this:
>>>>>
>>>>> ~/ownlib90/tests> ./jbandtest
>>>>> cpp: fatal error: cannot execute `cc1plus`: execvp: No such file or 
>>>>> directory
>>>>> compilation terminated.
>>>>
>>>> Isn`t "cpp" just the C/C++ preprocessor? Shouldn`t you use "g++" 
>>>> instead?
>>>>
>>> Ah, OK, I thought it mean c plus plus. I will install C++ now
>>> and compile with G++ (i suppose).
>>>
>> I now have g++ installed and ran it with g++ instead of cpp. I still
>> get complaints about long double:
>> 
>> jbandtest.f90:21:31:
>> 
>>     21 |     import JBAND, c_long_double
>>        |                               1
>> Error: Cannot IMPORT `c_long_double` from host scoping unit at (1) - 
>> does not exist.
>> jbandtest.f90:22:30:
>> 
>> (several times). Is it the underscores? Inside the C++ function, it
>> is just "long double".

> I just tried reducing all long double entities to plain double in both
> the C++ and calling Fortran units. Now it says that C_double does not
> exist. So the problem is getting Fortran to specify a C++ type. I
> thought that the import statement does that.

Please go back to one of my previous message where I wrote: "I don`t get 
the syntax with the "import"". And proposed the correct syntax.
--- Nemo/0.999a
 * Origin: Nemoweb (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    
                                                                                
В этой области больше нет сообщений.

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