<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Using Interface Statements to call C from Fortran in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Using-Interface-Statements-to-call-C-from-Fortran/m-p/910276#M83247</link>
    <description>&lt;P&gt;Here is a simple test program of 3 files (below) when I try to compile and link in Visual Fortran (VS) I get:&lt;/P&gt;
&lt;P&gt;1&amp;gt;------ Build started: Project: test, Configuration: Debug Win32 ------&lt;/P&gt;
&lt;P&gt;1&amp;gt;Compiling with Intel Fortran 11.0.061 [IA-32]...&lt;/P&gt;
&lt;P&gt;1&amp;gt;test3.f90&lt;/P&gt;
&lt;P&gt;1&amp;gt;Linking...&lt;/P&gt;
&lt;P&gt;1&amp;gt;test3.obj : error LNK2019: unresolved external symbol _c_code referenced in function _MAIN__&lt;/P&gt;
&lt;P&gt;1&amp;gt;Debug\\test.exe : fatal error LNK1120: 1 unresolved externals&lt;/P&gt;
&lt;P&gt;1&amp;gt;&lt;/P&gt;
&lt;P&gt;1&amp;gt;Build log written to "file://C:\\Users\\hovg\\test\\Debug\\BuildLog.htm"&lt;/P&gt;
&lt;P&gt;1&amp;gt;test - 2 error(s), 0 warning(s)&lt;/P&gt;
&lt;P&gt;========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;What am I missing??&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;FILE 1:&lt;/P&gt;
&lt;P&gt;program test&lt;/P&gt;
&lt;P&gt;use int_mod&lt;/P&gt;
&lt;P&gt;real*4 a(50)&lt;/P&gt;
&lt;P&gt;integer*4 n&lt;/P&gt;
&lt;P&gt;call c_code(n,a)&lt;/P&gt;
&lt;P&gt;write(*,*) (a(i),i=1,n)&lt;/P&gt;
&lt;P&gt;stop&lt;/P&gt;
&lt;P&gt;end&lt;/P&gt;
&lt;P&gt;FILE 2:&lt;/P&gt;
&lt;B&gt;
&lt;P&gt;module int_mod&lt;/P&gt;
&lt;P&gt;&lt;B&gt;interface&lt;/B&gt;
&lt;/P&gt;&lt;P&gt;&lt;B&gt;subroutine&lt;/B&gt; c_code(nn,a)&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;B&gt;!DEC$ ATTRIBUTES C, ALIAS:'_c_code' :: c_code&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;B&gt;!DEC$ ATTRIBUTES REFERENCE :: nn&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;B&gt;!DEC$ ATTRIBUTES REFERENCE :: a&lt;/B&gt;
&lt;/P&gt;&lt;P&gt;&lt;B&gt;integer*4&lt;/B&gt; nn&lt;/P&gt;
&lt;P&gt;&lt;B&gt;real*4&lt;/B&gt;, &lt;B&gt;dimension&lt;/B&gt; (*) :: a&lt;/P&gt;
&lt;P&gt;&lt;B&gt;end subroutine&lt;/B&gt; c_code&lt;/P&gt;
&lt;P&gt;&lt;B&gt;end interface&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;end module&lt;/P&gt;
&lt;P&gt;FILE 3:&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;void c_code(n,a)&lt;/P&gt;
&lt;P&gt;int n;&lt;/P&gt;
&lt;P&gt;float *a;&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;int i;&lt;/P&gt;
&lt;P&gt;n =10;&lt;/P&gt;
&lt;P&gt;for (i=0;i&lt;N&gt;
&lt;/N&gt;&lt;/P&gt;&lt;P&gt;a&lt;I&gt; = (float) i;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;return;&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;/B&gt;
&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 20 Jan 2010 22:05:42 GMT</pubDate>
    <dc:creator>mhovers</dc:creator>
    <dc:date>2010-01-20T22:05:42Z</dc:date>
    <item>
      <title>Using Interface Statements to call C from Fortran</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Using-Interface-Statements-to-call-C-from-Fortran/m-p/910276#M83247</link>
      <description>&lt;P&gt;Here is a simple test program of 3 files (below) when I try to compile and link in Visual Fortran (VS) I get:&lt;/P&gt;
&lt;P&gt;1&amp;gt;------ Build started: Project: test, Configuration: Debug Win32 ------&lt;/P&gt;
&lt;P&gt;1&amp;gt;Compiling with Intel Fortran 11.0.061 [IA-32]...&lt;/P&gt;
&lt;P&gt;1&amp;gt;test3.f90&lt;/P&gt;
&lt;P&gt;1&amp;gt;Linking...&lt;/P&gt;
&lt;P&gt;1&amp;gt;test3.obj : error LNK2019: unresolved external symbol _c_code referenced in function _MAIN__&lt;/P&gt;
&lt;P&gt;1&amp;gt;Debug\\test.exe : fatal error LNK1120: 1 unresolved externals&lt;/P&gt;
&lt;P&gt;1&amp;gt;&lt;/P&gt;
&lt;P&gt;1&amp;gt;Build log written to "file://C:\\Users\\hovg\\test\\Debug\\BuildLog.htm"&lt;/P&gt;
&lt;P&gt;1&amp;gt;test - 2 error(s), 0 warning(s)&lt;/P&gt;
&lt;P&gt;========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;What am I missing??&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;FILE 1:&lt;/P&gt;
&lt;P&gt;program test&lt;/P&gt;
&lt;P&gt;use int_mod&lt;/P&gt;
&lt;P&gt;real*4 a(50)&lt;/P&gt;
&lt;P&gt;integer*4 n&lt;/P&gt;
&lt;P&gt;call c_code(n,a)&lt;/P&gt;
&lt;P&gt;write(*,*) (a(i),i=1,n)&lt;/P&gt;
&lt;P&gt;stop&lt;/P&gt;
&lt;P&gt;end&lt;/P&gt;
&lt;P&gt;FILE 2:&lt;/P&gt;
&lt;B&gt;
&lt;P&gt;module int_mod&lt;/P&gt;
&lt;P&gt;&lt;B&gt;interface&lt;/B&gt;
&lt;/P&gt;&lt;P&gt;&lt;B&gt;subroutine&lt;/B&gt; c_code(nn,a)&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;B&gt;!DEC$ ATTRIBUTES C, ALIAS:'_c_code' :: c_code&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;B&gt;!DEC$ ATTRIBUTES REFERENCE :: nn&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;&lt;B&gt;!DEC$ ATTRIBUTES REFERENCE :: a&lt;/B&gt;
&lt;/P&gt;&lt;P&gt;&lt;B&gt;integer*4&lt;/B&gt; nn&lt;/P&gt;
&lt;P&gt;&lt;B&gt;real*4&lt;/B&gt;, &lt;B&gt;dimension&lt;/B&gt; (*) :: a&lt;/P&gt;
&lt;P&gt;&lt;B&gt;end subroutine&lt;/B&gt; c_code&lt;/P&gt;
&lt;P&gt;&lt;B&gt;end interface&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;end module&lt;/P&gt;
&lt;P&gt;FILE 3:&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;void c_code(n,a)&lt;/P&gt;
&lt;P&gt;int n;&lt;/P&gt;
&lt;P&gt;float *a;&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;int i;&lt;/P&gt;
&lt;P&gt;n =10;&lt;/P&gt;
&lt;P&gt;for (i=0;i&lt;N&gt;
&lt;/N&gt;&lt;/P&gt;&lt;P&gt;a&lt;I&gt; = (float) i;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;return;&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;/B&gt;
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jan 2010 22:05:42 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Using-Interface-Statements-to-call-C-from-Fortran/m-p/910276#M83247</guid>
      <dc:creator>mhovers</dc:creator>
      <dc:date>2010-01-20T22:05:42Z</dc:date>
    </item>
    <item>
      <title>Using Interface Statements to call C from Fortran</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Using-Interface-Statements-to-call-C-from-Fortran/m-p/910277#M83248</link>
      <description>&lt;P&gt;Are you linking in the object file produced from compilation of the c file? The build log would help us diagnose. Within Visual Studio you probably need to make the Fortran project depend on the C project. Details depend on the version of Visual Studio, but right click on the fortran project and look for a Project dependencies item or similar in the resulting menu.&lt;/P&gt;
&lt;P&gt;A better (compliant with Fortran standards and therefore more portable and robust) way of writing the interface is:&lt;/P&gt;
&lt;PRE&gt;[fortran]module int_mod&lt;BR /&gt;  IMPLICIT NONE&lt;BR /&gt;  interface
    subroutine c_code(nn,a) BIND(C, NAME='c_code')
      USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INT, C_FLOAT
      IMPLICIT NONE
      INTEGER(C_INT), INTENT(OUT) :: nn
      REAL(C_FLOAT), INTENT(OUT)  :: a(*)
    end subroutine c_code
  end interface
end module int_mod[/fortran]&lt;/PRE&gt;
&lt;P&gt;Note the use of the standard INTEGER(xx) style declarations, rather than the INTEGER*xx form, the latter is a language extension. You could apply similar changes to your main program too. (If you see a red coloured REAL in the code above then that is just due to a problem with the forum software - please ignore it)&lt;/P&gt;
&lt;P&gt;&lt;B&gt;More importantly&lt;/B&gt;, the first argument in your c function as presented above is being taken by value, not by reference. The form of the declarations for the function arguments are also a relic of a bygone era. Consider:&lt;/P&gt;
&lt;P&gt;
&lt;PRE&gt;[cpp]void c_code(int *n, float *a)
{
   int i;
   *n = 10;
   for (i = 0; i &amp;lt; *n; i ++) {
      a&lt;I&gt; = (float) i;
   }
   return;
}
[/cpp]&lt;/I&gt;&lt;/PRE&gt;
&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jan 2010 01:46:06 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Using-Interface-Statements-to-call-C-from-Fortran/m-p/910277#M83248</guid>
      <dc:creator>IanH</dc:creator>
      <dc:date>2010-01-21T01:46:06Z</dc:date>
    </item>
  </channel>
</rss>

