<?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 How to call Fortran routine in static library using C in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929487#M87451</link>
    <description>&lt;P&gt;What is the proper calling convention for a C routine to call a Fortran subroutine containing a character variable in the call list.&lt;/P&gt;

&lt;P&gt;Example C code:&lt;BR /&gt;
	...&lt;BR /&gt;
	char *strng[1];&lt;BR /&gt;
	fsubr(strng[0]);&lt;BR /&gt;
	...&lt;/P&gt;

&lt;P&gt;Example Fortran code:&lt;BR /&gt;
	SUBROUTINE FSUBR(strng)&lt;BR /&gt;
	!DEC$ ATTRIBUTES DECORATE, ALIAS:'fsubr' :: FSUBR&lt;BR /&gt;
	...&lt;BR /&gt;
	CHARACTER*(*) STRNG&lt;BR /&gt;
	...&lt;/P&gt;

&lt;P&gt;Using the default calling convention for the Fortran code and __cdecl for the C code I end up with STRNG having an undefined address when I step into the Fortran routine.&lt;/P&gt;

&lt;P&gt;Regards, Mike&lt;/P&gt;</description>
    <pubDate>Thu, 27 Feb 2014 13:56:12 GMT</pubDate>
    <dc:creator>Kipling__Michael</dc:creator>
    <dc:date>2014-02-27T13:56:12Z</dc:date>
    <item>
      <title>How to call Fortran routine in static library using C</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929487#M87451</link>
      <description>&lt;P&gt;What is the proper calling convention for a C routine to call a Fortran subroutine containing a character variable in the call list.&lt;/P&gt;

&lt;P&gt;Example C code:&lt;BR /&gt;
	...&lt;BR /&gt;
	char *strng[1];&lt;BR /&gt;
	fsubr(strng[0]);&lt;BR /&gt;
	...&lt;/P&gt;

&lt;P&gt;Example Fortran code:&lt;BR /&gt;
	SUBROUTINE FSUBR(strng)&lt;BR /&gt;
	!DEC$ ATTRIBUTES DECORATE, ALIAS:'fsubr' :: FSUBR&lt;BR /&gt;
	...&lt;BR /&gt;
	CHARACTER*(*) STRNG&lt;BR /&gt;
	...&lt;/P&gt;

&lt;P&gt;Using the default calling convention for the Fortran code and __cdecl for the C code I end up with STRNG having an undefined address when I step into the Fortran routine.&lt;/P&gt;

&lt;P&gt;Regards, Mike&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2014 13:56:12 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929487#M87451</guid>
      <dc:creator>Kipling__Michael</dc:creator>
      <dc:date>2014-02-27T13:56:12Z</dc:date>
    </item>
    <item>
      <title>Review this recent forum</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929488#M87452</link>
      <description>&lt;P&gt;Review this recent forum topic &lt;STRONG&gt;&lt;A href="http://software.intel.com/en-us/forums/topic/505505"&gt;discussion&lt;/A&gt;&lt;/STRONG&gt;: but also this question has been addressed often at this forum and if you attempt a search you'll find lots of information.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2014 14:47:28 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929488#M87452</guid>
      <dc:creator>FortranFan</dc:creator>
      <dc:date>2014-02-27T14:47:28Z</dc:date>
    </item>
    <item>
      <title>I had searched and did not</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929489#M87453</link>
      <description>&lt;P&gt;I had searched and did not find an answer to my question.&amp;nbsp; The discussion you referred to was for calling C routines from Fortran.&amp;nbsp; I do not have a problem doing this placing an interface block in the Fortran code.&lt;/P&gt;

&lt;P&gt;What I'm trying to do is to&amp;nbsp;call a Fortran routine from a static library using C to make the call.&amp;nbsp; I had thought that I could add the line&lt;BR /&gt;
	!DEC$ ATTRIBUTES REFERENCE :: STRNG&lt;/P&gt;

&lt;P&gt;to the Fortran code to handle the hidden string length, but this results in the compiler errors:&lt;BR /&gt;
	The REFERENCE attribute cannot be used with a passed length CHARACTER variable on this platform&lt;BR /&gt;
	CHARACTER variable 'FILSPC' has no length argument in routines with C or STDCALL attribute&lt;/P&gt;

&lt;P&gt;Mike&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2014 15:14:00 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929489#M87453</guid>
      <dc:creator>Kipling__Michael</dc:creator>
      <dc:date>2014-02-27T15:14:00Z</dc:date>
    </item>
    <item>
      <title>BTW I've also tried the</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929490#M87454</link>
      <description>&lt;P&gt;BTW I've also tried the following code, but end up with the strng variable being undefined when I step into the routine.&lt;/P&gt;

&lt;P&gt;Example Fortran code:&lt;BR /&gt;
	SUBROUTINE FSUBR(strng)&lt;BR /&gt;
	!DEC$ ATTRIBUTES DECORATE, ALIAS:'fsubr' :: FSUBR&lt;BR /&gt;
	!DEC$ ATTRIBUTES REFERENCE :: FSUBR&lt;BR /&gt;
	...&lt;BR /&gt;
	CHARACTER*(*) STRNG&lt;BR /&gt;
	...&lt;/P&gt;

&lt;P&gt;Mike&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2014 15:47:51 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929490#M87454</guid>
      <dc:creator>Kipling__Michael</dc:creator>
      <dc:date>2014-02-27T15:47:51Z</dc:date>
    </item>
    <item>
      <title>You're passing a single</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929491#M87455</link>
      <description>&lt;P&gt;You're passing a single character by value - that isn't what Fortran is expecting. Try passing &amp;amp;strng. But use of CHARACTER(*) is a problem because now you don't have the length passed.&lt;/P&gt;

&lt;P&gt;Here's the standard approach:&lt;/P&gt;

&lt;P&gt;SUBROUTINE FSUBR (STRNG) BIND(C)&lt;BR /&gt;
	CHARACTER, DIMENSION(*) :: STRNG&lt;/P&gt;

&lt;P&gt;Note that this gives you an array of characters and no length. It's a bit awkward to deal with this in Fortran but it can be done.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2014 17:43:56 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929491#M87455</guid>
      <dc:creator>Steven_L_Intel1</dc:creator>
      <dc:date>2014-02-27T17:43:56Z</dc:date>
    </item>
    <item>
      <title>I modified the C code to be :</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929492#M87456</link>
      <description>&lt;P&gt;I modified the C code to be :&lt;BR /&gt;
	...&lt;BR /&gt;
	char *strng[1];&lt;BR /&gt;
	fsubr(&amp;amp;strng);&lt;BR /&gt;
	...&lt;/P&gt;

&lt;P&gt;and the Fortran code to be:&lt;BR /&gt;
	SUBROUTINE FSUBR(strng) BIND(C)&lt;BR /&gt;
	...&lt;BR /&gt;
	CHARACTER, DIMENSION(1):: strng&lt;BR /&gt;
	...&lt;BR /&gt;
	&amp;nbsp;&lt;/P&gt;

&lt;P&gt;The debugger does not say undefined address for strng, but does&amp;nbsp;shows garbage characters.&lt;/P&gt;

&lt;P&gt;Would it be better to pass an integer variable in the C code call list for the string length?&amp;nbsp; I could even dimension the character length of the strng variable.&amp;nbsp;(In the actual code,the variable strng is actually a path/filename so Character*(255) would be fine.)&lt;/P&gt;

&lt;P&gt;I'm just looking for the best way to pass character variables from C to Fortran static library routines.&amp;nbsp; I can modify either or both the C and Fortran code.&lt;/P&gt;

&lt;P&gt;Mike&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2014 19:00:04 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929492#M87456</guid>
      <dc:creator>Kipling__Michael</dc:creator>
      <dc:date>2014-02-27T19:00:04Z</dc:date>
    </item>
    <item>
      <title>OK, I found a solution in the</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929493#M87457</link>
      <description>&lt;P&gt;OK, I found a solution in the Reference Guide, Mixed Language Programming Standard Tools for Interoperability, Data Types, Characters section.&lt;/P&gt;

&lt;P&gt;By modifying both the C code and the Fortran code to look like the example I got it to work.&lt;/P&gt;

&lt;P&gt;Thanks, Mike&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2014 20:23:54 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929493#M87457</guid>
      <dc:creator>Kipling__Michael</dc:creator>
      <dc:date>2014-02-27T20:23:54Z</dc:date>
    </item>
    <item>
      <title>Although the problem seems </title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929494#M87458</link>
      <description>&lt;P&gt;Although the problem seems&amp;nbsp; to have been solved, I would like to point out that&lt;/P&gt;

&lt;P&gt;char *strng[1];&lt;/P&gt;

&lt;P&gt;declares an array of pointers to char with one element, and&lt;/P&gt;

&lt;P&gt;fsubr(strng[0]);&lt;/P&gt;

&lt;P&gt;passes the first element, a pointer to char, by value and so is equivalent to passing an array of char by reference.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Feb 2014 03:01:51 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929494#M87458</guid>
      <dc:creator>JVanB</dc:creator>
      <dc:date>2014-02-28T03:01:51Z</dc:date>
    </item>
    <item>
      <title>The actual code I'm working</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929495#M87459</link>
      <description>&lt;P&gt;The actual code I'm working with was written 25 years ago.&amp;nbsp; The main C routines called C wrapper routines used to call Fortran routines.&lt;/P&gt;

&lt;P&gt;The actual routine passed&amp;nbsp;char *strng to the wrapper which called another routine to copy it to the char * strng[1] variable.&amp;nbsp; This is what was being passed to the Fortran routine.&lt;/P&gt;

&lt;P&gt;The solution I found lets me bypass the wrapper routine altogether, and call the Fortran directly from the main C routine.&lt;/P&gt;

&lt;P&gt;Solution:&lt;BR /&gt;
	Main C routine:&lt;BR /&gt;
	...&lt;BR /&gt;
	char *strng;&lt;BR /&gt;
	char strng1[1024];&lt;BR /&gt;
	strcpy(strng1,strng)&lt;BR /&gt;
	fsubr(strng1);&lt;BR /&gt;
	...&lt;/P&gt;

&lt;P&gt;Fortran code:&lt;BR /&gt;
	SUBROUTINE FSUBR(strng1) BIND(C)&lt;BR /&gt;
	USE,INTRINSIC :: ISO_C_BINDING&lt;BR /&gt;
	...&lt;BR /&gt;
	CHARACTER, DIMENSION(1024) :: strng1&lt;BR /&gt;
	INTEGER s_len&lt;BR /&gt;
	CHARACTER(1024), POINTER :: strng2&lt;BR /&gt;
	call C_F_POINTER(C_LOC(strng1),strng2)&lt;BR /&gt;
	s_len = INDEX(strng1,C_NULL_CHAR) - 1&lt;BR /&gt;
	...&lt;BR /&gt;
	&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Mike&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Feb 2014 12:20:08 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929495#M87459</guid>
      <dc:creator>Kipling__Michael</dc:creator>
      <dc:date>2014-02-28T12:20:08Z</dc:date>
    </item>
    <item>
      <title>I now need help expanding the</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929496#M87460</link>
      <description>&lt;P&gt;I now need help expanding the solution I found for passing a string from C to Fortran.&lt;BR /&gt;
	I need to figure out how to pass string arrays to/from C and Fortran.&lt;/P&gt;

&lt;P&gt;In the example below, the C routine is passing an array of strings to Fortran and getting back a 2nd array of strings generated by Fortran.&lt;/P&gt;

&lt;P&gt;Can anyone tell me how to handle this?&lt;/P&gt;

&lt;P&gt;Example C code:&lt;BR /&gt;
	...&lt;BR /&gt;
	char **str_array1;&lt;BR /&gt;
	char **str_array2;&lt;BR /&gt;
	...&lt;BR /&gt;
	fsubr(str_array1, str_array2);&lt;BR /&gt;
	...&lt;/P&gt;

&lt;P&gt;&lt;BR /&gt;
	SUBROUTINE FSUBR(str_array1, str_array2) BIND(C)&lt;BR /&gt;
	USE,INTRINSIC :: ISO_C_BINDING&lt;BR /&gt;
	...&lt;/P&gt;

&lt;P&gt;Mike&lt;/P&gt;</description>
      <pubDate>Fri, 28 Feb 2014 16:40:02 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/How-to-call-Fortran-routine-in-static-library-using-C/m-p/929496#M87460</guid>
      <dc:creator>Kipling__Michael</dc:creator>
      <dc:date>2014-02-28T16:40:02Z</dc:date>
    </item>
  </channel>
</rss>

