Interoperable-Subroutines-and-Functions.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1999-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>The GNU Fortran Compiler: Interoperable Subroutines and Functions</title>
  20. <meta name="description" content="The GNU Fortran Compiler: Interoperable Subroutines and Functions">
  21. <meta name="keywords" content="The GNU Fortran Compiler: Interoperable Subroutines and Functions">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Interoperability-with-C.html#Interoperability-with-C" rel="up" title="Interoperability with C">
  30. <link href="Working-with-Pointers.html#Working-with-Pointers" rel="next" title="Working with Pointers">
  31. <link href="Interoperable-Global-Variables.html#Interoperable-Global-Variables" rel="prev" title="Interoperable Global Variables">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Interoperable-Subroutines-and-Functions"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Working-with-Pointers.html#Working-with-Pointers" accesskey="n" rel="next">Working with Pointers</a>, Previous: <a href="Interoperable-Global-Variables.html#Interoperable-Global-Variables" accesskey="p" rel="prev">Interoperable Global Variables</a>, Up: <a href="Interoperability-with-C.html#Interoperability-with-C" accesskey="u" rel="up">Interoperability with C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Interoperable-Subroutines-and-Functions-1"></a>
  69. <h4 class="subsection">7.1.4 Interoperable Subroutines and Functions</h4>
  70. <p>Subroutines and functions have to have the <code>BIND(C)</code> attribute to
  71. be compatible with C. The dummy argument declaration is relatively
  72. straightforward. However, one needs to be careful because C uses
  73. call-by-value by default while Fortran behaves usually similar to
  74. call-by-reference. Furthermore, strings and pointers are handled
  75. differently. Note that in Fortran 2003 and 2008 only explicit size
  76. and assumed-size arrays are supported but not assumed-shape or
  77. deferred-shape (i.e. allocatable or pointer) arrays. However, those
  78. are allowed since the Technical Specification 29113, see
  79. <a href="Further-Interoperability-of-Fortran-with-C.html#Further-Interoperability-of-Fortran-with-C">Further Interoperability of Fortran with C</a>
  80. </p>
  81. <p>To pass a variable by value, use the <code>VALUE</code> attribute.
  82. Thus, the following C prototype
  83. </p>
  84. <div class="smallexample">
  85. <pre class="smallexample"><code>int func(int i, int *j)</code>
  86. </pre></div>
  87. <p>matches the Fortran declaration
  88. </p>
  89. <div class="smallexample">
  90. <pre class="smallexample"> integer(c_int) function func(i,j)
  91. use iso_c_binding, only: c_int
  92. integer(c_int), VALUE :: i
  93. integer(c_int) :: j
  94. </pre></div>
  95. <p>Note that pointer arguments also frequently need the <code>VALUE</code> attribute,
  96. see <a href="Working-with-Pointers.html#Working-with-Pointers">Working with Pointers</a>.
  97. </p>
  98. <p>Strings are handled quite differently in C and Fortran. In C a string
  99. is a <code>NUL</code>-terminated array of characters while in Fortran each string
  100. has a length associated with it and is thus not terminated (by e.g.
  101. <code>NUL</code>). For example, if one wants to use the following C function,
  102. </p>
  103. <div class="smallexample">
  104. <pre class="smallexample"> #include &lt;stdio.h&gt;
  105. void print_C(char *string) /* equivalent: char string[] */
  106. {
  107. printf(&quot;%s\n&quot;, string);
  108. }
  109. </pre></div>
  110. <p>to print &ldquo;Hello World&rdquo; from Fortran, one can call it using
  111. </p>
  112. <div class="smallexample">
  113. <pre class="smallexample"> use iso_c_binding, only: C_CHAR, C_NULL_CHAR
  114. interface
  115. subroutine print_c(string) bind(C, name=&quot;print_C&quot;)
  116. use iso_c_binding, only: c_char
  117. character(kind=c_char) :: string(*)
  118. end subroutine print_c
  119. end interface
  120. call print_c(C_CHAR_&quot;Hello World&quot;//C_NULL_CHAR)
  121. </pre></div>
  122. <p>As the example shows, one needs to ensure that the
  123. string is <code>NUL</code> terminated. Additionally, the dummy argument
  124. <var>string</var> of <code>print_C</code> is a length-one assumed-size
  125. array; using <code>character(len=*)</code> is not allowed. The example
  126. above uses <code>c_char_&quot;Hello World&quot;</code> to ensure the string
  127. literal has the right type; typically the default character
  128. kind and <code>c_char</code> are the same and thus <code>&quot;Hello World&quot;</code>
  129. is equivalent. However, the standard does not guarantee this.
  130. </p>
  131. <p>The use of strings is now further illustrated using the C library
  132. function <code>strncpy</code>, whose prototype is
  133. </p>
  134. <div class="smallexample">
  135. <pre class="smallexample"> char *strncpy(char *restrict s1, const char *restrict s2, size_t n);
  136. </pre></div>
  137. <p>The function <code>strncpy</code> copies at most <var>n</var> characters from
  138. string <var>s2</var> to <var>s1</var> and returns <var>s1</var>. In the following
  139. example, we ignore the return value:
  140. </p>
  141. <div class="smallexample">
  142. <pre class="smallexample"> use iso_c_binding
  143. implicit none
  144. character(len=30) :: str,str2
  145. interface
  146. ! Ignore the return value of strncpy -&gt; subroutine
  147. ! &quot;restrict&quot; is always assumed if we do not pass a pointer
  148. subroutine strncpy(dest, src, n) bind(C)
  149. import
  150. character(kind=c_char), intent(out) :: dest(*)
  151. character(kind=c_char), intent(in) :: src(*)
  152. integer(c_size_t), value, intent(in) :: n
  153. end subroutine strncpy
  154. end interface
  155. str = repeat('X',30) ! Initialize whole string with 'X'
  156. call strncpy(str, c_char_&quot;Hello World&quot;//C_NULL_CHAR, &amp;
  157. len(c_char_&quot;Hello World&quot;,kind=c_size_t))
  158. print '(a)', str ! prints: &quot;Hello WorldXXXXXXXXXXXXXXXXXXX&quot;
  159. end
  160. </pre></div>
  161. <p>The intrinsic procedures are described in <a href="Intrinsic-Procedures.html#Intrinsic-Procedures">Intrinsic Procedures</a>.
  162. </p>
  163. <hr>
  164. <div class="header">
  165. <p>
  166. Next: <a href="Working-with-Pointers.html#Working-with-Pointers" accesskey="n" rel="next">Working with Pointers</a>, Previous: <a href="Interoperable-Global-Variables.html#Interoperable-Global-Variables" accesskey="p" rel="prev">Interoperable Global Variables</a>, Up: <a href="Interoperability-with-C.html#Interoperability-with-C" accesskey="u" rel="up">Interoperability with C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  167. </div>
  168. </body>
  169. </html>