Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #91612

Problem with calling C++ from Fortran

From db <dieterhansbritz@gmail.com>
Newsgroups comp.lang.c++
Subject Problem with calling C++ from Fortran
Date 2023-09-09 10:54 +0200
Organization A noiseless patient Spider
Message-ID <udhbs9$e5d$1@dont-email.me> (permalink)

Show all headers | View raw


I want to call a C++ function from Fortran. The function is
called jband, in the file jband.cpp. The function is of type
long double, and so is its argument tbar.
I wrote this:

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
   FORTJBAND = JBAND (tbar)
end function FORTJBAND

I run this using this script

g++ -o jband.o jband.cpp
gfortran -o jbandtest.o jbandtest.f90
gfortran jband.o jbandtest.o
./a.out << eoi
1.0
-1
eoi
rm a.out jband.o jbandtest.o

and I get this output

/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
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)
[...]

So there is a communication problem with agreeing on the C++
type long double.
I have tried making everything in jband plain double, making
the two statements

     import JBAND, c_double
     import tbar, c_double

but now it says that C-double doesn't exist.

Any ideas?



-- 
Dieter Britz

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Problem with calling C++ from Fortran db <dieterhansbritz@gmail.com> - 2023-09-09 10:54 +0200
  Re: Problem with calling C++ from Fortran Muttley@dastardlyhq.com - 2023-09-09 09:02 +0000
    Re: Problem with calling C++ from Fortran db <dieterhansbritz@gmail.com> - 2023-09-09 12:47 +0200
      Re: Problem with calling C++ from Fortran Muttley@dastardlyhq.com - 2023-09-09 10:51 +0000
      Re: Problem with calling C++ from Fortran db <dieterhansbritz@gmail.com> - 2023-09-09 12:52 +0200
        Re: Problem with calling C++ from Fortran Muttley@dastardlyhq.com - 2023-09-09 11:00 +0000
          Re: Problem with calling C++ from Fortran db <dieterhansbritz@gmail.com> - 2023-09-10 14:33 +0200
            Re: Problem with calling C++ from Fortran Paavo Helde <eesnimi@osa.pri.ee> - 2023-09-10 18:49 +0300
            Re: Problem with calling C++ from Fortran Muttley@dastardlyhq.com - 2023-09-11 08:23 +0000
        Re: Problem with calling C++ from Fortran Paavo Helde <eesnimi@osa.pri.ee> - 2023-09-09 16:56 +0300
          Re: Problem with calling C++ from Fortran db <dieterhansbritz@gmail.com> - 2023-09-10 14:36 +0200
          Re: Problem with calling C++ from Fortran James Kuyper <jameskuyper@alumni.caltech.edu> - 2023-09-11 00:14 -0400
        Re: Problem with calling C++ from Fortran Bo Persson <bo@bo-persson.se> - 2023-09-10 16:08 +0200
      Re: Problem with calling C++ from Fortran "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-09-09 16:30 +0200
    Re: Problem with calling C++ from Fortran db <dieterhansbritz@gmail.com> - 2023-09-10 14:48 +0200
      Re: Problem with calling C++ from Fortran Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-09-10 14:22 +0100
        Re: Problem with calling C++ from Fortran Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-09-10 17:27 +0100
        Re: Problem with calling C++ from Fortran db <dieterhansbritz@gmail.com> - 2023-09-11 14:00 +0200
          Re: Problem with calling C++ from Fortran Michael S <already5chosen@yahoo.com> - 2023-09-11 05:21 -0700
          Re: Problem with calling C++ from Fortran Muttley@dastardlyhq.com - 2023-09-11 13:33 +0000
          Re: Problem with calling C++ from Fortran Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-09-12 00:26 +0100
            Re: Problem with calling C++ from Fortran db <dieterhansbritz@gmail.com> - 2023-09-12 10:48 +0200
              Re: Problem with calling C++ from Fortran Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-09-12 14:14 +0100
              Re: Problem with calling C++ from Fortran Michael S <already5chosen@yahoo.com> - 2023-09-12 07:15 -0700
            Re: Problem with calling C++ from Fortran Michael S <already5chosen@yahoo.com> - 2023-09-12 02:00 -0700
              Re: Problem with calling C++ from Fortran Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-09-12 14:22 +0100
                Re: Problem with calling C++ from Fortran Michael S <already5chosen@yahoo.com> - 2023-09-12 07:32 -0700
                Re: Problem with calling C++ from Fortran db <dieterhansbritz@gmail.com> - 2023-09-12 18:36 +0200
                Re: Problem with calling C++ from Fortran Michael S <already5chosen@yahoo.com> - 2023-09-19 02:39 -0700
  Re: Problem with calling C++ from Fortran Michael S <already5chosen@yahoo.com> - 2023-09-10 08:38 -0700
  Re: Problem with calling C++ from Fortran Michael S <already5chosen@yahoo.com> - 2023-09-10 09:51 -0700
    Re: Problem with calling C++ from Fortran Michael S <already5chosen@yahoo.com> - 2023-09-11 02:49 -0700
  Buy Zen Mushroom Chocolate Bar Microdose "https://psychedelicminds.shop/" <psychedelicsmind@gmail.com> - 2023-09-11 05:14 -0700

csiph-web