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