Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Everything should be made as simple as possible, but not simpler. -- Albert Einstein


devel / comp.lang.fortran / Re: Calling a C++ function from Forttran

SubjectAuthor
* Calling a C++ function from Forttrandb
+- Re: Calling a C++ function from Forttrangah4
+* Re: Calling a C++ function from Forttranpehache
|`* Re: Calling a C++ function from Forttrandb
| +- Re: Calling a C++ function from Forttrangah4
| `* Re: Calling a C++ function from Forttranpehache
|  `* Re: Calling a C++ function from Forttrandb
|   +* Re: Calling a C++ function from Forttrangah4
|   |`- Re: Calling a C++ function from Forttranpehache
|   `* Re: Calling a C++ function from Forttrandb
|    `* Re: Calling a C++ function from Forttrandb
|     `- Re: Calling a C++ function from Forttranpehache
`* Re: Calling a C++ function from Forttrandb
 +- Re: Calling a C++ function from ForttranGary Scott
 `* Re: Calling a C++ function from ForttranFortranFan
  `* Re: Calling a C++ function from Forttrandb
   `* Re: Calling a C++ function from Forttrandb
    +- Re: Calling a C++ function from Forttrangah4
    `* Re: Calling a C++ function from ForttranFortranFan
     +* Re: Calling a C++ function from Forttrandb
     |`* Re: Calling a C++ function from Forttranfeanor
     | `* Re: Calling a C++ function from Forttrandb
     |  `- Re: Calling a C++ function from Forttrangah4
     `- Re: Calling a C++ function from ForttranSteven G. Kargl

1
Calling a C++ function from Forttran

<ud4qeq$1gqvg$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3584&group=comp.lang.fortran#3584

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dieterhansbritz@gmail.com (db)
Newsgroups: comp.lang.fortran
Subject: Calling a C++ function from Forttran
Date: Mon, 4 Sep 2023 16:43:38 +0200
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <ud4qeq$1gqvg$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 4 Sep 2023 14:43:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6e8aae73027e1cc3c4401066a0b04daa";
logging-data="1600496"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bZXCi2d9cGLkMVP4H5NJskWodfAt61is="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:/DccSNCOWHAF+2HxcyG3WbiR12U=
Content-Language: en-US
 by: db - Mon, 4 Sep 2023 14:43 UTC

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
it involves an interface:

interface
subroutine normal_vec(seed, n, x) bind(c)
import c_int, c_double
integer(kind=c_int), intent(in), value :: seed, n
real(kind=c_double) :: x(n)
end subroutine normal_vec
end interface

This is for an example of a random number generator.
I note a declaration of the variable type c_double.
The function I want to call declares "long double"
but I doubt that I can use

import c_long double
....
real(kind=c_long double) :: t, JBAND

where t is the variable passed to the function JBAND

What should I put there?
--
Dieter Britz

Re: Calling a C++ function from Forttran

<b2f7b710-3644-4305-abca-0beaec277022n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3587&group=comp.lang.fortran#3587

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:622a:64e:b0:403:f763:5c6a with SMTP id a14-20020a05622a064e00b00403f7635c6amr263362qtb.12.1693857709501;
Mon, 04 Sep 2023 13:01:49 -0700 (PDT)
X-Received: by 2002:a17:90a:590d:b0:263:3727:6045 with SMTP id
k13-20020a17090a590d00b0026337276045mr2685477pji.4.1693857708925; Mon, 04 Sep
2023 13:01:48 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Mon, 4 Sep 2023 13:01:48 -0700 (PDT)
In-Reply-To: <ud4qeq$1gqvg$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:25e8:99cf:b97f:8814;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:25e8:99cf:b97f:8814
References: <ud4qeq$1gqvg$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <b2f7b710-3644-4305-abca-0beaec277022n@googlegroups.com>
Subject: Re: Calling a C++ function from Forttran
From: gah4@u.washington.edu (gah4)
Injection-Date: Mon, 04 Sep 2023 20:01:49 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: gah4 - Mon, 4 Sep 2023 20:01 UTC

On Monday, September 4, 2023 at 7:43:44 AM UTC-7, db wrote:
(snip)

> This is for an example of a random number generator.
> I note a declaration of the variable type c_double.
> The function I want to call declares "long double"
> but I doubt that I can use
>
> import c_long double
> ...
> real(kind=c_long double) :: t, JBAND

The constants are supposed to be defined for the types supported
by the associated C compiler. (You need to write C callable C++ code.)

Re: Calling a C++ function from Forttran

<klobumFfoflU1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3590&group=comp.lang.fortran#3590

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: pehache.7@gmail.com (pehache)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Tue, 5 Sep 2023 11:50:13 +0200
Lines: 35
Message-ID: <klobumFfoflU1@mid.individual.net>
References: <ud4qeq$1gqvg$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net ZWXV8U2ohh3VEN77N5PgQAxl4OiZvlqG/QypVJU6KApuJCwRZ3
Cancel-Lock: sha1:XsJAEhO78Jwu3l031qZydvfWNRQ= sha256:oMRqZaYgN3po99uTc6SvkTEbc3TKHDOGIKKZ6yEyExs=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:91.0)
Gecko/20100101 Thunderbird/91.11.0
Content-Language: fr
In-Reply-To: <ud4qeq$1gqvg$1@dont-email.me>
 by: pehache - Tue, 5 Sep 2023 09:50 UTC

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, and
> it involves an interface:
>
> interface
> subroutine normal_vec(seed, n, x) bind(c)
> import c_int, c_double
> integer(kind=c_int), intent(in), value :: seed, n
> real(kind=c_double)                    :: x(n)
> end subroutine normal_vec
> end interface
>
> This is for an example of a random number generator.
> I note a declaration of the variable type c_double.
> The function I want to call declares "long double"
> but I doubt that I can use
>
> import c_long double
> ...
> real(kind=c_long double) :: t, JBAND
>
> where t is the variable passed to the function JBAND
>
>
> What should I put there?

real(kind=c_long_double) :: ...

--
"...sois ouvert aux idées des autres pour peu qu'elles aillent dans le
même sens que les tiennes.", ST sur fr.bio.medecine
ST passe le mur du çon : <j3nn2hFmqj7U1@mid.individual.net>

Re: Calling a C++ function from Forttran

<ud9r2g$2gaml$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3597&group=comp.lang.fortran#3597

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dieterhansbritz@gmail.com (db)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Wed, 6 Sep 2023 14:24:48 +0200
Organization: A noiseless patient Spider
Lines: 83
Message-ID: <ud9r2g$2gaml$1@dont-email.me>
References: <ud4qeq$1gqvg$1@dont-email.me> <klobumFfoflU1@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 6 Sep 2023 12:24:48 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9f0072eb8cbf368fb00afaa7ff92bfc6";
logging-data="2632405"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19hqlDC1/e8PKKBwiWJqyjoE3cp64scDK4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:PjnRdixmQnVX+pBzUrgEBVBqqw8=
In-Reply-To: <klobumFfoflU1@mid.individual.net>
Content-Language: en-US
 by: db - Wed, 6 Sep 2023 12:24 UTC

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

The file jband.cpp is large. It is declared as long double and so
is the input to it, tbar.

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.
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:

22 | import tbar, c_long_double
| 1
Error: Cannot IMPORT 'c_long_double' from host scoping unit at (1) -
does not exist.
jbandtest.f90:27:14:

27 | FORTJBAND = JBAND (tbar)
| 1
Error: Type mismatch in argument 'tbar' at (1); passed REAL(16) to REAL(4)

etc, What is cc1plus, where does that come in? Why is long double
not recognised? What do I do to get this running?
--
Dieter Britz

Re: Calling a C++ function from Forttran

<8e340a31-9b28-419f-97d2-ba430522f87an@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3601&group=comp.lang.fortran#3601

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:620a:878c:b0:76d:9200:6df3 with SMTP id py12-20020a05620a878c00b0076d92006df3mr338417qkn.13.1694006004793;
Wed, 06 Sep 2023 06:13:24 -0700 (PDT)
X-Received: by 2002:a63:221d:0:b0:56f:e58d:8835 with SMTP id
i29-20020a63221d000000b0056fe58d8835mr3419780pgi.12.1694006004313; Wed, 06
Sep 2023 06:13:24 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Wed, 6 Sep 2023 06:13:23 -0700 (PDT)
In-Reply-To: <ud9r2g$2gaml$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:3558:2391:2595:b888;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:3558:2391:2595:b888
References: <ud4qeq$1gqvg$1@dont-email.me> <klobumFfoflU1@mid.individual.net> <ud9r2g$2gaml$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <8e340a31-9b28-419f-97d2-ba430522f87an@googlegroups.com>
Subject: Re: Calling a C++ function from Forttran
From: gah4@u.washington.edu (gah4)
Injection-Date: Wed, 06 Sep 2023 13:13:24 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 1690
 by: gah4 - Wed, 6 Sep 2023 13:13 UTC

On Wednesday, September 6, 2023 at 5:24:52 AM UTC-7, db wrote:

(snip)

> When I run this, I get this:
>
> ~/ownlib90/tests> ./jbandtest
> cpp: fatal error: cannot execute 'cc1plus': execvp: No such file or
> directory
> compilation terminated.
cc1plus is the first pass of the C++ compiler.
(I suspect it also won't find the second pass, but didn't get that far.)

You need to install a C++ compiler.

Re: Calling a C++ function from Forttran

<kls9koF4ootU1@mid.individual.net>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3603&group=comp.lang.fortran#3603

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: pehache.7@gmail.com (pehache)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Wed, 6 Sep 2023 23:35:20 +0200
Lines: 87
Message-ID: <kls9koF4ootU1@mid.individual.net>
References: <ud4qeq$1gqvg$1@dont-email.me> <klobumFfoflU1@mid.individual.net>
<ud9r2g$2gaml$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: individual.net pwSFFqnUUNYmvpVxH57TrgT8Sfot6vQ3bxzx7KvVDWAUwRW1UD
Cancel-Lock: sha1:WIj3s7DhIP3lnRSpTEyKaAWWby4= sha256:Mb9tklc21PyUE6Id9gJ4IZExmlry7tLz3GNQDgPGYMw=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:91.0)
Gecko/20100101 Thunderbird/91.11.0
Content-Language: fr
In-Reply-To: <ud9r2g$2gaml$1@dont-email.me>
 by: pehache - Wed, 6 Sep 2023 21:35 UTC

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?

--
"...sois ouvert aux idées des autres pour peu qu'elles aillent dans le
même sens que les tiennes.", ST sur fr.bio.medecine
ST passe le mur du çon : <j3nn2hFmqj7U1@mid.individual.net>

Re: Calling a C++ function from Forttran

<udc6b0$2ulig$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3604&group=comp.lang.fortran#3604

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dieterhansbritz@gmail.com (db)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Thu, 7 Sep 2023 11:49:20 +0200
Organization: A noiseless patient Spider
Lines: 90
Message-ID: <udc6b0$2ulig$1@dont-email.me>
References: <ud4qeq$1gqvg$1@dont-email.me> <klobumFfoflU1@mid.individual.net>
<ud9r2g$2gaml$1@dont-email.me> <kls9koF4ootU1@mid.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 7 Sep 2023 09:49:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9e06cdb8723eec0571a7d37b9f559d84";
logging-data="3102288"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+6XH9vEtvVHPXglfmTXd56BI58xLleoIk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:e6fPD/0hhlO77mQ4S2VSfnQQMg4=
Content-Language: en-US
In-Reply-To: <kls9koF4ootU1@mid.individual.net>
 by: db - Thu, 7 Sep 2023 09:49 UTC

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).

--
Dieter Britz

Re: Calling a C++ function from Forttran

<ce7c69a8-fac9-410a-9411-c92a2e7ac519n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3605&group=comp.lang.fortran#3605

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:622a:110f:b0:412:d46:a8c2 with SMTP id e15-20020a05622a110f00b004120d46a8c2mr71475qty.1.1694093157267;
Thu, 07 Sep 2023 06:25:57 -0700 (PDT)
X-Received: by 2002:a17:902:e881:b0:1bc:6799:3f88 with SMTP id
w1-20020a170902e88100b001bc67993f88mr6676318plg.0.1694093156780; Thu, 07 Sep
2023 06:25:56 -0700 (PDT)
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Thu, 7 Sep 2023 06:25:56 -0700 (PDT)
In-Reply-To: <udc6b0$2ulig$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:3088:73b4:91f4:3efd;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:3088:73b4:91f4:3efd
References: <ud4qeq$1gqvg$1@dont-email.me> <klobumFfoflU1@mid.individual.net>
<ud9r2g$2gaml$1@dont-email.me> <kls9koF4ootU1@mid.individual.net> <udc6b0$2ulig$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ce7c69a8-fac9-410a-9411-c92a2e7ac519n@googlegroups.com>
Subject: Re: Calling a C++ function from Forttran
From: gah4@u.washington.edu (gah4)
Injection-Date: Thu, 07 Sep 2023 13:25:57 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 1990
 by: gah4 - Thu, 7 Sep 2023 13:25 UTC

On Thursday, September 7, 2023 at 2:49:24 AM UTC-7, db wrote:

(snip)

> > Isn't "cpp" just the C/C++ preprocessor? Shouldn't you use "g++" instead?

Tradition is that it figures out what to do based on the file names.

In any case, cc1 is the usual first pass of the C compilers, and I suspect that
cc1plus is the first pass of the C++ compiler.

It is usual for C and C++ compilers to include the preprocessor, and not
run it as a separate pass. You can still ask for only the preprocessor, though.

Traditionally, that is:

cc -E
> Ah, OK, I thought it mean c plus plus. I will install C++ now
> and compile with G++ (i suppose).

Re: Calling a C++ function from Forttran

<Z8-fIicnNLDIK2Bhc5oiJz2tj7M@jntp>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3607&group=comp.lang.fortran#3607

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!news.niel.me!pasdenom.info!from-devjntp
Message-ID: <Z8-fIicnNLDIK2Bhc5oiJz2tj7M@jntp>
JNTP-Route: news2.nemoweb.net
JNTP-DataType: Article
Subject: Re: Calling a C++ function from Forttran
References: <ud4qeq$1gqvg$1@dont-email.me> <klobumFfoflU1@mid.individual.net> <ud9r2g$2gaml$1@dont-email.me>
<kls9koF4ootU1@mid.individual.net> <udc6b0$2ulig$1@dont-email.me>
<ce7c69a8-fac9-410a-9411-c92a2e7ac519n@googlegroups.com>
Newsgroups: comp.lang.fortran
JNTP-HashClient: Di1Lv0cpwHrMml9_Yunms1WHcCY
JNTP-ThreadID: ud4qeq$1gqvg$1@dont-email.me
JNTP-Uri: http://news2.nemoweb.net/?DataID=Z8-fIicnNLDIK2Bhc5oiJz2tj7M@jntp
User-Agent: Nemo/0.999a
JNTP-OriginServer: news2.nemoweb.net
Date: Fri, 08 Sep 23 12:33:18 +0000
Organization: Nemoweb
JNTP-Browser: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0
Injection-Info: news2.nemoweb.net; posting-host="fd5675444fb2abe5cff243786215e1a6f7fd1bea"; logging-data="2023-09-08T12:33:18Z/8199139"; posting-account="44@news2.nemoweb.net"; mail-complaints-to="newsmaster@news2.nemoweb.net"
JNTP-ProtocolVersion: 0.21.1
JNTP-Server: PhpNemoServer/0.94.5
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-JNTP-JsonNewsGateway: 0.96
From: pehache.7@gmail.com (pehache)
 by: pehache - Fri, 8 Sep 2023 12:33 UTC

Le 07/09/2023 à 15:25, gah4 a écrit :
> On Thursday, September 7, 2023 at 2:49:24 AM UTC-7, db wrote:
>
> (snip)
>
>> > Isn't "cpp" just the C/C++ preprocessor? Shouldn't you use "g++" instead?
>
> Tradition is that it figures out what to do based on the file names.
>
> In any case, cc1 is the usual first pass of the C compilers, and I suspect that
> cc1plus is the first pass of the C++ compiler.
>
> It is usual for C and C++ compilers to include the preprocessor, and not
> run it as a separate pass. You can still ask for only the preprocessor, though.
>
> Traditionally, that is:
>
> cc -E
>

Sure, but the preprocessor is also available as a separate command.

https://linux.die.net/man/1/cpp

Re: Calling a C++ function from Forttran

<udf5e7$3f8l0$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3608&group=comp.lang.fortran#3608

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dieterhansbritz@gmail.com (db)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Fri, 8 Sep 2023 14:52:23 +0200
Organization: A noiseless patient Spider
Lines: 104
Message-ID: <udf5e7$3f8l0$1@dont-email.me>
References: <ud4qeq$1gqvg$1@dont-email.me> <klobumFfoflU1@mid.individual.net>
<ud9r2g$2gaml$1@dont-email.me> <kls9koF4ootU1@mid.individual.net>
<udc6b0$2ulig$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 8 Sep 2023 12:52:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f48b1a94cd9ae905897840bfd69e9c03";
logging-data="3646112"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/yU1rOU93Lymih/Rmbpt78ZOjvKWB8G7I="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:HMYWvDLuBFVt/9ZXKbUPTSJfhEg=
In-Reply-To: <udc6b0$2ulig$1@dont-email.me>
Content-Language: en-US
 by: db - Fri, 8 Sep 2023 12:52 UTC

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".
--
Dieter Britz

Re: Calling a C++ function from Forttran

<udf6pr$3ff08$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3609&group=comp.lang.fortran#3609

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dieterhansbritz@gmail.com (db)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Fri, 8 Sep 2023 15:15:39 +0200
Organization: A noiseless patient Spider
Lines: 112
Message-ID: <udf6pr$3ff08$1@dont-email.me>
References: <ud4qeq$1gqvg$1@dont-email.me> <klobumFfoflU1@mid.individual.net>
<ud9r2g$2gaml$1@dont-email.me> <kls9koF4ootU1@mid.individual.net>
<udc6b0$2ulig$1@dont-email.me> <udf5e7$3f8l0$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 8 Sep 2023 13:15:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f48b1a94cd9ae905897840bfd69e9c03";
logging-data="3652616"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19tcI+HLpjGYcGA1NaEHo60BYErKsCA7Jc="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:UZNk9k1SskOKEzV0Epn1DKcBwOo=
In-Reply-To: <udf5e7$3f8l0$1@dont-email.me>
Content-Language: en-US
 by: db - Fri, 8 Sep 2023 13:15 UTC

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

Re: Calling a C++ function from Forttran

<hcOYeLyjfX2UVj7ha7g7kjkPDpU@jntp>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3622&group=comp.lang.fortran#3622

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!news.niel.me!pasdenom.info!from-devjntp
Message-ID: <hcOYeLyjfX2UVj7ha7g7kjkPDpU@jntp>
JNTP-Route: news2.nemoweb.net
JNTP-DataType: Article
Subject: Re: Calling a C++ function from Forttran
References: <ud4qeq$1gqvg$1@dont-email.me> <klobumFfoflU1@mid.individual.net> <ud9r2g$2gaml$1@dont-email.me>
<kls9koF4ootU1@mid.individual.net> <udc6b0$2ulig$1@dont-email.me> <udf5e7$3f8l0$1@dont-email.me>
<udf6pr$3ff08$1@dont-email.me>
Newsgroups: comp.lang.fortran
JNTP-HashClient: fEHmfmpk5h4dfC9f2k4EXefh1Uc
JNTP-ThreadID: ud4qeq$1gqvg$1@dont-email.me
JNTP-Uri: http://news2.nemoweb.net/?DataID=hcOYeLyjfX2UVj7ha7g7kjkPDpU@jntp
User-Agent: Nemo/0.999a
JNTP-OriginServer: news2.nemoweb.net
Date: Thu, 21 Sep 23 12:47:18 +0000
Organization: Nemoweb
JNTP-Browser: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:109.0) Gecko/20100101 Firefox/115.0
Injection-Info: news2.nemoweb.net; posting-host="020ce5ce37af206065359e759fcc34c2c28de98b"; logging-data="2023-09-21T12:47:18Z/8236160"; posting-account="44@news2.nemoweb.net"; mail-complaints-to="newsmaster@news2.nemoweb.net"
JNTP-ProtocolVersion: 0.21.1
JNTP-Server: PhpNemoServer/0.94.5
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-JNTP-JsonNewsGateway: 0.96
From: pehache.7@gmail.com (pehache)
 by: pehache - Thu, 21 Sep 2023 12:47 UTC

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.

Re: Calling a C++ function from Forttran

<uf98k5$t65i$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3627&group=comp.lang.fortran#3627

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dieterhansbritz@gmail.com (db)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Sat, 30 Sep 2023 15:42:29 +0200
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <uf98k5$t65i$1@dont-email.me>
References: <ud4qeq$1gqvg$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 30 Sep 2023 13:42:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0e79b4ea8ae1c33d949b4d694e326679";
logging-data="956594"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19pntF7bx73RglvpbiUur2dMss9mVzf3mA="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:f3bpW1TsO8UM+/YVsOGuhaYZJDA=
In-Reply-To: <ud4qeq$1gqvg$1@dont-email.me>
Content-Language: en-US
 by: db - Sat, 30 Sep 2023 13:42 UTC

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 <iostream>
#include <fstream>
#include <iomanip>
#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

Re: Calling a C++ function from Forttran

<uf99ne$tiqv$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3628&group=comp.lang.fortran#3628

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: garylscott@sbcglobal.net (Gary Scott)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Sat, 30 Sep 2023 09:01:18 -0500
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <uf99ne$tiqv$1@dont-email.me>
References: <ud4qeq$1gqvg$1@dont-email.me> <uf98k5$t65i$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 30 Sep 2023 14:01:18 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="29c53c0ae3595fc20c95f64872ad0b98";
logging-data="969567"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Cr29JPp8lg/rvKteyaOfQlQCrKNC8a+8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:6CjJ7nxWL1sHu25dy9h3d7SNVfg=
Content-Language: en-US
In-Reply-To: <uf98k5$t65i$1@dont-email.me>
 by: Gary Scott - Sat, 30 Sep 2023 14:01 UTC

I wonder if you add ISO_C_BINDING inside the function interface if it
helps resolve the reference.

On 9/30/2023 8:42 AM, db wrote:
> 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 <iostream>
> #include <fstream>
> #include <iomanip>
> #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.
>
>

Re: Calling a C++ function from Forttran

<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3629&group=comp.lang.fortran#3629

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:620a:15b:b0:774:f02:7e59 with SMTP id e27-20020a05620a015b00b007740f027e59mr121638qkn.11.1696092791199;
Sat, 30 Sep 2023 09:53:11 -0700 (PDT)
X-Received: by 2002:a05:6808:f04:b0:3a7:9a19:332b with SMTP id
m4-20020a0568080f0400b003a79a19332bmr3466013oiw.7.1696092790929; Sat, 30 Sep
2023 09:53:10 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Sat, 30 Sep 2023 09:53:10 -0700 (PDT)
In-Reply-To: <uf98k5$t65i$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=108.52.103.144; posting-account=ZZXq9AoAAAAQEcA7zKAGm0UFQh4gMBv7
NNTP-Posting-Host: 108.52.103.144
References: <ud4qeq$1gqvg$1@dont-email.me> <uf98k5$t65i$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com>
Subject: Re: Calling a C++ function from Forttran
From: parekhvs@gmail.com (FortranFan)
Injection-Date: Sat, 30 Sep 2023 16:53:11 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 4155
 by: FortranFan - Sat, 30 Sep 2023 16:53 UTC

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.

Re: Calling a C++ function from Forttran

<ufbpk1$1ifq9$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3630&group=comp.lang.fortran#3630

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dieterhansbritz@gmail.com (db)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Sun, 1 Oct 2023 14:44:48 +0200
Organization: A noiseless patient Spider
Lines: 76
Message-ID: <ufbpk1$1ifq9$1@dont-email.me>
References: <ud4qeq$1gqvg$1@dont-email.me> <uf98k5$t65i$1@dont-email.me>
<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 1 Oct 2023 12:44:49 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="de93cc473a97cd9a2ff3a0e6a711a7f9";
logging-data="1654601"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX181MxHyRTa9d1Wdwk9WDy5YkzQIDJGbuU0="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:bWbFgbCQu8Nf8h9qc++6xiqfT5E=
Content-Language: en-US
In-Reply-To: <e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com>
 by: db - Sun, 1 Oct 2023 12:44 UTC

On 30.09.2023 18.53, FortranFan wrote:
> 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.

Thank you, that worked, also with my original mini.cpp afterI commented
out the first few lines

#include <iostream>
#include <fstream>
#include <iomanip>
#include "math.h"
using namespace std;

And I added your extern "C".

I have never used C ++ so I just used what the original, much
longer, C++ function code used, but these lines seem not to
be needed. I must now implement all this with the actual
function, trimmed appropriately.

You might be right that I could study more modern Fortran but so
far f90 has served me well. As for online fora, this is what they
are for, aren't they? You have solved my problem. If everyone
knew everything, there would be no need for this ng.
--
Dieter Britz

Re: Calling a C++ function from Forttran

<ufe3u6$2smq8$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3631&group=comp.lang.fortran#3631

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dieterhansbritz@gmail.com (db)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Mon, 2 Oct 2023 11:53:10 +0200
Organization: A noiseless patient Spider
Lines: 115
Message-ID: <ufe3u6$2smq8$1@dont-email.me>
References: <ud4qeq$1gqvg$1@dont-email.me> <uf98k5$t65i$1@dont-email.me>
<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com>
<ufbpk1$1ifq9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 2 Oct 2023 09:53:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d5e4403f562a5bfbeddedf70aae4fd75";
logging-data="3038024"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ir3FnL0LanjTXaCaL+Q/eVMjQ8ovX9G8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:ECkiqMWkRFMpG18aUijaT0ztJEw=
In-Reply-To: <ufbpk1$1ifq9$1@dont-email.me>
Content-Language: en-US
 by: db - Mon, 2 Oct 2023 09:53 UTC

On 01.10.2023 14.44, db wrote:
> On 30.09.2023 18.53, FortranFan wrote:
>> 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.
>
> Thank you, that worked, also with my original mini.cpp afterI commented
> out the first few lines
>
> #include <iostream>
> #include <fstream>
> #include <iomanip>
> #include "math.h"
> using namespace std;
>
> And I added your extern "C".
>
> I have never used C ++ so I just used what the original, much
> longer, C++ function code used, but these lines seem not to
> be needed. I must now implement all this with the actual
> function, trimmed appropriately.
>
> You might be right that I could study more modern Fortran but so
> far f90 has served me well. As for online fora, this is what they
> are for, aren't they? You have solved my problem. If everyone
> knew everything, there would be no need for this ng.

I might add that f90 was a major step, generic intrinsics, whole array
operations, self defined types and precisions. It simplified my
programming.

However, one more question. Your suggestions worked and I implemented
them for the actual long C++ function that is the object of all this.
I had given up on the original declarations of long double because
Fortran didn't seem to want to know it, and reduced the C++ code to
plain double, stating (c_double) in the Fortran calling program.
How do I specify, in the Fortran code, long double? It would be
equivalent to quad precision which I can ask for, 30 digits.

--
Dieter Britz

Re: Calling a C++ function from Forttran

<bd8a3508-6c0d-40d2-94e2-fe568c36e5c3n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3632&group=comp.lang.fortran#3632

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:622a:1804:b0:410:a249:bee5 with SMTP id t4-20020a05622a180400b00410a249bee5mr152293qtc.9.1696245610110;
Mon, 02 Oct 2023 04:20:10 -0700 (PDT)
X-Received: by 2002:a05:6808:20a9:b0:3a7:45f6:4b3f with SMTP id
s41-20020a05680820a900b003a745f64b3fmr5643038oiw.3.1696245609975; Mon, 02 Oct
2023 04:20:09 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!tncsrv06.tnetconsulting.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Mon, 2 Oct 2023 04:20:09 -0700 (PDT)
In-Reply-To: <ufe3u6$2smq8$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:2948:afa0:5700:1f27;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:2948:afa0:5700:1f27
References: <ud4qeq$1gqvg$1@dont-email.me> <uf98k5$t65i$1@dont-email.me>
<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com> <ufbpk1$1ifq9$1@dont-email.me>
<ufe3u6$2smq8$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <bd8a3508-6c0d-40d2-94e2-fe568c36e5c3n@googlegroups.com>
Subject: Re: Calling a C++ function from Forttran
From: gah4@u.washington.edu (gah4)
Injection-Date: Mon, 02 Oct 2023 11:20:10 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2433
 by: gah4 - Mon, 2 Oct 2023 11:20 UTC

On Monday, October 2, 2023 at 2:53:14 AM UTC-7, db wrote:

(snip)

> However, one more question. Your suggestions worked and I implemented
> them for the actual long C++ function that is the object of all this.
> I had given up on the original declarations of long double because
> Fortran didn't seem to want to know it, and reduced the C++ code to
> plain double, stating (c_double) in the Fortran calling program.
> How do I specify, in the Fortran code, long double? It would be
> equivalent to quad precision which I can ask for, 30 digits.
C allows for long double to be the same as long.

Not much hardware supports a 30 digit floating point type.

(IBM successors to S/360, including z/architecture,
VAX H_float, IBM POWER9, support it in hardware.)

The Fortran compiler should have the constant C_LONG_DOUBLE,
but it will be a negative value is the companion C processor
doesn't support it. I think that means if isn't more than double.

You can:

PRINT *, C_LONG_DOUBLE

Some compilers support 30 digits in software emulation.

Re: Calling a C++ function from Forttran

<c9dae701-63b0-4bfa-94b1-b54d6fd528adn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3633&group=comp.lang.fortran#3633

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:6214:4c1a:b0:647:2b82:5fd9 with SMTP id qh26-20020a0562144c1a00b006472b825fd9mr189937qvb.10.1696278627370;
Mon, 02 Oct 2023 13:30:27 -0700 (PDT)
X-Received: by 2002:a05:6808:1891:b0:3a3:d677:9a8d with SMTP id
bi17-20020a056808189100b003a3d6779a8dmr6019136oib.0.1696278627084; Mon, 02
Oct 2023 13:30:27 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Mon, 2 Oct 2023 13:30:26 -0700 (PDT)
In-Reply-To: <ufe3u6$2smq8$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=136.226.81.23; posting-account=ZZXq9AoAAAAQEcA7zKAGm0UFQh4gMBv7
NNTP-Posting-Host: 136.226.81.23
References: <ud4qeq$1gqvg$1@dont-email.me> <uf98k5$t65i$1@dont-email.me>
<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com> <ufbpk1$1ifq9$1@dont-email.me>
<ufe3u6$2smq8$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <c9dae701-63b0-4bfa-94b1-b54d6fd528adn@googlegroups.com>
Subject: Re: Calling a C++ function from Forttran
From: parekhvs@gmail.com (FortranFan)
Injection-Date: Mon, 02 Oct 2023 20:30:27 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2917
 by: FortranFan - Mon, 2 Oct 2023 20:30 UTC

On Monday, October 2, 2023 at 5:53:14 AM UTC-4, db wrote:

> ..
> How do I specify, in the Fortran code, long double? It would be
> equivalent to quad precision which I can ask for, 30 digits. ..

@db,

Since you appear to be using the GCC/gfortran system, refer to their documentation - see a couple of links below, especially the last one:

https://gcc.gnu.org/onlinedocs/gfortran/index.html#SEC_Contents
https://gcc.gnu.org/onlinedocs/gfortran/index.html#toc-Mixed-Language-Programming-1
https://gcc.gnu.org/onlinedocs/gfortran/ISO_005fC_005fBINDING.html

So with the last link, you will notice GCC/gfortran processor provides you with at least two higher precision options: one toward `C long double` and another for `C _Float128`.

Then you can evaluate these two options in terms of precision as follows:

--- begin console output ---
C:\temp>type p.f
use, intrinsic :: iso_c_binding, only : LD => c_long_double, F128 => c_float128
print *, "Precision with C long double: ", precision(1.0_ld)
print *, "Precision with C _Float128: ", precision(1.0_f128)
end

C:\temp>gfortran -ffree-form p.f -o p.exe

C:\temp>l.exe
Precision with C long double: 18
Precision with C _Float128: 33
--- end console output ---

Which then informs you the `C long double`, as implemented in the GCC/gfortran, will yield a precision of 18 digits whereas `C _Float128` can give you 33. Note both are likely to be software emulations given no native hardware support for higher floating points types such as 128-bit - as pointed out to you by @gah4.

Re: Calling a C++ function from Forttran

<ufjjrt$57ho$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3634&group=comp.lang.fortran#3634

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dieterhansbritz@gmail.com (db)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Wed, 4 Oct 2023 13:55:40 +0200
Organization: A noiseless patient Spider
Lines: 65
Message-ID: <ufjjrt$57ho$1@dont-email.me>
References: <ud4qeq$1gqvg$1@dont-email.me> <uf98k5$t65i$1@dont-email.me>
<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com>
<ufbpk1$1ifq9$1@dont-email.me> <ufe3u6$2smq8$1@dont-email.me>
<c9dae701-63b0-4bfa-94b1-b54d6fd528adn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 4 Oct 2023 11:55:41 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="19b6da5359cba0e6a9bdc752388dbaa1";
logging-data="171576"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ro7wstdeNAKu1bZmb5BWAEqhG4xMW3iQ="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:D3/HYvdaN8YbVXdbBe8+2+vL7u8=
Content-Language: en-US
In-Reply-To: <c9dae701-63b0-4bfa-94b1-b54d6fd528adn@googlegroups.com>
 by: db - Wed, 4 Oct 2023 11:55 UTC

On 02.10.2023 22.30, FortranFan wrote:
> On Monday, October 2, 2023 at 5:53:14 AM UTC-4, db wrote:
>
>> ..
>> How do I specify, in the Fortran code, long double? It would be
>> equivalent to quad precision which I can ask for, 30 digits. ..
>
> @db,
>
> Since you appear to be using the GCC/gfortran system, refer to their documentation - see a couple of links below, especially the last one:
>
> https://gcc.gnu.org/onlinedocs/gfortran/index.html#SEC_Contents
> https://gcc.gnu.org/onlinedocs/gfortran/index.html#toc-Mixed-Language-Programming-1
> https://gcc.gnu.org/onlinedocs/gfortran/ISO_005fC_005fBINDING.html
>
> So with the last link, you will notice GCC/gfortran processor provides you with at least two higher precision options: one toward `C long double` and another for `C _Float128`.
>
> Then you can evaluate these two options in terms of precision as follows:
>
> --- begin console output ---
> C:\temp>type p.f
> use, intrinsic :: iso_c_binding, only : LD => c_long_double, F128 => c_float128
> print *, "Precision with C long double: ", precision(1.0_ld)
> print *, "Precision with C _Float128: ", precision(1.0_f128)
> end
>
> C:\temp>gfortran -ffree-form p.f -o p.exe
>
> C:\temp>l.exe
> Precision with C long double: 18
> Precision with C _Float128: 33
> --- end console output ---
>
> Which then informs you the `C long double`, as implemented in the GCC/gfortran, will yield a precision of 18 digits whereas `C _Float128` can give you 33. Note both are likely to be software emulations given no native hardware support for higher floating points types such as 128-bit - as pointed out to you by @gah4.

OK, thanks for clearing that up.
My friend, who wrote the C++ function I am calling from Fortran,
has now modified it so that it takes a plain double argument
and returns a plain double result, but internally uses long
double, thus preserving the original precision. This now
works, my Fortran function calls it succesfully.
Now I want to put it into the object library. Until now, I
have put functions and subroutines into it by first compiling
them using
gfortran -o x.o x.f90
and then using ar to stash x.o. Now I must compile both the
Fortran function and the C++ one it calls. I therefore tried
it with
> gfortran -o iband_double.o iband_double.cpp fortiband.f90

and got this error message

/usr/bin/ld:
/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in
function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status

It seems that the compiler expects a main program, rather than
the job to make an object file. How do I convince it to do that?

--
Dieter Britz

Re: Calling a C++ function from Forttran

<03087ad1-1d7e-4f40-a522-e199f8ac4405n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3635&group=comp.lang.fortran#3635

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:a05:620a:84c7:b0:76d:567a:42f0 with SMTP id pq7-20020a05620a84c700b0076d567a42f0mr30032qkn.3.1696423164825;
Wed, 04 Oct 2023 05:39:24 -0700 (PDT)
X-Received: by 2002:a05:6870:72c7:b0:1e1:3367:1429 with SMTP id
o7-20020a05687072c700b001e133671429mr906818oak.10.1696423164609; Wed, 04 Oct
2023 05:39:24 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Wed, 4 Oct 2023 05:39:24 -0700 (PDT)
In-Reply-To: <ufjjrt$57ho$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=131.169.210.75; posting-account=nzBwnQoAAACNPvrl2_g4iY27wcwaeeNX
NNTP-Posting-Host: 131.169.210.75
References: <ud4qeq$1gqvg$1@dont-email.me> <uf98k5$t65i$1@dont-email.me>
<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com> <ufbpk1$1ifq9$1@dont-email.me>
<ufe3u6$2smq8$1@dont-email.me> <c9dae701-63b0-4bfa-94b1-b54d6fd528adn@googlegroups.com>
<ufjjrt$57ho$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <03087ad1-1d7e-4f40-a522-e199f8ac4405n@googlegroups.com>
Subject: Re: Calling a C++ function from Forttran
From: juergen.reuter@desy.de (feanor)
Injection-Date: Wed, 04 Oct 2023 12:39:24 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2787
 by: feanor - Wed, 4 Oct 2023 12:39 UTC

db schrieb am Mittwoch, 4. Oktober 2023 um 13:55:45 UTC+2:
> On 02.10.2023 22.30, FortranFan wrote:
> > On Monday, October 2, 2023 at 5:53:14 AM UTC-4, db wrote:
> >
> >> ..
> >> How do I specify, in the Fortran code, long double? It would be
> >> equivalent to quad precision which I can ask for, 30 digits. ..
> >
> > @db,
> >

> it with
> > gfortran -o iband_double.o iband_double.cpp fortiband.f90
>
> and got this error message
>
> /usr/bin/ld:
> /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in
> function `_start':
> (.text+0x1b): undefined reference to `main'
> collect2: error: ld returned 1 exit status
>
> It seems that the compiler expects a main program, rather than
> the job to make an object file. How do I convince it to do that?
>
>
> --

There are several aspects here: you cannot compile both a C++ and a Fortran file at the same time,
you have to compile them separately with the C++ and Fortran, correspondingly. And then you link
them together, either into a shared library or an executable. For this is I usually take the C or C++
compiler (resp. its corresponding linker). Or you link the object files from C++ and Fortran code together
into a shared library. Alternatively you can use, as you mentioned, ar to create a static library.

Re: Calling a C++ function from Forttran

<ufjndi$623n$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3636&group=comp.lang.fortran#3636

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dieterhansbritz@gmail.com (db)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Wed, 4 Oct 2023 14:56:18 +0200
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <ufjndi$623n$1@dont-email.me>
References: <ud4qeq$1gqvg$1@dont-email.me> <uf98k5$t65i$1@dont-email.me>
<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com>
<ufbpk1$1ifq9$1@dont-email.me> <ufe3u6$2smq8$1@dont-email.me>
<c9dae701-63b0-4bfa-94b1-b54d6fd528adn@googlegroups.com>
<ufjjrt$57ho$1@dont-email.me>
<03087ad1-1d7e-4f40-a522-e199f8ac4405n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 4 Oct 2023 12:56:18 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="19b6da5359cba0e6a9bdc752388dbaa1";
logging-data="198775"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hDPwpBGS0Gewx44bR0BIxMigz4jAGq2w="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:liCTzpklY/y+OPd7+rEZjzE6FPI=
In-Reply-To: <03087ad1-1d7e-4f40-a522-e199f8ac4405n@googlegroups.com>
Content-Language: en-US
 by: db - Wed, 4 Oct 2023 12:56 UTC

On 04.10.2023 14.39, feanor wrote:
> db schrieb am Mittwoch, 4. Oktober 2023 um 13:55:45 UTC+2:
>> On 02.10.2023 22.30, FortranFan wrote:
>>> On Monday, October 2, 2023 at 5:53:14 AM UTC-4, db wrote:
>>>
>>>> ..
>>>> How do I specify, in the Fortran code, long double? It would be
>>>> equivalent to quad precision which I can ask for, 30 digits. ..
>>>
>>> @db,
>>>
>
>> it with
>>> gfortran -o iband_double.o iband_double.cpp fortiband.f90
>>
>> and got this error message
>>
>> /usr/bin/ld:
>> /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in
>> function `_start':
>> (.text+0x1b): undefined reference to `main'
>> collect2: error: ld returned 1 exit status
>>
>> It seems that the compiler expects a main program, rather than
>> the job to make an object file. How do I convince it to do that?
>>
>>
>> --
>
> There are several aspects here: you cannot compile both a C++ and a Fortran file at the same time,
> you have to compile them separately with the C++ and Fortran, correspondingly. And then you link
> them together, either into a shared library or an executable. For this is I usually take the C or C++
> compiler (resp. its corresponding linker). Or you link the object files from C++ and Fortran code together
> into a shared library. Alternatively you can use, as you mentioned, ar to create a static library.
>
>

Yeah, sorry, I realised that and they work now. Thank you all for your
patience and help.
--
Dieter Britz

Re: Calling a C++ function from Forttran

<1a9c281e-7638-469b-b2c5-1ff6975befecn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3637&group=comp.lang.fortran#3637

  copy link   Newsgroups: comp.lang.fortran
X-Received: by 2002:ad4:5883:0:b0:65b:7a2:eebd with SMTP id dz3-20020ad45883000000b0065b07a2eebdmr32431qvb.9.1696430441735;
Wed, 04 Oct 2023 07:40:41 -0700 (PDT)
X-Received: by 2002:a9d:6842:0:b0:6af:9f8b:c606 with SMTP id
c2-20020a9d6842000000b006af9f8bc606mr708322oto.0.1696430441544; Wed, 04 Oct
2023 07:40:41 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.fortran
Date: Wed, 4 Oct 2023 07:40:41 -0700 (PDT)
In-Reply-To: <ufjndi$623n$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=2601:602:9700:4689:c54d:c365:8221:2da9;
posting-account=gLDX1AkAAAA26M5HM-O3sVMAXdxK9FPA
NNTP-Posting-Host: 2601:602:9700:4689:c54d:c365:8221:2da9
References: <ud4qeq$1gqvg$1@dont-email.me> <uf98k5$t65i$1@dont-email.me>
<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com> <ufbpk1$1ifq9$1@dont-email.me>
<ufe3u6$2smq8$1@dont-email.me> <c9dae701-63b0-4bfa-94b1-b54d6fd528adn@googlegroups.com>
<ufjjrt$57ho$1@dont-email.me> <03087ad1-1d7e-4f40-a522-e199f8ac4405n@googlegroups.com>
<ufjndi$623n$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <1a9c281e-7638-469b-b2c5-1ff6975befecn@googlegroups.com>
Subject: Re: Calling a C++ function from Forttran
From: gah4@u.washington.edu (gah4)
Injection-Date: Wed, 04 Oct 2023 14:40:41 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 1849
 by: gah4 - Wed, 4 Oct 2023 14:40 UTC

On Wednesday, October 4, 2023 at 5:56:22 AM UTC-7, db wrote:

(snip)

> Yeah, sorry, I realised that and they work now. Thank you all for your
> patience and help.

Most important, you need the -c option.

Otherwise, it tries to compile and link the program.

And, confusingly, will name the result what you put in the -o option.

Re: Calling a C++ function from Forttran

<ufjvur$binb$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=3638&group=comp.lang.fortran#3638

  copy link   Newsgroups: comp.lang.fortran
Path: i2pn2.org!i2pn.org!news.chmurka.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: sgk@REMOVEtroutmask.apl.washington.edu (Steven G. Kargl)
Newsgroups: comp.lang.fortran
Subject: Re: Calling a C++ function from Forttran
Date: Wed, 4 Oct 2023 15:22:04 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 63
Message-ID: <ufjvur$binb$1@dont-email.me>
References: <ud4qeq$1gqvg$1@dont-email.me> <uf98k5$t65i$1@dont-email.me>
<e3a87908-b940-454d-bd37-fa8a167de1a8n@googlegroups.com>
<ufbpk1$1ifq9$1@dont-email.me> <ufe3u6$2smq8$1@dont-email.me>
<c9dae701-63b0-4bfa-94b1-b54d6fd528adn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 4 Oct 2023 15:22:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ea7c9457636a1cc59a948a8ef4d94759";
logging-data="379627"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18AFrsRDVJrjXP9dTPQGqfp"
User-Agent: Pan/0.145 (Duplicitous mercenary valetism; d7e168a
git.gnome.org/pan2)
Cancel-Lock: sha1:EGVJYtgDkaVuUSi5C1bEyEjxFN8=
 by: Steven G. Kargl - Wed, 4 Oct 2023 15:22 UTC

On Mon, 02 Oct 2023 13:30:26 -0700, FortranFan wrote:

>
> C:\temp>gfortran -ffree-form p.f -o p.exe
>
> C:\temp>l.exe
> Precision with C long double: 18
> Precision with C _Float128: 33
> --- end console output ---
>
> Which then informs you the `C long double`, as implemented in
> the GCC/gfortran, will yield a precision of 18 digits whereas
> `C _Float128` can give you 33. Note both are likely to be
> software emulations given no native hardware support for
> higher floating points types such as 128-bit - as pointed
> out to you by @gah4.

Wrong. If you have a c_long_double that return 18 as
in the above, then it is using Intel 80-bit extended
double. The gcc backend will use whatever hardward on
the CPU If you hardware supports 128-bit floating point,
then gcc/gfortran will use the hard support. On Intel
class hardware, there isn't 128-bit FPU support, so it
is emulated.

% cat a.f90
function foo(x)
use iso_c_binding, only : fp => c_long_double
real(fp) foo
real(fp), value :: x
foo = x + x
end function foo

% gfcx -S -O3 -march=native -mtune=native a.f90
% cat a.s
.file "a.f90"
.text
.p2align 5
.globl foo_
.type foo_, @function
foo_:
..LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
fldt 16(%rbp)
popq %rbp
.cfi_def_cfa 7, 8
fadd %st(0), %st
ret
.cfi_endproc
..LFE0:
.size foo_, .-foo_
.ident "GCC: (GNU) 14.0.0 20230812 (experimental)"
.section .note.GNU-stack,"",@progbits

HTH

--
steve

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor