Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"Just the facts, Ma'am" -- Joe Friday


devel / comp.lang.c++ / Re: Problem with calling C++ from Fortran

SubjectAuthor
* Problem with calling C++ from Fortrandb
+* Re: Problem with calling C++ from FortranMuttley
|+* Re: Problem with calling C++ from Fortrandb
||+- Re: Problem with calling C++ from FortranMuttley
||+* Re: Problem with calling C++ from Fortrandb
|||+* Re: Problem with calling C++ from FortranMuttley
||||`* Re: Problem with calling C++ from Fortrandb
|||| +- Re: Problem with calling C++ from FortranPaavo Helde
|||| `- Re: Problem with calling C++ from FortranMuttley
|||+* Re: Problem with calling C++ from FortranPaavo Helde
||||+- Re: Problem with calling C++ from Fortrandb
||||`- Re: Problem with calling C++ from FortranJames Kuyper
|||`- Re: Problem with calling C++ from FortranBo Persson
||`- Re: Problem with calling C++ from FortranAlf P. Steinbach
|`* Re: Problem with calling C++ from Fortrandb
| `* Re: Problem with calling C++ from FortranBen Bacarisse
|  +- Re: Problem with calling C++ from FortranBen Bacarisse
|  `* Re: Problem with calling C++ from Fortrandb
|   +- Re: Problem with calling C++ from FortranMichael S
|   +- Re: Problem with calling C++ from FortranMuttley
|   `* Re: Problem with calling C++ from FortranBen Bacarisse
|    +* Re: Problem with calling C++ from Fortrandb
|    |+- Re: Problem with calling C++ from FortranBen Bacarisse
|    |`- Re: Problem with calling C++ from FortranMichael S
|    `* Re: Problem with calling C++ from FortranMichael S
|     `* Re: Problem with calling C++ from FortranBen Bacarisse
|      `* Re: Problem with calling C++ from FortranMichael S
|       `* Re: Problem with calling C++ from Fortrandb
|        `- Re: Problem with calling C++ from FortranMichael S
+- Re: Problem with calling C++ from FortranMichael S
`* Re: Problem with calling C++ from FortranMichael S
 `- Re: Problem with calling C++ from FortranMichael S

Pages:12
Re: Problem with calling C++ from Fortran

<2ccb965a-8d31-48af-97ad-2d96daa4862dn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1465&group=comp.lang.c%2B%2B#1465

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ac8:5a89:0:b0:412:2954:3451 with SMTP id c9-20020ac85a89000000b0041229543451mr245826qtc.0.1694509253498;
Tue, 12 Sep 2023 02:00:53 -0700 (PDT)
X-Received: by 2002:a17:90b:8d5:b0:26d:232d:8c32 with SMTP id
ds21-20020a17090b08d500b0026d232d8c32mr3225973pjb.9.1694509252830; Tue, 12
Sep 2023 02:00:52 -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.c++
Date: Tue, 12 Sep 2023 02:00:52 -0700 (PDT)
In-Reply-To: <87cyyoqoka.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <udhbs9$e5d$1@dont-email.me> <udhcaj$j8l$1@dont-email.me>
<udkdu9$itm4$1@dont-email.me> <87o7iajh8b.fsf@bsb.me.uk> <udmvg7$11r4u$1@dont-email.me>
<87cyyoqoka.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <2ccb965a-8d31-48af-97ad-2d96daa4862dn@googlegroups.com>
Subject: Re: Problem with calling C++ from Fortran
From: already5chosen@yahoo.com (Michael S)
Injection-Date: Tue, 12 Sep 2023 09:00:53 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Michael S - Tue, 12 Sep 2023 09:00 UTC

On Tuesday, September 12, 2023 at 2:26:49 AM UTC+3, Ben Bacarisse wrote:
> db <dieterh...@gmail.com> writes:
>
> > On 10.09.2023 15.22, Ben Bacarisse wrote:
> >> db <dieterh...@gmail.com> writes:
> >>
> >>> On 09.09.2023 11.02, Mut...@dastardlyhq.com wrote:
> >>>> On Sat, 9 Sep 2023 10:54:33 +0200
> >>>> db <dieterh...@gmail.com> wrote:
> >>>>> but now it says that C-double doesn't exist.
> >>>>>
> >>>>> Any ideas?
> >>>> C++ mangles function names so that the name you give the function isn't
> >>>> the
> >>>> name the dynamic loader sees in the binary. You need to put this around whatever
> >>>> function you want to call from Fortran so it has C style linkage which means
> >>>> the name remains the same:
> >>>> extern "C" {
> >>>> void calledFromFortran()
> >>>> {
> >>>> :
> >>>> }
> >>>> }
> >>>>
> >>> Where should this go in the code? I added it at the end as is,
> >>> and now I get this:
> >>>
> >>> ~/ownlib90/tests> ./jbandtest
> >>> jband.cpp:88:1: error: expected initializer before 'extern'
> >>> 88 | extern "C" {
> >>> | ^~~~~~
> >>> jband.cpp:96:1: error: expected unqualified-id before '{' token
> >>> 96 | {
> >>> | ^
> >>> jband.cpp: In function 'void calledFromFortran()':
> >>> jband.cpp:350:9: error: expected primary-expression before ':' token
> >>> 350 | :
> >>> ...
> >> You are giving people very little to go on. There's an unknown command
> >> (./jbandtest) the gives some errors where only one line of code can be
> >> seen.
> >> Can you produce a cut-down version to the program that shows the same
> >> error -- mimimal example of the C++ and of the Fortran that should
> >> calls it but which fails? That would mean you could post the code.
> >> If that is not possible, can you link to the source so those who know
> >> both C++ and Fortran can take a proper look?
> >
> > I put the C++ function here: www.dieterbritz.dk/jband.cpp
> > After getting errors about the extern lines, whch I had
> > added at the end, I shifted it to near the start:
> >
> > #include <iostream>
> > #include <fstream>
> > #include <iomanip>
> > #include "math.h"
> >
> > using namespace std;
> >
> > extern "C" {
> >
> > void calledFromFortran()
> > {
> > :
> > }
> >
> > }
> Remove them all. You have two options. One: correct the C++. To do
> this put this, and only this,
>
> extern "C" {
> const long double iband(const long double tbar);
> }
>
> just after the #include lines. This is perfect (the first const is a
> meaningless word here and the compiler may warn you about that) but it
> will do.
>
> Next replace
>
> #include "math.h"
>
> with
>
> #include <math.h>
>
> I'd also remove the two unnecessary #include lines (for iomanip and
> fstream). All of these are "tidying up". Only the extern "C" part I
> gave corrects an actual mistake.
>
> The second option is the turn the code in C which links directly with
> Fortran. The only C++ used by the code is one "cout << " line (not a
> good way to report an error, but that's not important right now) and a
> setting for pi that is not a compile-time constant (4*atanl(1)) written
> in a more fussy way.
>
> To convert to C, copy the file to jband.c (rather than .cpp) and replce
> the declaration for pi. Given the code is littered with explicit 21
> digits decimals (a little more than the precision of C's usualy long
> double type) I would just write
>
> static const long double pi = 3.14159265358979323846L;
>
> Now replace the cout line with
>
> fputs("\nError in stredges::iband(): tbar must be > 0", stderr);
>
> This writes the message to the error stream and put only these two
> include lines at the top:
>
> #include <math.h>
> #include <stdio.h>
>
> Now the file is C code.
>
> Frankly, this is the way I'd go, since linking C is just that bit
> simpler than linking C++. But if the code is going to become fully
> fledged (and complex) C++, obviously use my first option.
> > I still get errors pertaining to these lines. But after adding
> > the -c to the two compilation lines in the script, these are
> > now the only errors. I think I am getting closer. The errors
> > are now
> Yes, you must compile it like this:
>
> g++ -c jband.cpp (for C++ code)
> gcc -c jband.c (for C code)
>
> You don't need any -o but you could add in lots of warnings like
>
> gcc -Wall -Wextra -c jband.c
> > jband.cpp: In function 'void calledFromFortran()':
> > jband.cpp:12:9: error: expected primary-expression before ':' token
> > 12 | :
> > | ^
> > jband.cpp: At global scope:
> > jband.cpp:96:1: error: expected initializer before 'extern'
> > 96 | extern "C" {
> > | ^~~~~~
> > jband.cpp:104:1: error: expected unqualified-id before '{' token
> > 104 | {
> > | ^
> > jbandtest.f90:28:14:
> >
> > 28 | FORTJBAND = JBAND (tbar)
> > | 1
> > Error: Type mismatch in argument 'tbar' at (1); passed REAL(16) to REAL(10)
> >
> > The last error will go away together with the others.
> Michael S has given you one way to go for that. My comments were going
> to be more general. You have several ways of specifying the types, and
> I could not tell if you really wanted all of them. In general, I would
> define the Fortran types from the C ones (since those are the types you
> need for the call and return) and leave it at that.
>
> But the big one is that the Fortran code seems to think the function is
> called jband, when in fact it's called iband.
>
> I went for this:
>
> module STUFF
> use iso_c_binding
> integer, parameter :: qud=real(c_long_double)
> end module STUFF
> program JBAND_TEST
> use STUFF; implicit none
> real(qud) :: tbar, curr, FORTJBAND
> 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 IBAND (tbar) bind(c)
> use STUFF
> real(qud), VALUE :: tbar
> real(qud) :: IBAND
> end function iband
> end interface

I think, you are taking chances here. Even if it happens to work
on x86-64 Linux, it can fail on some other target where calling
conventions for long double return value in 'C' differ from those
of Fortran. The method shown in one of my previous posts
appear more robust:

interface
real(c_long_double) function IBAND (tbar) bind(c)
import c_long_double
real(c_long_double), value :: tbar
end function IBAND

> real(qud) :: FORTJBAND, tbar
> FORTJBAND = IBAND(tbar)
> end function FORTJBAND
>
> Note that you can compile the Fortran along with the C at the same time
> if you like:
>
> $ gfortran -o jtest jbandtest.f90 jband.c -lm
> $ ./jtest
> 1
> curr = 1.5224
> 1e-10
> curr =56419.9584
> -1
> $
>
> ($ is my Linux command-line prompt). I have no idea if these are
> correct results and I can't be sure what you need to run on your system,
> but the code links and appears to work.
>
> If you go with the C++ option, the linking needs the standard C++
> library:
>
> $ gfortran -o jtest jbandtest.f90 jband.cpp -lstdc++ -lm
> $ ./jtest
> 1
> curr = 1.5224
> 1e-10
> curr =56419.9584
> -1
> $
>
> BTW, you said the C++ was written by a C++ expert. I don't think it
> was!

With no relationships to C, C++ or Fortran, I don't like the way they evaluate
their polynomials from numerical perspective . But I assume that algorithm
was copied 'as is' from 40 y.o. book so it's not a fault of C++ programmer.


Click here to read the complete article
Re: Problem with calling C++ from Fortran

<87cyynpm90.fsf@bsb.me.uk>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1467&group=comp.lang.c%2B%2B#1467

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.usenet@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c++
Subject: Re: Problem with calling C++ from Fortran
Date: Tue, 12 Sep 2023 14:14:03 +0100
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <87cyynpm90.fsf@bsb.me.uk>
References: <udhbs9$e5d$1@dont-email.me> <udhcaj$j8l$1@dont-email.me>
<udkdu9$itm4$1@dont-email.me> <87o7iajh8b.fsf@bsb.me.uk>
<udmvg7$11r4u$1@dont-email.me> <87cyyoqoka.fsf@bsb.me.uk>
<udp8l4$1frsv$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="0b3d05af22d1f5f0f239039facdde43d";
logging-data="1662275"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FP35JNpikUtB3e/UOXxHz5Hv5DCQsVPo="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:QxxxAqkfrnWkfwMM/Rkesk9UfPU=
sha1:LE+fr7O5G7yMYT2HHUUwTTzRrG8=
X-BSB-Auth: 1.3a07ebe5454fef238a8e.20230912141403BST.87cyynpm90.fsf@bsb.me.uk
 by: Ben Bacarisse - Tue, 12 Sep 2023 13:14 UTC

db <dieterhansbritz@gmail.com> writes:

>> BTW, you said the C++ was written by a C++ expert. I don't think it
>> was!
>>
> The code, apart from the extra lines suggested by someone here,
> but without the information on where to put them, is runnable
> C++ code written by another person who knows C++ very well.

I was just saying that the code does not look like it was written by
someone who knows C++ very well. All those unnecessary duplicated
sizes. A const qualified function return type. Including "math.h"
rather that either <math.h> or <cmath>.

> I myself use Fortran and you are right, I know next to nothing about
> C++.

Did I say that? Sorry.

> I did use the extra code exactly as is, but possibly
> put it in the wrong place. I tried two, without success.
> I am giving up, and will translate the C++ function into Fortran,
> where I know what I'm doing.

OK. Odd to give up just as two people have given you working code. Oh
well...

Mind you, the C++ (or C) just picks an array and an initial value (an
'x' in a polynomial evaluation) and calculates a simple sum. It will be
trivial in Fortran. The hard work is in the coefficients and those can
just be coped.

--
Ben.

Re: Problem with calling C++ from Fortran

<87a5trplua.fsf@bsb.me.uk>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1468&group=comp.lang.c%2B%2B#1468

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.usenet@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c++
Subject: Re: Problem with calling C++ from Fortran
Date: Tue, 12 Sep 2023 14:22:53 +0100
Organization: A noiseless patient Spider
Lines: 228
Message-ID: <87a5trplua.fsf@bsb.me.uk>
References: <udhbs9$e5d$1@dont-email.me> <udhcaj$j8l$1@dont-email.me>
<udkdu9$itm4$1@dont-email.me> <87o7iajh8b.fsf@bsb.me.uk>
<udmvg7$11r4u$1@dont-email.me> <87cyyoqoka.fsf@bsb.me.uk>
<2ccb965a-8d31-48af-97ad-2d96daa4862dn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="0b3d05af22d1f5f0f239039facdde43d";
logging-data="1662275"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19O2udUvXgiZw89/9ibHKJPjdjEk5bY9lo="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:KGc9VGkdjPEd1P8UbWiuN7WsTWg=
sha1:TrqZUu72ukYoPukjyVDJLGtKcLY=
X-BSB-Auth: 1.4ac430060a7e3c2358b2.20230912142253BST.87a5trplua.fsf@bsb.me.uk
 by: Ben Bacarisse - Tue, 12 Sep 2023 13:22 UTC

Michael S <already5chosen@yahoo.com> writes:

> On Tuesday, September 12, 2023 at 2:26:49 AM UTC+3, Ben Bacarisse wrote:
>> db <dieterh...@gmail.com> writes:
>>
>> > On 10.09.2023 15.22, Ben Bacarisse wrote:
>> >> db <dieterh...@gmail.com> writes:
>> >>
>> >>> On 09.09.2023 11.02, Mut...@dastardlyhq.com wrote:
>> >>>> On Sat, 9 Sep 2023 10:54:33 +0200
>> >>>> db <dieterh...@gmail.com> wrote:
>> >>>>> but now it says that C-double doesn't exist.
>> >>>>>
>> >>>>> Any ideas?
>> >>>> C++ mangles function names so that the name you give the function isn't
>> >>>> the
>> >>>> name the dynamic loader sees in the binary. You need to put this around whatever
>> >>>> function you want to call from Fortran so it has C style linkage which means
>> >>>> the name remains the same:
>> >>>> extern "C" {
>> >>>> void calledFromFortran()
>> >>>> {
>> >>>> :
>> >>>> }
>> >>>> }
>> >>>>
>> >>> Where should this go in the code? I added it at the end as is,
>> >>> and now I get this:
>> >>>
>> >>> ~/ownlib90/tests> ./jbandtest
>> >>> jband.cpp:88:1: error: expected initializer before 'extern'
>> >>> 88 | extern "C" {
>> >>> | ^~~~~~
>> >>> jband.cpp:96:1: error: expected unqualified-id before '{' token
>> >>> 96 | {
>> >>> | ^
>> >>> jband.cpp: In function 'void calledFromFortran()':
>> >>> jband.cpp:350:9: error: expected primary-expression before ':' token
>> >>> 350 | :
>> >>> ...
>> >> You are giving people very little to go on. There's an unknown command
>> >> (./jbandtest) the gives some errors where only one line of code can be
>> >> seen.
>> >> Can you produce a cut-down version to the program that shows the same
>> >> error -- mimimal example of the C++ and of the Fortran that should
>> >> calls it but which fails? That would mean you could post the code.
>> >> If that is not possible, can you link to the source so those who know
>> >> both C++ and Fortran can take a proper look?
>> >
>> > I put the C++ function here: www.dieterbritz.dk/jband.cpp
>> > After getting errors about the extern lines, whch I had
>> > added at the end, I shifted it to near the start:
>> >
>> > #include <iostream>
>> > #include <fstream>
>> > #include <iomanip>
>> > #include "math.h"
>> >
>> > using namespace std;
>> >
>> > extern "C" {
>> >
>> > void calledFromFortran()
>> > {
>> > :
>> > }
>> >
>> > }
>> Remove them all. You have two options. One: correct the C++. To do
>> this put this, and only this,
>>
>> extern "C" {
>> const long double iband(const long double tbar);
>> }
>>
>> just after the #include lines. This is perfect (the first const is a
>> meaningless word here and the compiler may warn you about that) but it
>> will do.
>>
>> Next replace
>>
>> #include "math.h"
>>
>> with
>>
>> #include <math.h>
>>
>> I'd also remove the two unnecessary #include lines (for iomanip and
>> fstream). All of these are "tidying up". Only the extern "C" part I
>> gave corrects an actual mistake.
>>
>> The second option is the turn the code in C which links directly with
>> Fortran. The only C++ used by the code is one "cout << " line (not a
>> good way to report an error, but that's not important right now) and a
>> setting for pi that is not a compile-time constant (4*atanl(1)) written
>> in a more fussy way.
>>
>> To convert to C, copy the file to jband.c (rather than .cpp) and replce
>> the declaration for pi. Given the code is littered with explicit 21
>> digits decimals (a little more than the precision of C's usualy long
>> double type) I would just write
>>
>> static const long double pi = 3.14159265358979323846L;
>>
>> Now replace the cout line with
>>
>> fputs("\nError in stredges::iband(): tbar must be > 0", stderr);
>>
>> This writes the message to the error stream and put only these two
>> include lines at the top:
>>
>> #include <math.h>
>> #include <stdio.h>
>>
>> Now the file is C code.
>>
>> Frankly, this is the way I'd go, since linking C is just that bit
>> simpler than linking C++. But if the code is going to become fully
>> fledged (and complex) C++, obviously use my first option.
>> > I still get errors pertaining to these lines. But after adding
>> > the -c to the two compilation lines in the script, these are
>> > now the only errors. I think I am getting closer. The errors
>> > are now
>> Yes, you must compile it like this:
>>
>> g++ -c jband.cpp (for C++ code)
>> gcc -c jband.c (for C code)
>>
>> You don't need any -o but you could add in lots of warnings like
>>
>> gcc -Wall -Wextra -c jband.c
>> > jband.cpp: In function 'void calledFromFortran()':
>> > jband.cpp:12:9: error: expected primary-expression before ':' token
>> > 12 | :
>> > | ^
>> > jband.cpp: At global scope:
>> > jband.cpp:96:1: error: expected initializer before 'extern'
>> > 96 | extern "C" {
>> > | ^~~~~~
>> > jband.cpp:104:1: error: expected unqualified-id before '{' token
>> > 104 | {
>> > | ^
>> > jbandtest.f90:28:14:
>> >
>> > 28 | FORTJBAND = JBAND (tbar)
>> > | 1
>> > Error: Type mismatch in argument 'tbar' at (1); passed REAL(16) to REAL(10)
>> >
>> > The last error will go away together with the others.
>> Michael S has given you one way to go for that. My comments were going
>> to be more general. You have several ways of specifying the types, and
>> I could not tell if you really wanted all of them. In general, I would
>> define the Fortran types from the C ones (since those are the types you
>> need for the call and return) and leave it at that.
>>
>> But the big one is that the Fortran code seems to think the function is
>> called jband, when in fact it's called iband.
>>
>> I went for this:
>>
>> module STUFF
>> use iso_c_binding
>> integer, parameter :: qud=real(c_long_double)
>> end module STUFF
>> program JBAND_TEST
>> use STUFF; implicit none
>> real(qud) :: tbar, curr, FORTJBAND
>> 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 IBAND (tbar) bind(c)
>> use STUFF
>> real(qud), VALUE :: tbar
>> real(qud) :: IBAND
>> end function iband
>> end interface
>
> I think, you are taking chances here. Even if it happens to work
> on x86-64 Linux, it can fail on some other target where calling
> conventions for long double return value in 'C' differ from those
> of Fortran. The method shown in one of my previous posts
> appear more robust:
>
> interface
> real(c_long_double) function IBAND (tbar) bind(c)
> import c_long_double
> real(c_long_double), value :: tbar
> end function IBAND

I think I see -- keep the C-related types in the smallest interface? I
didn't study your example, but the OP renamed the C function and gave
the resulting Fortran function a native Fortran type. Is that what you
advise?

The OP should have taken your Fortran and applied my C++ edits. Maybe
they took my Fortran and it did not work on Windows. That would be a
shame.


Click here to read the complete article
Re: Problem with calling C++ from Fortran

<ac4c9220-8eb6-41d7-a5ad-522e33c55d28n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1469&group=comp.lang.c%2B%2B#1469

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:a05:620a:2b87:b0:76f:58a:8ffc with SMTP id dz7-20020a05620a2b8700b0076f058a8ffcmr231222qkb.7.1694528107561;
Tue, 12 Sep 2023 07:15:07 -0700 (PDT)
X-Received: by 2002:a17:902:f0ca:b0:1b9:df8f:888c with SMTP id
v10-20020a170902f0ca00b001b9df8f888cmr4078694pla.8.1694528107328; Tue, 12 Sep
2023 07:15:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.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.c++
Date: Tue, 12 Sep 2023 07:15:06 -0700 (PDT)
In-Reply-To: <udp8l4$1frsv$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <udhbs9$e5d$1@dont-email.me> <udhcaj$j8l$1@dont-email.me>
<udkdu9$itm4$1@dont-email.me> <87o7iajh8b.fsf@bsb.me.uk> <udmvg7$11r4u$1@dont-email.me>
<87cyyoqoka.fsf@bsb.me.uk> <udp8l4$1frsv$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ac4c9220-8eb6-41d7-a5ad-522e33c55d28n@googlegroups.com>
Subject: Re: Problem with calling C++ from Fortran
From: already5chosen@yahoo.com (Michael S)
Injection-Date: Tue, 12 Sep 2023 14:15:07 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 10198
 by: Michael S - Tue, 12 Sep 2023 14:15 UTC

On Tuesday, September 12, 2023 at 11:48:55 AM UTC+3, db wrote:
> On 12.09.2023 01.26, Ben Bacarisse wrote:
> > db <dieterh...@gmail.com> writes:
> >
> >> On 10.09.2023 15.22, Ben Bacarisse wrote:
> >>> db <dieterh...@gmail.com> writes:
> >>>
> >>>> On 09.09.2023 11.02, Mut...@dastardlyhq.com wrote:
> >>>>> On Sat, 9 Sep 2023 10:54:33 +0200
> >>>>> db <dieterh...@gmail.com> wrote:
> >>>>>> but now it says that C-double doesn't exist.
> >>>>>>
> >>>>>> Any ideas?
> >>>>> C++ mangles function names so that the name you give the function isn't
> >>>>> the
> >>>>> name the dynamic loader sees in the binary. You need to put this around whatever
> >>>>> function you want to call from Fortran so it has C style linkage which means
> >>>>> the name remains the same:
> >>>>> extern "C" {
> >>>>> void calledFromFortran()
> >>>>> {
> >>>>> :
> >>>>> }
> >>>>> }
> >>>>>
> >>>> Where should this go in the code? I added it at the end as is,
> >>>> and now I get this:
> >>>>
> >>>> ~/ownlib90/tests> ./jbandtest
> >>>> jband.cpp:88:1: error: expected initializer before 'extern'
> >>>> 88 | extern "C" {
> >>>> | ^~~~~~
> >>>> jband.cpp:96:1: error: expected unqualified-id before '{' token
> >>>> 96 | {
> >>>> | ^
> >>>> jband.cpp: In function 'void calledFromFortran()':
> >>>> jband.cpp:350:9: error: expected primary-expression before ':' token
> >>>> 350 | :
> >>>> ...
> >>> You are giving people very little to go on. There's an unknown command
> >>> (./jbandtest) the gives some errors where only one line of code can be
> >>> seen.
> >>> Can you produce a cut-down version to the program that shows the same
> >>> error -- mimimal example of the C++ and of the Fortran that should
> >>> calls it but which fails? That would mean you could post the code.
> >>> If that is not possible, can you link to the source so those who know
> >>> both C++ and Fortran can take a proper look?
> >>
> >> I put the C++ function here: www.dieterbritz.dk/jband.cpp
> >> After getting errors about the extern lines, whch I had
> >> added at the end, I shifted it to near the start:
> >>
> >> #include <iostream>
> >> #include <fstream>
> >> #include <iomanip>
> >> #include "math.h"
> >>
> >> using namespace std;
> >>
> >> extern "C" {
> >>
> >> void calledFromFortran()
> >> {
> >> :
> >> }
> >>
> >> }
> >
> > Remove them all. You have two options. One: correct the C++. To do
> > this put this, and only this,
> >
> > extern "C" {
> > const long double iband(const long double tbar);
> > }
> >
> > just after the #include lines. This is perfect (the first const is a
> > meaningless word here and the compiler may warn you about that) but it
> > will do.
> >
> > Next replace
> >
> > #include "math.h"
> >
> > with
> >
> > #include <math.h>
> >
> > I'd also remove the two unnecessary #include lines (for iomanip and
> > fstream). All of these are "tidying up". Only the extern "C" part I
> > gave corrects an actual mistake.
> >
> > The second option is the turn the code in C which links directly with
> > Fortran. The only C++ used by the code is one "cout << " line (not a
> > good way to report an error, but that's not important right now) and a
> > setting for pi that is not a compile-time constant (4*atanl(1)) written
> > in a more fussy way.
> >
> > To convert to C, copy the file to jband.c (rather than .cpp) and replce
> > the declaration for pi. Given the code is littered with explicit 21
> > digits decimals (a little more than the precision of C's usualy long
> > double type) I would just write
> >
> > static const long double pi = 3.14159265358979323846L;
> >
> > Now replace the cout line with
> >
> > fputs("\nError in stredges::iband(): tbar must be > 0", stderr);
> >
> > This writes the message to the error stream and put only these two
> > include lines at the top:
> >
> > #include <math.h>
> > #include <stdio.h>
> >
> > Now the file is C code.
> >
> > Frankly, this is the way I'd go, since linking C is just that bit
> > simpler than linking C++. But if the code is going to become fully
> > fledged (and complex) C++, obviously use my first option.
> >
> >> I still get errors pertaining to these lines. But after adding
> >> the -c to the two compilation lines in the script, these are
> >> now the only errors. I think I am getting closer. The errors
> >> are now
> >
> > Yes, you must compile it like this:
> >
> > g++ -c jband.cpp (for C++ code)
> > gcc -c jband.c (for C code)
> >
> > You don't need any -o but you could add in lots of warnings like
> >
> > gcc -Wall -Wextra -c jband.c
> >
> >> jband.cpp: In function 'void calledFromFortran()':
> >> jband.cpp:12:9: error: expected primary-expression before ':' token
> >> 12 | :
> >> | ^
> >> jband.cpp: At global scope:
> >> jband.cpp:96:1: error: expected initializer before 'extern'
> >> 96 | extern "C" {
> >> | ^~~~~~
> >> jband.cpp:104:1: error: expected unqualified-id before '{' token
> >> 104 | {
> >> | ^
> >> jbandtest.f90:28:14:
> >>
> >> 28 | FORTJBAND = JBAND (tbar)
> >> | 1
> >> Error: Type mismatch in argument 'tbar' at (1); passed REAL(16) to REAL(10)
> >>
> >> The last error will go away together with the others.
> >
> > Michael S has given you one way to go for that. My comments were going
> > to be more general. You have several ways of specifying the types, and
> > I could not tell if you really wanted all of them. In general, I would
> > define the Fortran types from the C ones (since those are the types you
> > need for the call and return) and leave it at that.
> >
> > But the big one is that the Fortran code seems to think the function is
> > called jband, when in fact it's called iband.
> >
> > I went for this:
> >
> > module STUFF
> > use iso_c_binding
> > integer, parameter :: qud=real(c_long_double)
> > end module STUFF
> > program JBAND_TEST
> > use STUFF; implicit none
> > real(qud) :: tbar, curr, FORTJBAND
> > 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 IBAND (tbar) bind(c)
> > use STUFF
> > real(qud), VALUE :: tbar
> > real(qud) :: IBAND
> > end function iband
> > end interface
> > real(qud) :: FORTJBAND, tbar
> > FORTJBAND = IBAND(tbar)
> > end function FORTJBAND
> >
> > Note that you can compile the Fortran along with the C at the same time
> > if you like:
> >
> > $ gfortran -o jtest jbandtest.f90 jband.c -lm
> > $ ./jtest
> > 1
> > curr = 1.5224
> > 1e-10
> > curr =56419.9584
> > -1
> > $
> >
> > ($ is my Linux command-line prompt). I have no idea if these are
> > correct results and I can't be sure what you need to run on your system,
> > but the code links and appears to work.
> >
> > If you go with the C++ option, the linking needs the standard C++
> > library:
> >
> > $ gfortran -o jtest jbandtest.f90 jband.cpp -lstdc++ -lm
> > $ ./jtest
> > 1
> > curr = 1.5224
> > 1e-10
> > curr =56419.9584
> > -1
> > $
> >
> > BTW, you said the C++ was written by a C++ expert. I don't think it
> > was!
> >
> The code, apart from the extra lines suggested by someone here,
> but without the information on where to put them, is runnable
> C++ code written by another person who knows C++ very well. I
> myself use Fortran and you are right, I know next to nothing
> about C++. I did use the extra code exactly as is, but possibly
> put it in the wrong place. I tried two, without success.
> I am giving up,


Click here to read the complete article
Re: Problem with calling C++ from Fortran

<f1b6f8e1-fa16-4fb7-b4b2-883788d098fdn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1470&group=comp.lang.c%2B%2B#1470

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ad4:4b6a:0:b0:63d:30b8:ff8b with SMTP id m10-20020ad44b6a000000b0063d30b8ff8bmr268889qvx.13.1694529175123;
Tue, 12 Sep 2023 07:32:55 -0700 (PDT)
X-Received: by 2002:a17:90a:778a:b0:274:920:a44c with SMTP id
v10-20020a17090a778a00b002740920a44cmr1825174pjk.3.1694529174503; Tue, 12 Sep
2023 07:32:54 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.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.c++
Date: Tue, 12 Sep 2023 07:32:53 -0700 (PDT)
In-Reply-To: <87a5trplua.fsf@bsb.me.uk>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <udhbs9$e5d$1@dont-email.me> <udhcaj$j8l$1@dont-email.me>
<udkdu9$itm4$1@dont-email.me> <87o7iajh8b.fsf@bsb.me.uk> <udmvg7$11r4u$1@dont-email.me>
<87cyyoqoka.fsf@bsb.me.uk> <2ccb965a-8d31-48af-97ad-2d96daa4862dn@googlegroups.com>
<87a5trplua.fsf@bsb.me.uk>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f1b6f8e1-fa16-4fb7-b4b2-883788d098fdn@googlegroups.com>
Subject: Re: Problem with calling C++ from Fortran
From: already5chosen@yahoo.com (Michael S)
Injection-Date: Tue, 12 Sep 2023 14:32:55 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 11124
 by: Michael S - Tue, 12 Sep 2023 14:32 UTC

On Tuesday, September 12, 2023 at 4:23:09 PM UTC+3, Ben Bacarisse wrote:
> Michael S <already...@yahoo.com> writes:
>
> > On Tuesday, September 12, 2023 at 2:26:49 AM UTC+3, Ben Bacarisse wrote:
> >> db <dieterh...@gmail.com> writes:
> >>
> >> > On 10.09.2023 15.22, Ben Bacarisse wrote:
> >> >> db <dieterh...@gmail.com> writes:
> >> >>
> >> >>> On 09.09.2023 11.02, Mut...@dastardlyhq.com wrote:
> >> >>>> On Sat, 9 Sep 2023 10:54:33 +0200
> >> >>>> db <dieterh...@gmail.com> wrote:
> >> >>>>> but now it says that C-double doesn't exist.
> >> >>>>>
> >> >>>>> Any ideas?
> >> >>>> C++ mangles function names so that the name you give the function isn't
> >> >>>> the
> >> >>>> name the dynamic loader sees in the binary. You need to put this around whatever
> >> >>>> function you want to call from Fortran so it has C style linkage which means
> >> >>>> the name remains the same:
> >> >>>> extern "C" {
> >> >>>> void calledFromFortran()
> >> >>>> {
> >> >>>> :
> >> >>>> }
> >> >>>> }
> >> >>>>
> >> >>> Where should this go in the code? I added it at the end as is,
> >> >>> and now I get this:
> >> >>>
> >> >>> ~/ownlib90/tests> ./jbandtest
> >> >>> jband.cpp:88:1: error: expected initializer before 'extern'
> >> >>> 88 | extern "C" {
> >> >>> | ^~~~~~
> >> >>> jband.cpp:96:1: error: expected unqualified-id before '{' token
> >> >>> 96 | {
> >> >>> | ^
> >> >>> jband.cpp: In function 'void calledFromFortran()':
> >> >>> jband.cpp:350:9: error: expected primary-expression before ':' token
> >> >>> 350 | :
> >> >>> ...
> >> >> You are giving people very little to go on. There's an unknown command
> >> >> (./jbandtest) the gives some errors where only one line of code can be
> >> >> seen.
> >> >> Can you produce a cut-down version to the program that shows the same
> >> >> error -- mimimal example of the C++ and of the Fortran that should
> >> >> calls it but which fails? That would mean you could post the code.
> >> >> If that is not possible, can you link to the source so those who know
> >> >> both C++ and Fortran can take a proper look?
> >> >
> >> > I put the C++ function here: www.dieterbritz.dk/jband.cpp
> >> > After getting errors about the extern lines, whch I had
> >> > added at the end, I shifted it to near the start:
> >> >
> >> > #include <iostream>
> >> > #include <fstream>
> >> > #include <iomanip>
> >> > #include "math.h"
> >> >
> >> > using namespace std;
> >> >
> >> > extern "C" {
> >> >
> >> > void calledFromFortran()
> >> > {
> >> > :
> >> > }
> >> >
> >> > }
> >> Remove them all. You have two options. One: correct the C++. To do
> >> this put this, and only this,
> >>
> >> extern "C" {
> >> const long double iband(const long double tbar);
> >> }
> >>
> >> just after the #include lines. This is perfect (the first const is a
> >> meaningless word here and the compiler may warn you about that) but it
> >> will do.
> >>
> >> Next replace
> >>
> >> #include "math.h"
> >>
> >> with
> >>
> >> #include <math.h>
> >>
> >> I'd also remove the two unnecessary #include lines (for iomanip and
> >> fstream). All of these are "tidying up". Only the extern "C" part I
> >> gave corrects an actual mistake.
> >>
> >> The second option is the turn the code in C which links directly with
> >> Fortran. The only C++ used by the code is one "cout << " line (not a
> >> good way to report an error, but that's not important right now) and a
> >> setting for pi that is not a compile-time constant (4*atanl(1)) written
> >> in a more fussy way.
> >>
> >> To convert to C, copy the file to jband.c (rather than .cpp) and replce
> >> the declaration for pi. Given the code is littered with explicit 21
> >> digits decimals (a little more than the precision of C's usualy long
> >> double type) I would just write
> >>
> >> static const long double pi = 3.14159265358979323846L;
> >>
> >> Now replace the cout line with
> >>
> >> fputs("\nError in stredges::iband(): tbar must be > 0", stderr);
> >>
> >> This writes the message to the error stream and put only these two
> >> include lines at the top:
> >>
> >> #include <math.h>
> >> #include <stdio.h>
> >>
> >> Now the file is C code.
> >>
> >> Frankly, this is the way I'd go, since linking C is just that bit
> >> simpler than linking C++. But if the code is going to become fully
> >> fledged (and complex) C++, obviously use my first option.
> >> > I still get errors pertaining to these lines. But after adding
> >> > the -c to the two compilation lines in the script, these are
> >> > now the only errors. I think I am getting closer. The errors
> >> > are now
> >> Yes, you must compile it like this:
> >>
> >> g++ -c jband.cpp (for C++ code)
> >> gcc -c jband.c (for C code)
> >>
> >> You don't need any -o but you could add in lots of warnings like
> >>
> >> gcc -Wall -Wextra -c jband.c
> >> > jband.cpp: In function 'void calledFromFortran()':
> >> > jband.cpp:12:9: error: expected primary-expression before ':' token
> >> > 12 | :
> >> > | ^
> >> > jband.cpp: At global scope:
> >> > jband.cpp:96:1: error: expected initializer before 'extern'
> >> > 96 | extern "C" {
> >> > | ^~~~~~
> >> > jband.cpp:104:1: error: expected unqualified-id before '{' token
> >> > 104 | {
> >> > | ^
> >> > jbandtest.f90:28:14:
> >> >
> >> > 28 | FORTJBAND = JBAND (tbar)
> >> > | 1
> >> > Error: Type mismatch in argument 'tbar' at (1); passed REAL(16) to REAL(10)
> >> >
> >> > The last error will go away together with the others.
> >> Michael S has given you one way to go for that. My comments were going
> >> to be more general. You have several ways of specifying the types, and
> >> I could not tell if you really wanted all of them. In general, I would
> >> define the Fortran types from the C ones (since those are the types you
> >> need for the call and return) and leave it at that.
> >>
> >> But the big one is that the Fortran code seems to think the function is
> >> called jband, when in fact it's called iband.
> >>
> >> I went for this:
> >>
> >> module STUFF
> >> use iso_c_binding
> >> integer, parameter :: qud=real(c_long_double)
> >> end module STUFF
> >> program JBAND_TEST
> >> use STUFF; implicit none
> >> real(qud) :: tbar, curr, FORTJBAND
> >> 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 IBAND (tbar) bind(c)
> >> use STUFF
> >> real(qud), VALUE :: tbar
> >> real(qud) :: IBAND
> >> end function iband
> >> end interface
> >
> > I think, you are taking chances here. Even if it happens to work
> > on x86-64 Linux, it can fail on some other target where calling
> > conventions for long double return value in 'C' differ from those
> > of Fortran. The method shown in one of my previous posts
> > appear more robust:
> >
> > interface
> > real(c_long_double) function IBAND (tbar) bind(c)
> > import c_long_double
> > real(c_long_double), value :: tbar
> > end function IBAND
> I think I see -- keep the C-related types in the smallest interface? I
> didn't study your example, but the OP renamed the C function and gave
> the resulting Fortran function a native Fortran type. Is that what you
> advise?
>
> The OP should have taken your Fortran and applied my C++ edits. Maybe
> they took my Fortran and it did not work on Windows. That would be a
> shame.
> >> BTW, you said the C++ was written by a C++ expert. I don't think it
> >> was!
> >
> > With no relationships to C, C++ or Fortran, I don't like the way they
> > evaluate their polynomials from numerical perspective . But I assume
> > that algorithm was copied 'as is' from 40 y.o. book so it's not a
> > fault of C++ programmer.
> I was just remarking on the C++ itself which has some 'non-expert'
> attributes.
> > Despite my disliking, their method is good enough to achieve their modest
> > specified goal for relative accuracy: of 14-15 significant digits.
> > With smarter method the same or better accuracy can be achieved using
> > standard 'double" arithmetic (IEEE-754 binary64) thus making the routine
> > portable to platforms that have no support for extended precision
> > math.
> Interesting. I don't know the function being evaluated at all.
>


Click here to read the complete article
Re: Problem with calling C++ from Fortran

<udq41s$1kto4$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1471&group=comp.lang.c%2B%2B#1471

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dieterhansbritz@gmail.com (db)
Newsgroups: comp.lang.c++
Subject: Re: Problem with calling C++ from Fortran
Date: Tue, 12 Sep 2023 18:36:11 +0200
Organization: A noiseless patient Spider
Lines: 254
Message-ID: <udq41s$1kto4$1@dont-email.me>
References: <udhbs9$e5d$1@dont-email.me> <udhcaj$j8l$1@dont-email.me>
<udkdu9$itm4$1@dont-email.me> <87o7iajh8b.fsf@bsb.me.uk>
<udmvg7$11r4u$1@dont-email.me> <87cyyoqoka.fsf@bsb.me.uk>
<2ccb965a-8d31-48af-97ad-2d96daa4862dn@googlegroups.com>
<87a5trplua.fsf@bsb.me.uk>
<f1b6f8e1-fa16-4fb7-b4b2-883788d098fdn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 12 Sep 2023 16:36:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a95eb3e5ffa1773c6f3000948b8740fd";
logging-data="1734404"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NwJPhg194e+DPoi+tYve2Xu096vc8xlo="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:gLpuqbw3xWn4e1VDkmtGMnPlBb4=
In-Reply-To: <f1b6f8e1-fa16-4fb7-b4b2-883788d098fdn@googlegroups.com>
Content-Language: en-US
 by: db - Tue, 12 Sep 2023 16:36 UTC

On 12.09.2023 16.32, Michael S wrote:
> On Tuesday, September 12, 2023 at 4:23:09 PM UTC+3, Ben Bacarisse wrote:
>> Michael S <already...@yahoo.com> writes:
>>
>>> On Tuesday, September 12, 2023 at 2:26:49 AM UTC+3, Ben Bacarisse wrote:
>>>> db <dieterh...@gmail.com> writes:
>>>>
>>>>> On 10.09.2023 15.22, Ben Bacarisse wrote:
>>>>>> db <dieterh...@gmail.com> writes:
>>>>>>
>>>>>>> On 09.09.2023 11.02, Mut...@dastardlyhq.com wrote:
>>>>>>>> On Sat, 9 Sep 2023 10:54:33 +0200
>>>>>>>> db <dieterh...@gmail.com> wrote:
>>>>>>>>> but now it says that C-double doesn't exist.
>>>>>>>>>
>>>>>>>>> Any ideas?
>>>>>>>> C++ mangles function names so that the name you give the function isn't
>>>>>>>> the
>>>>>>>> name the dynamic loader sees in the binary. You need to put this around whatever
>>>>>>>> function you want to call from Fortran so it has C style linkage which means
>>>>>>>> the name remains the same:
>>>>>>>> extern "C" {
>>>>>>>> void calledFromFortran()
>>>>>>>> {
>>>>>>>> :
>>>>>>>> }
>>>>>>>> }
>>>>>>>>
>>>>>>> Where should this go in the code? I added it at the end as is,
>>>>>>> and now I get this:
>>>>>>>
>>>>>>> ~/ownlib90/tests> ./jbandtest
>>>>>>> jband.cpp:88:1: error: expected initializer before 'extern'
>>>>>>> 88 | extern "C" {
>>>>>>> | ^~~~~~
>>>>>>> jband.cpp:96:1: error: expected unqualified-id before '{' token
>>>>>>> 96 | {
>>>>>>> | ^
>>>>>>> jband.cpp: In function 'void calledFromFortran()':
>>>>>>> jband.cpp:350:9: error: expected primary-expression before ':' token
>>>>>>> 350 | :
>>>>>>> ...
>>>>>> You are giving people very little to go on. There's an unknown command
>>>>>> (./jbandtest) the gives some errors where only one line of code can be
>>>>>> seen.
>>>>>> Can you produce a cut-down version to the program that shows the same
>>>>>> error -- mimimal example of the C++ and of the Fortran that should
>>>>>> calls it but which fails? That would mean you could post the code.
>>>>>> If that is not possible, can you link to the source so those who know
>>>>>> both C++ and Fortran can take a proper look?
>>>>>
>>>>> I put the C++ function here: www.dieterbritz.dk/jband.cpp
>>>>> After getting errors about the extern lines, whch I had
>>>>> added at the end, I shifted it to near the start:
>>>>>
>>>>> #include <iostream>
>>>>> #include <fstream>
>>>>> #include <iomanip>
>>>>> #include "math.h"
>>>>>
>>>>> using namespace std;
>>>>>
>>>>> extern "C" {
>>>>>
>>>>> void calledFromFortran()
>>>>> {
>>>>> :
>>>>> }
>>>>>
>>>>> }
>>>> Remove them all. You have two options. One: correct the C++. To do
>>>> this put this, and only this,
>>>>
>>>> extern "C" {
>>>> const long double iband(const long double tbar);
>>>> }
>>>>
>>>> just after the #include lines. This is perfect (the first const is a
>>>> meaningless word here and the compiler may warn you about that) but it
>>>> will do.
>>>>
>>>> Next replace
>>>>
>>>> #include "math.h"
>>>>
>>>> with
>>>>
>>>> #include <math.h>
>>>>
>>>> I'd also remove the two unnecessary #include lines (for iomanip and
>>>> fstream). All of these are "tidying up". Only the extern "C" part I
>>>> gave corrects an actual mistake.
>>>>
>>>> The second option is the turn the code in C which links directly with
>>>> Fortran. The only C++ used by the code is one "cout << " line (not a
>>>> good way to report an error, but that's not important right now) and a
>>>> setting for pi that is not a compile-time constant (4*atanl(1)) written
>>>> in a more fussy way.
>>>>
>>>> To convert to C, copy the file to jband.c (rather than .cpp) and replce
>>>> the declaration for pi. Given the code is littered with explicit 21
>>>> digits decimals (a little more than the precision of C's usualy long
>>>> double type) I would just write
>>>>
>>>> static const long double pi = 3.14159265358979323846L;
>>>>
>>>> Now replace the cout line with
>>>>
>>>> fputs("\nError in stredges::iband(): tbar must be > 0", stderr);
>>>>
>>>> This writes the message to the error stream and put only these two
>>>> include lines at the top:
>>>>
>>>> #include <math.h>
>>>> #include <stdio.h>
>>>>
>>>> Now the file is C code.
>>>>
>>>> Frankly, this is the way I'd go, since linking C is just that bit
>>>> simpler than linking C++. But if the code is going to become fully
>>>> fledged (and complex) C++, obviously use my first option.
>>>>> I still get errors pertaining to these lines. But after adding
>>>>> the -c to the two compilation lines in the script, these are
>>>>> now the only errors. I think I am getting closer. The errors
>>>>> are now
>>>> Yes, you must compile it like this:
>>>>
>>>> g++ -c jband.cpp (for C++ code)
>>>> gcc -c jband.c (for C code)
>>>>
>>>> You don't need any -o but you could add in lots of warnings like
>>>>
>>>> gcc -Wall -Wextra -c jband.c
>>>>> jband.cpp: In function 'void calledFromFortran()':
>>>>> jband.cpp:12:9: error: expected primary-expression before ':' token
>>>>> 12 | :
>>>>> | ^
>>>>> jband.cpp: At global scope:
>>>>> jband.cpp:96:1: error: expected initializer before 'extern'
>>>>> 96 | extern "C" {
>>>>> | ^~~~~~
>>>>> jband.cpp:104:1: error: expected unqualified-id before '{' token
>>>>> 104 | {
>>>>> | ^
>>>>> jbandtest.f90:28:14:
>>>>>
>>>>> 28 | FORTJBAND = JBAND (tbar)
>>>>> | 1
>>>>> Error: Type mismatch in argument 'tbar' at (1); passed REAL(16) to REAL(10)
>>>>>
>>>>> The last error will go away together with the others.
>>>> Michael S has given you one way to go for that. My comments were going
>>>> to be more general. You have several ways of specifying the types, and
>>>> I could not tell if you really wanted all of them. In general, I would
>>>> define the Fortran types from the C ones (since those are the types you
>>>> need for the call and return) and leave it at that.
>>>>
>>>> But the big one is that the Fortran code seems to think the function is
>>>> called jband, when in fact it's called iband.
>>>>
>>>> I went for this:
>>>>
>>>> module STUFF
>>>> use iso_c_binding
>>>> integer, parameter :: qud=real(c_long_double)
>>>> end module STUFF
>>>> program JBAND_TEST
>>>> use STUFF; implicit none
>>>> real(qud) :: tbar, curr, FORTJBAND
>>>> 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 IBAND (tbar) bind(c)
>>>> use STUFF
>>>> real(qud), VALUE :: tbar
>>>> real(qud) :: IBAND
>>>> end function iband
>>>> end interface
>>>
>>> I think, you are taking chances here. Even if it happens to work
>>> on x86-64 Linux, it can fail on some other target where calling
>>> conventions for long double return value in 'C' differ from those
>>> of Fortran. The method shown in one of my previous posts
>>> appear more robust:
>>>
>>> interface
>>> real(c_long_double) function IBAND (tbar) bind(c)
>>> import c_long_double
>>> real(c_long_double), value :: tbar
>>> end function IBAND
>> I think I see -- keep the C-related types in the smallest interface? I
>> didn't study your example, but the OP renamed the C function and gave
>> the resulting Fortran function a native Fortran type. Is that what you
>> advise?
>>
>> The OP should have taken your Fortran and applied my C++ edits. Maybe
>> they took my Fortran and it did not work on Windows. That would be a
>> shame.
>>>> BTW, you said the C++ was written by a C++ expert. I don't think it
>>>> was!
>>>
>>> With no relationships to C, C++ or Fortran, I don't like the way they
>>> evaluate their polynomials from numerical perspective . But I assume
>>> that algorithm was copied 'as is' from 40 y.o. book so it's not a
>>> fault of C++ programmer.
>> I was just remarking on the C++ itself which has some 'non-expert'
>> attributes.
>>> Despite my disliking, their method is good enough to achieve their modest
>>> specified goal for relative accuracy: of 14-15 significant digits.
>>> With smarter method the same or better accuracy can be achieved using
>>> standard 'double" arithmetic (IEEE-754 binary64) thus making the routine
>>> portable to platforms that have no support for extended precision
>>> math.
>> Interesting. I don't know the function being evaluated at all.
>>
>
> I don't know it either. But I had drawn the graphs and looked at
> coefficients in two ranges where precision is worst: [0.0081: 0.0158] and
> [0.0158:0.0631].
> A simple affine transform of the polynomial could either improve precision
> of result by 4 orders of magnitude or reduce needs for precision of arithmetic
> by 3 orders of magnitude while still somewhat improving the precision of result.
> I'd guess, more advanced techniques can improve it further in both dimensions,
> but low hanging fruits first.
>
>> --
>> Ben.


Click here to read the complete article
Re: Problem with calling C++ from Fortran

<9f0bc74c-6243-4a8f-9539-cbecc304df15n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=1615&group=comp.lang.c%2B%2B#1615

  copy link   Newsgroups: comp.lang.c++
X-Received: by 2002:ac8:7ec7:0:b0:40a:9069:895b with SMTP id x7-20020ac87ec7000000b0040a9069895bmr32116qtj.2.1695116376296;
Tue, 19 Sep 2023 02:39:36 -0700 (PDT)
X-Received: by 2002:a05:6808:1886:b0:3a7:4878:233d with SMTP id
bi6-20020a056808188600b003a74878233dmr5304580oib.0.1695116376015; Tue, 19 Sep
2023 02:39:36 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!border-2.nntp.ord.giganews.com!border-1.nntp.ord.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c++
Date: Tue, 19 Sep 2023 02:39:35 -0700 (PDT)
In-Reply-To: <udq41s$1kto4$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=199.203.251.52; posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 199.203.251.52
References: <udhbs9$e5d$1@dont-email.me> <udhcaj$j8l$1@dont-email.me>
<udkdu9$itm4$1@dont-email.me> <87o7iajh8b.fsf@bsb.me.uk> <udmvg7$11r4u$1@dont-email.me>
<87cyyoqoka.fsf@bsb.me.uk> <2ccb965a-8d31-48af-97ad-2d96daa4862dn@googlegroups.com>
<87a5trplua.fsf@bsb.me.uk> <f1b6f8e1-fa16-4fb7-b4b2-883788d098fdn@googlegroups.com>
<udq41s$1kto4$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <9f0bc74c-6243-4a8f-9539-cbecc304df15n@googlegroups.com>
Subject: Re: Problem with calling C++ from Fortran
From: already5chosen@yahoo.com (Michael S)
Injection-Date: Tue, 19 Sep 2023 09:39:36 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 356
 by: Michael S - Tue, 19 Sep 2023 09:39 UTC

On Tuesday, September 12, 2023 at 7:36:32 PM UTC+3, db wrote:
> On 12.09.2023 16.32, Michael S wrote:
> > On Tuesday, September 12, 2023 at 4:23:09 PM UTC+3, Ben Bacarisse wrote:
> >> Michael S <already...@yahoo.com> writes:
> >>
> >>> On Tuesday, September 12, 2023 at 2:26:49 AM UTC+3, Ben Bacarisse wrote:
> >>>> db <dieterh...@gmail.com> writes:
> >>>>
> >>>>> On 10.09.2023 15.22, Ben Bacarisse wrote:
> >>>>>> db <dieterh...@gmail.com> writes:
> >>>>>>
> >>>>>>> On 09.09.2023 11.02, Mut...@dastardlyhq.com wrote:
> >>>>>>>> On Sat, 9 Sep 2023 10:54:33 +0200
> >>>>>>>> db <dieterh...@gmail.com> wrote:
> >>>>>>>>> but now it says that C-double doesn't exist.
> >>>>>>>>>
> >>>>>>>>> Any ideas?
> >>>>>>>> C++ mangles function names so that the name you give the function isn't
> >>>>>>>> the
> >>>>>>>> name the dynamic loader sees in the binary. You need to put this around whatever
> >>>>>>>> function you want to call from Fortran so it has C style linkage which means
> >>>>>>>> the name remains the same:
> >>>>>>>> extern "C" {
> >>>>>>>> void calledFromFortran()
> >>>>>>>> {
> >>>>>>>> :
> >>>>>>>> }
> >>>>>>>> }
> >>>>>>>>
> >>>>>>> Where should this go in the code? I added it at the end as is,
> >>>>>>> and now I get this:
> >>>>>>>
> >>>>>>> ~/ownlib90/tests> ./jbandtest
> >>>>>>> jband.cpp:88:1: error: expected initializer before 'extern'
> >>>>>>> 88 | extern "C" {
> >>>>>>> | ^~~~~~
> >>>>>>> jband.cpp:96:1: error: expected unqualified-id before '{' token
> >>>>>>> 96 | {
> >>>>>>> | ^
> >>>>>>> jband.cpp: In function 'void calledFromFortran()':
> >>>>>>> jband.cpp:350:9: error: expected primary-expression before ':' token
> >>>>>>> 350 | :
> >>>>>>> ...
> >>>>>> You are giving people very little to go on. There's an unknown command
> >>>>>> (./jbandtest) the gives some errors where only one line of code can be
> >>>>>> seen.
> >>>>>> Can you produce a cut-down version to the program that shows the same
> >>>>>> error -- mimimal example of the C++ and of the Fortran that should
> >>>>>> calls it but which fails? That would mean you could post the code.
> >>>>>> If that is not possible, can you link to the source so those who know
> >>>>>> both C++ and Fortran can take a proper look?
> >>>>>
> >>>>> I put the C++ function here: www.dieterbritz.dk/jband.cpp
> >>>>> After getting errors about the extern lines, whch I had
> >>>>> added at the end, I shifted it to near the start:
> >>>>>
> >>>>> #include <iostream>
> >>>>> #include <fstream>
> >>>>> #include <iomanip>
> >>>>> #include "math.h"
> >>>>>
> >>>>> using namespace std;
> >>>>>
> >>>>> extern "C" {
> >>>>>
> >>>>> void calledFromFortran()
> >>>>> {
> >>>>> :
> >>>>> }
> >>>>>
> >>>>> }
> >>>> Remove them all. You have two options. One: correct the C++. To do
> >>>> this put this, and only this,
> >>>>
> >>>> extern "C" {
> >>>> const long double iband(const long double tbar);
> >>>> }
> >>>>
> >>>> just after the #include lines. This is perfect (the first const is a
> >>>> meaningless word here and the compiler may warn you about that) but it
> >>>> will do.
> >>>>
> >>>> Next replace
> >>>>
> >>>> #include "math.h"
> >>>>
> >>>> with
> >>>>
> >>>> #include <math.h>
> >>>>
> >>>> I'd also remove the two unnecessary #include lines (for iomanip and
> >>>> fstream). All of these are "tidying up". Only the extern "C" part I
> >>>> gave corrects an actual mistake.
> >>>>
> >>>> The second option is the turn the code in C which links directly with
> >>>> Fortran. The only C++ used by the code is one "cout << " line (not a
> >>>> good way to report an error, but that's not important right now) and a
> >>>> setting for pi that is not a compile-time constant (4*atanl(1)) written
> >>>> in a more fussy way.
> >>>>
> >>>> To convert to C, copy the file to jband.c (rather than .cpp) and replce
> >>>> the declaration for pi. Given the code is littered with explicit 21
> >>>> digits decimals (a little more than the precision of C's usualy long
> >>>> double type) I would just write
> >>>>
> >>>> static const long double pi = 3.14159265358979323846L;
> >>>>
> >>>> Now replace the cout line with
> >>>>
> >>>> fputs("\nError in stredges::iband(): tbar must be > 0", stderr);
> >>>>
> >>>> This writes the message to the error stream and put only these two
> >>>> include lines at the top:
> >>>>
> >>>> #include <math.h>
> >>>> #include <stdio.h>
> >>>>
> >>>> Now the file is C code.
> >>>>
> >>>> Frankly, this is the way I'd go, since linking C is just that bit
> >>>> simpler than linking C++. But if the code is going to become fully
> >>>> fledged (and complex) C++, obviously use my first option.
> >>>>> I still get errors pertaining to these lines. But after adding
> >>>>> the -c to the two compilation lines in the script, these are
> >>>>> now the only errors. I think I am getting closer. The errors
> >>>>> are now
> >>>> Yes, you must compile it like this:
> >>>>
> >>>> g++ -c jband.cpp (for C++ code)
> >>>> gcc -c jband.c (for C code)
> >>>>
> >>>> You don't need any -o but you could add in lots of warnings like
> >>>>
> >>>> gcc -Wall -Wextra -c jband.c
> >>>>> jband.cpp: In function 'void calledFromFortran()':
> >>>>> jband.cpp:12:9: error: expected primary-expression before ':' token
> >>>>> 12 | :
> >>>>> | ^
> >>>>> jband.cpp: At global scope:
> >>>>> jband.cpp:96:1: error: expected initializer before 'extern'
> >>>>> 96 | extern "C" {
> >>>>> | ^~~~~~
> >>>>> jband.cpp:104:1: error: expected unqualified-id before '{' token
> >>>>> 104 | {
> >>>>> | ^
> >>>>> jbandtest.f90:28:14:
> >>>>>
> >>>>> 28 | FORTJBAND = JBAND (tbar)
> >>>>> | 1
> >>>>> Error: Type mismatch in argument 'tbar' at (1); passed REAL(16) to REAL(10)
> >>>>>
> >>>>> The last error will go away together with the others.
> >>>> Michael S has given you one way to go for that. My comments were going
> >>>> to be more general. You have several ways of specifying the types, and
> >>>> I could not tell if you really wanted all of them. In general, I would
> >>>> define the Fortran types from the C ones (since those are the types you
> >>>> need for the call and return) and leave it at that.
> >>>>
> >>>> But the big one is that the Fortran code seems to think the function is
> >>>> called jband, when in fact it's called iband.
> >>>>
> >>>> I went for this:
> >>>>
> >>>> module STUFF
> >>>> use iso_c_binding
> >>>> integer, parameter :: qud=real(c_long_double)
> >>>> end module STUFF
> >>>> program JBAND_TEST
> >>>> use STUFF; implicit none
> >>>> real(qud) :: tbar, curr, FORTJBAND
> >>>> 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 IBAND (tbar) bind(c)
> >>>> use STUFF
> >>>> real(qud), VALUE :: tbar
> >>>> real(qud) :: IBAND
> >>>> end function iband
> >>>> end interface
> >>>
> >>> I think, you are taking chances here. Even if it happens to work
> >>> on x86-64 Linux, it can fail on some other target where calling
> >>> conventions for long double return value in 'C' differ from those
> >>> of Fortran. The method shown in one of my previous posts
> >>> appear more robust:
> >>>
> >>> interface
> >>> real(c_long_double) function IBAND (tbar) bind(c)
> >>> import c_long_double
> >>> real(c_long_double), value :: tbar
> >>> end function IBAND
> >> I think I see -- keep the C-related types in the smallest interface? I
> >> didn't study your example, but the OP renamed the C function and gave
> >> the resulting Fortran function a native Fortran type. Is that what you
> >> advise?
> >>
> >> The OP should have taken your Fortran and applied my C++ edits. Maybe
> >> they took my Fortran and it did not work on Windows. That would be a
> >> shame.
> >>>> BTW, you said the C++ was written by a C++ expert. I don't think it
> >>>> was!
> >>>
> >>> With no relationships to C, C++ or Fortran, I don't like the way they
> >>> evaluate their polynomials from numerical perspective . But I assume
> >>> that algorithm was copied 'as is' from 40 y.o. book so it's not a
> >>> fault of C++ programmer.
> >> I was just remarking on the C++ itself which has some 'non-expert'
> >> attributes.
> >>> Despite my disliking, their method is good enough to achieve their modest
> >>> specified goal for relative accuracy: of 14-15 significant digits.
> >>> With smarter method the same or better accuracy can be achieved using
> >>> standard 'double" arithmetic (IEEE-754 binary64) thus making the routine
> >>> portable to platforms that have no support for extended precision
> >>> math.
> >> Interesting. I don't know the function being evaluated at all.
> >>
> >
> > I don't know it either. But I had drawn the graphs and looked at
> > coefficients in two ranges where precision is worst: [0.0081: 0.0158] and
> > [0.0158:0.0631].
> > A simple affine transform of the polynomial could either improve precision
> > of result by 4 orders of magnitude or reduce needs for precision of arithmetic
> > by 3 orders of magnitude while still somewhat improving the precision of result.
> > I'd guess, more advanced techniques can improve it further in both dimensions,
> > but low hanging fruits first.
> >
> >> --
> >> Ben.
> The person who gave me the C++ code is an older experienced C++
> programmer. Maybe he uses out of data C++ features but the
> function works as is (without the extra lines given to me here,
> which were meant to help with calling it from Fortran). He also
> gave me a little C++ program to call it, and it produced
> output that I was able to verify.
>
> The problem is electrochemical, the current vs time at a flat band
> electrode flush with an insulating plane, for which there is no
> analytical solution, and the function gives highly accurate values
> using piecewise polynomial fits. Actually, he is over the top with
> the accuracy because in practice, comparing with experiments, 3-4
> figures are all that we need but he likes to do the best that can
> be done.
>
> --
> Dieter Britz


Click here to read the complete article
Pages:12
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor