|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +permalink: /en/documents/4.0.0/Ch10.S1.html |
| 4 | +translated: true |
| 5 | +lang: en |
| 6 | +head: | |
| 7 | + <!--Generated on Tue Apr 6 18:19:31 2021 by LaTeXML (version 0.8.5) http://dlmf.nist.gov/LaTeXML/.--> |
| 8 | + <!--Document created on Apr, 2021.--> |
| 9 | + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 10 | + <link rel="stylesheet" href="LaTeXML.css" type="text/css"> |
| 11 | + <link rel="stylesheet" href="ltx-book.css" type="text/css"> |
| 12 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=MML_CHTML" type="text/javascript"></script> |
| 13 | + <link rel="up" href="Ch10.html" title="Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 14 | + <link rel="up up" href="Pt2.html" title="Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 15 | + <link rel="up up up" href="./" title="SML# Document Version 4.0.0"> |
| 16 | + <link rel="start" href="./" title="SML# Document Version 4.0.0"> |
| 17 | + <link rel="prev" href="Ch10.html" title="Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 18 | + <link rel="next" href="Ch10.S2.html" title="10.2 Declaring types of C functions ‣ Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 19 | + <link rel="section" href="Ch10.S2.html" title="10.2 Declaring types of C functions ‣ Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 20 | + <link rel="section" href="Ch10.S3.html" title="10.3 Basic examples of importing C functions ‣ Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 21 | + <link rel="section" href="Ch10.S4.html" title="10.4 Using dynamically linked libraries ‣ Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 22 | + <link rel="chapter" href="Ch5.html" title="Chapter 5 Installing SML# ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 23 | + <link rel="chapter" href="Ch6.html" title="Chapter 6 Setting up SML# programming environment ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 24 | + <link rel="chapter" href="Ch7.html" title="Chapter 7 Introduction to ML programming ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 25 | + <link rel="chapter" href="Ch8.html" title="Chapter 8 SML# feature: record polymorphism ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 26 | + <link rel="chapter" href="Ch9.html" title="Chapter 9 SML# feature: other type system extensions ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 27 | + <link rel="chapter" href="Ch10.html" title="Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 28 | + <link rel="chapter" href="Ch11.html" title="Chapter 11 SML# feature: Multithread programming ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 29 | + <link rel="chapter" href="Ch12.html" title="Chapter 12 SML# feature: seamless SQL integration ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 30 | + <link rel="chapter" href="Ch13.html" title="Chapter 13 SML# feature: dynamic types and typed manipulation of JSON ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 31 | + <link rel="chapter" href="Ch14.html" title="Chapter 14 SML# feature: separate compilation ‣ Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 32 | + <link rel="part" href="Pt1.html" title="Part I Overview ‣ SML# Document Version 4.0.0"> |
| 33 | + <link rel="part" href="Pt2.html" title="Part II Tutorials ‣ SML# Document Version 4.0.0"> |
| 34 | + <link rel="part" href="Pt3.html" title="Part III Reference manual ‣ SML# Document Version 4.0.0"> |
| 35 | + <link rel="part" href="Pt4.html" title="Part IV Programming Tools ‣ SML# Document Version 4.0.0"> |
| 36 | + <link rel="part" href="Pt5.html" title="Part V SML# Internals and Data Structures ‣ SML# Document Version 4.0.0"> |
| 37 | + <link rel="part" href="Pt6.html" title="Part VI Bibliography and other documents ‣ SML# Document Version 4.0.0"> |
| 38 | +title: "10.1 Declaring and using C functions‣ Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0" |
| 39 | +--- |
| 40 | +{% raw %} |
| 41 | +<div class="ltx_page_main"> |
| 42 | +<header class="ltx_page_header"><div class="ltx_document_title">SML# Document Version 4.0.0</div> |
| 43 | +<div> |
| 44 | +<a href="Ch10.html" title="Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0" class="ltx_ref" rel="up"><span class="ltx_text ltx_ref_title"><span class="ltx_tag ltx_tag_ref">10 </span> |
| 45 | +SML# feature: direct interface to C</span></a><a href="Ch10.html" title="Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0" class="ltx_ref" rel="prev"><span class="ltx_text ltx_ref_title"><span class="ltx_tag ltx_tag_ref">10 </span> |
| 46 | +SML# feature: direct interface to C</span></a><a href="Ch10.S2.html" title="10.2 Declaring types of C functions ‣ Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0" class="ltx_ref" rel="next"><span class="ltx_text ltx_ref_title"><span class="ltx_tag ltx_tag_ref">10.2 </span>Declaring types of C functions</span></a> |
| 47 | +</div></header> |
| 48 | +<div class="ltx_page_content"> |
| 49 | +<section class="ltx_section ltx_authors_1line"> |
| 50 | +<h1 class="ltx_title ltx_title_section"> |
| 51 | +<span class="ltx_tag ltx_tag_section">10.1 </span>Declaring and using C functions</h1> |
| 52 | + |
| 53 | +<div id="p1" class="ltx_para"> |
| 54 | +<p class="ltx_p">In order to use C function, you only have to declare it in |
| 55 | +SML# in the following syntax.</p> |
| 56 | +<blockquote class="ltx_quote"> |
| 57 | +<p class="ltx_p"><span class="ltx_text ltx_font_typewriter">val <math id="p1.m1" class="ltx_Math" alttext="\mathit{id}" display="inline"><mrow><mi>i</mi><mi>d</mi></mrow></math> = _import "<math id="p1.m2" class="ltx_Math" alttext="\mathit{symbol}" display="inline"><mrow><mi>s</mi><mi>y</mi><mi>m</mi><mi>b</mi><mi>o</mi><mi>l</mi></mrow></math>" : <math id="p1.m3" class="ltx_Math" alttext="\mathit{type}" display="inline"><mrow><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi></mrow></math></span></p> |
| 58 | +</blockquote> |
| 59 | +<p class="ltx_p"><math id="p1.m4" class="ltx_Math" alttext="\mathit{symbol}" display="inline"><mrow><mi>s</mi><mi>y</mi><mi>m</mi><mi>b</mi><mi>o</mi><mi>l</mi></mrow></math> is the name of the C function. |
| 60 | +<math id="p1.m5" class="ltx_Math" alttext="\mathit{type}" display="inline"><mrow><mi>t</mi><mi>y</mi><mi>p</mi><mi>e</mi></mrow></math> is its type. |
| 61 | +Next section explain how to write the type of a C function.</p> |
| 62 | +</div> |
| 63 | +<div id="p2" class="ltx_para"> |
| 64 | +<p class="ltx_p">This declaration instructs SML# compiler to link the |
| 65 | +named function and bind the SML# variable <math id="p2.m1" class="ltx_Math" alttext="\mathit{id}" display="inline"><mrow><mi>i</mi><mi>d</mi></mrow></math> to |
| 66 | +that function. |
| 67 | +A target function linked by this declaration can be any code |
| 68 | +as far as it is in a standard calling convention of the OS in which |
| 69 | +SML# runs. |
| 70 | +The linking to the function is performed at linking time. |
| 71 | +So, to produce an executable file of SML# program containing |
| 72 | +this <span class="ltx_text ltx_font_typewriter">_import</span> declaration, it is required to specify either |
| 73 | +a library or an object file to the command line of SML# command to |
| 74 | +link it with the SML# program. |
| 75 | +Some of standard C libraries (including libc and libm in Unix |
| 76 | +family OS) are linked by default.</p> |
| 77 | +</div> |
| 78 | +<div id="p3" class="ltx_para"> |
| 79 | +<p class="ltx_p">This declaration can appear whenever <span class="ltx_text ltx_font_typewriter">val</span> declaration is |
| 80 | +allied. |
| 81 | +After this declaration, variable <math id="p3.m1" class="ltx_Math" alttext="\mathit{id}" display="inline"><mrow><mi>i</mi><mi>d</mi></mrow></math> can be used as an ordinary |
| 82 | +variable defined in SML#.</p> |
| 83 | +</div> |
| 84 | +<div id="p4" class="ltx_para"> |
| 85 | +<p class="ltx_p">As an example, consider the standard C library function.</p> |
| 86 | +<blockquote class="ltx_quote"> |
| 87 | +<p class="ltx_p"><span class="ltx_text ltx_font_typewriter">int puts(char *);</span></p> |
| 88 | +</blockquote> |
| 89 | +<p class="ltx_p">This function takes a string, appends a newline code and outputs |
| 90 | +it to the standard output, and returns the number of characters actually |
| 91 | +printed. |
| 92 | +If printing fails, then it returns the integer representing |
| 93 | +<span class="ltx_text ltx_font_typewriter">EOF</span> (which is <math id="p4.m1" class="ltx_Math" alttext="-1" display="inline"><mrow><mo>-</mo><mn>1</mn></mrow></math> in Linux). |
| 94 | +This function can be used by writing the following declarations.</p> |
| 95 | +<blockquote class="ltx_quote"> |
| 96 | +<p class="ltx_p"><span class="ltx_text ltx_font_typewriter">val puts = _import "puts" : string -> int</span></p> |
| 97 | +</blockquote> |
| 98 | +<p class="ltx_p">As seen in this example, C function can bound and used just by |
| 99 | +writing <span class="ltx_text ltx_font_typewriter">_import</span> keyword followed by the name and the type of the |
| 100 | +desired function. |
| 101 | +The following is interactive session using <span class="ltx_text ltx_font_typewriter">puts</span>.</p> |
| 102 | +<blockquote class="ltx_quote"> |
| 103 | +<p class="ltx_p"><span class="ltx_text ltx_font_typewriter"># val puts = _import "puts" : string -> int; |
| 104 | +<br class="ltx_break"> |
| 105 | +val puts = _ : string -> int |
| 106 | +<br class="ltx_break"> |
| 107 | +# puts "My first call to a C library"; |
| 108 | +<br class="ltx_break"> |
| 109 | +My first call to a C library |
| 110 | +<br class="ltx_break"> |
| 111 | +val it = 29 : int |
| 112 | +<br class="ltx_break"> |
| 113 | +# map puts ["I","became","fully","operational","in","April","6th","2012."]; |
| 114 | +<br class="ltx_break"> |
| 115 | +I |
| 116 | +<br class="ltx_break"> |
| 117 | +became |
| 118 | +<br class="ltx_break"> |
| 119 | +fully |
| 120 | +<br class="ltx_break"> |
| 121 | +operational |
| 122 | +<br class="ltx_break"> |
| 123 | +in |
| 124 | +<br class="ltx_break"> |
| 125 | +April |
| 126 | +<br class="ltx_break"> |
| 127 | +2nd |
| 128 | +<br class="ltx_break"> |
| 129 | +2012. |
| 130 | +<br class="ltx_break"> |
| 131 | +val it = [2, 7, 6, 12, 3, 6, 4, 5] : int list</span></p> |
| 132 | +</blockquote> |
| 133 | +<p class="ltx_p">The imported C functions can be freely used according to the |
| 134 | +ML’s programming principle – “expressions are freely |
| 135 | +composed as far as they are type consistent”. |
| 136 | +</p> |
| 137 | +</div> |
| 138 | +</section> |
| 139 | +</div> |
| 140 | +<footer class="ltx_page_footer"> |
| 141 | +<div> |
| 142 | +<a href="Ch10.html" title="Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0" class="ltx_ref" rel="prev"><span class="ltx_text ltx_ref_title"><span class="ltx_tag ltx_tag_ref">10 </span> |
| 143 | +SML# feature: direct interface to C</span></a><a href="Ch10.S2.html" title="10.2 Declaring types of C functions ‣ Chapter 10 SML# feature: direct interface to C ‣ Part II Tutorials ‣ SML# Document Version 4.0.0" class="ltx_ref" rel="next"><span class="ltx_text ltx_ref_title"><span class="ltx_tag ltx_tag_ref">10.2 </span>Declaring types of C functions</span></a> |
| 144 | +</div> |
| 145 | +<div class="ltx_page_logo">Generated on Tue Apr 6 18:19:31 2021 by <a href="http://dlmf.nist.gov/LaTeXML/">LaTeXML <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAOCAYAAAD5YeaVAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9wKExQZLWTEaOUAAAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAdpJREFUKM9tkL+L2nAARz9fPZNCKFapUn8kyI0e4iRHSR1Kb8ng0lJw6FYHFwv2LwhOpcWxTjeUunYqOmqd6hEoRDhtDWdA8ApRYsSUCDHNt5ul13vz4w0vWCgUnnEc975arX6ORqN3VqtVZbfbTQC4uEHANM3jSqXymFI6yWazP2KxWAXAL9zCUa1Wy2tXVxheKA9YNoR8Pt+aTqe4FVVVvz05O6MBhqUIBGk8Hn8HAOVy+T+XLJfLS4ZhTiRJgqIoVBRFIoric47jPnmeB1mW/9rr9ZpSSn3Lsmir1fJZlqWlUonKsvwWwD8ymc/nXwVBeLjf7xEKhdBut9Hr9WgmkyGEkJwsy5eHG5vN5g0AKIoCAEgkEkin0wQAfN9/cXPdheu6P33fBwB4ngcAcByHJpPJl+fn54mD3Gg0NrquXxeLRQAAwzAYj8cwTZPwPH9/sVg8PXweDAauqqr2cDjEer1GJBLBZDJBs9mE4zjwfZ85lAGg2+06hmGgXq+j3+/DsixYlgVN03a9Xu8jgCNCyIegIAgx13Vfd7vdu+FweG8YRkjXdWy329+dTgeSJD3ieZ7RNO0VAXAPwDEAO5VKndi2fWrb9jWl9Esul6PZbDY9Go1OZ7PZ9z/lyuD3OozU2wAAAABJRU5ErkJggg==" alt="[LOGO]"></a> |
| 146 | +</div></footer> |
| 147 | +</div> |
| 148 | +{% endraw %} |
0 commit comments