Nп/п : 80 из 100
 От   : db                                  2:5075/128        08 сен 23 15:15:39
 К    : db                                                    08 сен 23 16:17:02
 Тема : Re: Calling a C++ function from Forttran
----------------------------------------------------------------------------------
                                                                                 
@MSGID: 1@dont-email.me> 5e70ea2c
@REPLY: 1@dont-email.me> 8beb69f8
@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>
<klobumFfoflU1@mid.individual.net> 1@dont-email.me> <kls9koF4ootU1@mid.individual.net>
1@dont-email.me> 1@dont-email.me>
@TZUTC: 0200
@PID: Mozilla/5.0 (X11; Linux x86_64; rv:102.0)
Gecko/20100101 Thunderbird/102.15.0
@TID: FIDOGATE-5.12-ge4e8b94
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.

-- 
Dieter Britz

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

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