title: Mixed programming - Including C++ header to Fortran
body: <p>I am trying to use a function from library written in C++ in my program written in Fortran. The C++ library is summarized in one header file so that if you want to use it in another C++ program you only do <code>#include functions.h</code> I would like to find out how to do something similar in Fortran.</p>
  
  <p>From my research I've created this minimal viable example:</p>
  
  <p>clib/functions.h:</p>
  
  <pre><code>#ifndef ADD_H
  #define ADD_H
  extern "C"
  {
  int __stdcall add(int x, int y);
  } 
  #endif
  </code></pre>
  
  <p>clib/functions.cpp:</p>
  
  <pre><code>extern "C"
  {
  int __stdcall add(int x, int y)
  {
      return x + y;
  }
  }
  </code></pre>
  
  <p>cinclude.c</p>
  
  <pre><code>#include "clib/functions.h"
  </code></pre>
  
  <p>cinterface.f95:</p>
  
  <pre><code>module cinterface
    use,intrinsic::ISO_C_BINDING
    integer(C_INT)::a,b
    interface
      integer(C_INT) function add(a,b) bind(C,name="add")
        use,intrinsic::ISO_C_BINDING
        implicit none
  !GCC$ ATTRIBUTES STDCALL :: add
  !DEC$ ATTRIBUTES STDCALL :: add
        integer(C_INT), value ::a,b
      end function add
    end interface
  end module cinterface
  </code></pre>
  
  <p>main.f90</p>
  
  <pre><code> program main
     use cinterface      
     implicit none
     integer :: c
     c = add(1,2)
     write(*,*) c
   end program
  </code></pre>
  
  <p>makefile:</p>
  
  <pre><code>FC = gfortran
  CC = g++
  LD = gfortran
  FFLAGS = -c -O2
  CFLAGS = -c -O2
  OBJ=main.o 
  DEP = \
      cinterface.o cinclude.o
  
  .SUFFIXES: .f90 .f95 .c .o
  # default rule to make .o files from .f files
  .f90.o  : ;       $(FC) $(FFLAGS) $*.f90 -o $*.o
  .f95.o  : ;       $(FC) $(FFLAGS) $*.f95 -o $*.o
  .c.o  : ;       $(CC) $(CFLAGS) $*.c -o $*.o
  %.o: %.mod
  #
  main.ex: ${DEP} ${OBJ}
      $(LD)  ${DEP}  ${OBJ} -o prog.exe
  #
  </code></pre>
  
  <p>When I try to make this project using Cygwin I get a following error:</p>
  
  <pre><code>main.o:main.f90:(.text+0x13): undefined reference to `add'
  main.o:main.f90:(.text+0x13): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `add'
  collect2: error: ld returned 1 exit status
  make: *** [makefile:19: main.ex] Error 1
  </code></pre>
  
  <p>How can I make <code>add</code> function in Fortran work?</p>