Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Fortran calling C++

Tangy62
Beginner
499 Views

I'm porting an old Compaq Fortran application to Intel Fortran. The Fortran code calls also C++ code. Now I have the following problem:

I have a C++ method (Visual Studio 2019)

void myCpp(char *string1, int len1, char *string2, int len2)

In Fortran I call the C++ method

character*80 string1, string2
call myCpp(string1, string2)

For that I define an interface in a module

interface
   subroutine myCpp(string1, string2)
      !DEC$ ATTRIBUTES C, REFERENCE, ALIAS : '_myCpp' :: myCpp
      !DEC$ ATTRIBUTES REFERENCE :: string1, string2

      character string1, string2
   end subroutine
end interface

When I debug the code at myCpp the string1 contains the correct value. But the content of string2  is undefinded and the application crashed (any other method with only one char* parameter works fine)

I also try many different interface definitions. For example

subroutine myCpp(a, b) bind(C, name='myCpp')
     character(kind=c_char), dimension(*), intent(in) :: string1, string2
end subroutine

 

Where is my mistake?

 

0 Kudos
2 Replies
Arjen_Markus
Honored Contributor I
491 Views

The problem is the interfacing method for strings. There used to be at least two methods to pass on the length of string arguments: the length may appear directly after the string argument (hidden from the Fortran side) or be gathered as extra arguments at the end:

void myCpp(char *string1, char *string2, int len1, int len2)

Compaq Fortran used the former method, but the default method used by Intel oneAPI Fortran is the latter. Via th appropriate compile option you can switch between these methods.

You might consider using the C binding mechanism that was introduced with Fortran 2003, as that avoids this type of oddities as well as many others. That depends on the number of such routines and the amount of work a conversion would require.

0 Kudos
Tangy62
Beginner
382 Views

Thanks for answer.

First of all, I should mention that a syntax change would entail a lot of work.

I assume that you meant the compiler option /iface:mixed_str_len_arg. I use this option. But in this case the char* string2 is undefined. Only string1 is correct. 

Any idea?

 

0 Kudos
Reply