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

C interoperability, passing constants

faustino_de_sousa__j
324 Views

Hi all!

I am having trouble compiling the following code:

C code:

#include <stdlib.h>

extern const int foo = -1;


Fortran code:

module cnst_m

  use, intrinsic :: iso_c_binding, only: c_int

  implicit none

  private

  public :: &
    FOO

  integer(kind=c_int), bind(c), protected :: FOO

end module cnst_m

program cnst_p

  use cnst_m, only: FOO

  implicit none

  print *, FOO
  stop
end program cnst_p

When using the "-init=(minus_)huge" flag, the compiler returns:

/tmp/ifortSUJ720.o:(.data+0x0): multiple definition of `foo'
./cnst.o:(.rodata+0x0): first defined here

The problem is also present with real initialization.

I would wager this has something to do with the way default initialization is defined.

Thank you very much.

Best regards,

José Rui

0 Kudos
3 Replies
FortranFan
Honored Contributor II
324 Views

Try with 'name=' option with bind(C,):

integer(kind=c_int), bind(C, name="foo"), protected :: FOO !<-- Note name= option

 

0 Kudos
faustino_de_sousa__j
324 Views

Makes no difference here.

Thank you very much.

Best regards,

José Rui

 

0 Kudos
FortranFan
Honored Contributor II
324 Views

What happens if you do not define it in your C code:

extern const int foo;

but do so in Fortran:

integer(c_int), bind(C, name="foo"), protected :: FOO = 1

If this doesn't work, see if you can get help from Intel support.

0 Kudos
Reply