123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574 |
- <?xml version="1.0" encoding="utf-8" ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <!-- This file is generated by Nim. -->
- <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Nim Compiler User Guide</title>
- <!-- Google fonts -->
- <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
- <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
- <!-- Favicon -->
- <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
- <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
- <!-- CSS -->
- <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
- <!-- JS -->
- <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
- </head>
- <body>
- <div class="document" id="documentId">
- <div class="container">
- <h1 class="title">Nim Compiler User Guide</h1>
- <div class="row">
- <div class="three columns">
- <div class="theme-select-wrapper">
- <label for="theme-select">Theme: </label>
- <select id="theme-select" onchange="setTheme(this.value)">
- <option value="auto">🌗 Match OS</option>
- <option value="dark">🌑 Dark</option>
- <option value="light">🌕 Light</option>
- </select>
- </div>
- <div id="global-links">
- <ul class="simple-boot">
- <li><a href="manual.html">Manual</a></li>
- <li><a href="lib.html">Standard library</a></li>
- <li> <a id="indexLink" href="theindex.html">Index</a></li>
- <li><a href="compiler/theindex.html">Compiler docs</a></li>
- <li><a href="https://nim-lang.github.io/fusion/theindex.html">Fusion docs</a></li>
- <li><a href="https://nim-lang.github.io/Nim/">devel</a>, <a href="https://nim-lang.org/documentation.html">stable</a></li>
- </ul>
- </div>
- <div id="searchInputDiv">
- Search: <input type="search" id="searchInput"
- oninput="search()" />
- </div>
- <div class="search-groupby">
- Group by:
- <select onchange="groupBy(this.value)">
- <option value="section">Section</option>
- <option value="type">Type</option>
- </select>
- </div>
- <ul class="simple simple-toc" id="toc-list">
- <li><a class="reference" id="introduction_toc" href="#introduction">Introduction</a></li>
- <li><a class="reference" id="compiler-usage_toc" href="#compiler-usage">Compiler Usage</a></li>
- <ul class="simple"><li><a class="reference" id="compiler-usage-commandminusline-switches_toc" href="#compiler-usage-commandminusline-switches">Command-line switches</a></li>
- <li><a class="reference" id="compiler-usage-list-of-warnings_toc" href="#compiler-usage-list-of-warnings">List of warnings</a></li>
- <li><a class="reference" id="compiler-usage-list-of-hints_toc" href="#compiler-usage-list-of-hints">List of hints</a></li>
- <li><a class="reference" id="compiler-usage-verbosity-levels_toc" href="#compiler-usage-verbosity-levels">Verbosity levels</a></li>
- <li><a class="reference" id="compiler-usage-compileminustime-symbols_toc" href="#compiler-usage-compileminustime-symbols">Compile-time symbols</a></li>
- <li><a class="reference" id="compiler-usage-configuration-files_toc" href="#compiler-usage-configuration-files">Configuration files</a></li>
- <li><a class="reference" id="compiler-usage-search-path-handling_toc" href="#compiler-usage-search-path-handling">Search path handling</a></li>
- <li><a class="reference" id="compiler-usage-generated-c-code-directory_toc" href="#compiler-usage-generated-c-code-directory">Generated C code directory</a></li>
- </ul><li><a class="reference" id="compiler-selection_toc" href="#compiler-selection">Compiler Selection</a></li>
- <li><a class="reference" id="crossminuscompilation_toc" href="#crossminuscompilation">Cross-compilation</a></li>
- <li><a class="reference" id="crossminuscompilation-for-windows_toc" href="#crossminuscompilation-for-windows">Cross-compilation for Windows</a></li>
- <li><a class="reference" id="crossminuscompilation-for-android_toc" href="#crossminuscompilation-for-android">Cross-compilation for Android</a></li>
- <li><a class="reference" id="crossminuscompilation-for-ios_toc" href="#crossminuscompilation-for-ios">Cross-compilation for iOS</a></li>
- <li><a class="reference" id="crossminuscompilation-for-nintendo-switch_toc" href="#crossminuscompilation-for-nintendo-switch">Cross-compilation for Nintendo Switch</a></li>
- <li><a class="reference" id="gpu-compilation_toc" href="#gpu-compilation">GPU Compilation</a></li>
- <li><a class="reference" id="dll-generation_toc" href="#dll-generation">DLL generation</a></li>
- <li><a class="reference" id="additional-compilation-switches_toc" href="#additional-compilation-switches">Additional compilation switches</a></li>
- <li><a class="reference" id="additional-features_toc" href="#additional-features">Additional Features</a></li>
- <ul class="simple"><li><a class="reference" id="additional-features-linedir-option_toc" href="#additional-features-linedir-option">LineDir option</a></li>
- <li><a class="reference" id="additional-features-stacktrace-option_toc" href="#additional-features-stacktrace-option">StackTrace option</a></li>
- <li><a class="reference" id="additional-features-linetrace-option_toc" href="#additional-features-linetrace-option">LineTrace option</a></li>
- </ul><li><a class="reference" id="dynliboverride_toc" href="#dynliboverride">DynlibOverride</a></li>
- <li><a class="reference" id="backend-language-options_toc" href="#backend-language-options">Backend language options</a></li>
- <li><a class="reference" id="nim-documentation-tools_toc" href="#nim-documentation-tools">Nim documentation tools</a></li>
- <li><a class="reference" id="nim-idetools-integration_toc" href="#nim-idetools-integration">Nim idetools integration</a></li>
- <li><a class="reference" id="nim-for-embedded-systems_toc" href="#nim-for-embedded-systems">Nim for embedded systems</a></li>
- <ul class="simple"><li><a class="reference" id="nim-for-embedded-systems-nimallocpagesviamalloc_toc" href="#nim-for-embedded-systems-nimallocpagesviamalloc">nimAllocPagesViaMalloc</a></li>
- </ul><li><a class="reference" id="nimpage256-slash-nimpage512-slash-nimpage1k_toc" href="#nimpage256-slash-nimpage512-slash-nimpage1k">nimPage256 / nimPage512 / nimPage1k</a></li>
- <li><a class="reference" id="nimmemaligntiny_toc" href="#nimmemaligntiny">nimMemAlignTiny</a></li>
- <li><a class="reference" id="thread-stack-size_toc" href="#thread-stack-size">Thread stack size</a></li>
- <li><a class="reference" id="nim-for-realtime-systems_toc" href="#nim-for-realtime-systems">Nim for realtime systems</a></li>
- <li><a class="reference" id="signal-handling-in-nim_toc" href="#signal-handling-in-nim">Signal handling in Nim</a></li>
- <li><a class="reference" id="optimizing-for-nim_toc" href="#optimizing-for-nim">Optimizing for Nim</a></li>
- <ul class="simple"><li><a class="reference" id="optimizing-for-nim-optimizing-string-handling_toc" href="#optimizing-for-nim-optimizing-string-handling">Optimizing string handling</a></li>
- </ul>
- </ul>
- </div>
- <div class="nine columns" id="content">
- <a href="https://github.com/nim-lang/Nim/tree/devel/doc/nimc.md#L1" class="link-seesrc" target="_blank">Source</a>
- <a href="https://github.com/nim-lang/Nim/edit/devel/doc/nimc.md#L1" class="link-seesrc" target="_blank" >Edit</a>
- <div id="tocRoot"></div>
-
- <p class="module-desc"><table class="docinfo" frame="void" rules="none"><col class="docinfo-name" /><col class="docinfo-content" /><tbody valign="top"><tr><th class="docinfo-name">Author:</th><td>Andreas Rumpf</td></tr>
- <tr><th class="docinfo-name">Version:</th><td>2.3.1</td></tr>
- </tbody></table><blockquote class="markdown-quote"><p>"Look at you, hacker. A pathetic creature of meat and bone, panting and sweating as you run through my corridors. How can you challenge a perfect, immortal machine?"</p></blockquote>
- <h1><a class="toc-backref" id="introduction" href="#introduction">Introduction</a></h1><p>This document describes the usage of the <em>Nim compiler</em> on the different supported platforms. It is not a definition of the Nim programming language (which is covered in the <a class="reference external" href="manual.html">manual</a>).</p>
- <p>Nim is free software; it is licensed under the <a class="reference external" href="https://www.opensource.org/licenses/mit-license.php">MIT License</a>.</p>
- <h1><a class="toc-backref" id="compiler-usage" href="#compiler-usage">Compiler Usage</a></h1>
- <h2><a class="toc-backref" id="compiler-usage-commandminusline-switches" href="#compiler-usage-commandminusline-switches">Command-line switches</a></h2><p>All options that take a <tt class="docutils literal"><span class="pre"><span class="Identifier">PATH</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Identifier">DIR</span></span></tt> argument are subject to path substitution:</p>
- <ul class="simple"><li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">nim</span></span></tt>: The global nim prefix path</li>
- <li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">lib</span></span></tt>: The stdlib path</li>
- <li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">home</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Operator">~</span></span></tt>: The user's home path</li>
- <li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">config</span></span></tt>: The directory of the module currently being compiled</li>
- <li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">projectname</span></span></tt>: The project file's name without file extension</li>
- <li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">projectpath</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">projectdir</span></span></tt>: The project file's path</li>
- <li><tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">nimcache</span></span></tt>: The nimcache path</li>
- </ul>
- <p>Basic command-line switches are:</p>
- <p>Usage:</p>
- <pre>nim command [options] [projectfile] [arguments]</pre>
- <p>Command:</p>
- <div class="option-list"><div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">compile, c</span></tt></div><div class="option-list-description">compile project with default code generator (C)</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">r</span></tt></div><div class="option-list-description">compile to $nimcache/projname, run with <tt class="docutils literal"><span class="pre">arguments</span></tt> using backend specified by <tt class="docutils literal"><span class="pre">--backend</span></tt> (default: c)</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">doc</span></tt></div><div class="option-list-description">generate the documentation for inputfile for backend specified by <tt class="docutils literal"><span class="pre">--backend</span></tt> (default: c)</div></div>
- </div><p>Arguments: arguments are passed to the program being run (if --run option is selected)</p>
- <p>Options:</p>
- <div class="option-list"><div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">-p, --path:PATH</span></tt></div><div class="option-list-description">add path to search paths</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">-d, --define:SYMBOL(:VAL)</span></tt></div><div class="option-list-description">define a conditional symbol (Optionally: Define the value for that symbol, see: "compile time define pragmas")</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">-u, --undef:SYMBOL</span></tt></div><div class="option-list-description">undefine a conditional symbol</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">-f, --forceBuild:on|off</span></tt></div><div class="option-list-description">force rebuilding of all modules</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--stackTrace:on|off</span></tt></div><div class="option-list-description">turn stack tracing on|off</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--lineTrace:on|off</span></tt></div><div class="option-list-description">turn line tracing on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--threads:on|off</span></tt></div><div class="option-list-description">turn support for multi-threading on|off</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">-x, --checks:on|off</span></tt></div><div class="option-list-description">turn all runtime checks on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">-a, --assertions:on|off</span></tt></div><div class="option-list-description">turn assertions on|off</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--opt:none|speed|size</span></tt></div><div class="option-list-description">optimize not at all or for speed|size Note: use -d:release for a release build!</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--debugger:native</span></tt></div><div class="option-list-description">use native debugger (gdb)</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--app:console|gui|lib|staticlib</span></tt></div><div class="option-list-description">generate a console app|GUI app|DLL|static library</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">-r, --run</span></tt></div><div class="option-list-description">run the compiled program with given arguments</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--eval:cmd</span></tt></div><div class="option-list-description">evaluate nim code directly; e.g.: <tt class="docutils literal"><span class="pre">nim --eval:"echo 1"</span></tt> defaults to <tt class="docutils literal"><span class="pre">e</span></tt> (nimscript) but customizable: <tt class="docutils literal"><span class="pre">nim r --eval:'for a in stdin.lines: echo a'</span></tt></div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--fullhelp</span></tt></div><div class="option-list-description">show all command line switches</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">-h, --help</span></tt></div><div class="option-list-description">show this help</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">-v, --version</span></tt></div><div class="option-list-description">show detailed version information</div></div>
- </div><p>Note, single letter options that take an argument require a colon. E.g. -p:PATH. </p>
- <hr />
- <p>Advanced command-line switches are:</p>
- Advanced commands:<div class="option-list"><div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">compileToC, cc</span></tt></div><div class="option-list-description">compile project with C code generator</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">compileToCpp, cpp</span></tt></div><div class="option-list-description">compile project to C++ code</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">compileToOC, objc</span></tt></div><div class="option-list-description">compile project to Objective C code</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">js</span></tt></div><div class="option-list-description">compile project to Javascript</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">e</span></tt></div><div class="option-list-description">run a Nimscript file</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">md2html</span></tt></div><div class="option-list-description">convert a Markdown file to HTML use <tt class="docutils literal"><span class="pre">--docCmd:skip</span></tt> to skip compiling snippets</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">rst2html</span></tt></div><div class="option-list-description">convert a reStructuredText file to HTML use <tt class="docutils literal"><span class="pre">--docCmd:skip</span></tt> to skip compiling snippets</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">md2tex</span></tt></div><div class="option-list-description">convert a Markdown file to LaTeX</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">rst2tex</span></tt></div><div class="option-list-description">convert a reStructuredText file to LaTeX</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">doc2tex</span></tt></div><div class="option-list-description">extract the documentation to a LaTeX file</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">jsondoc</span></tt></div><div class="option-list-description">extract the documentation to a json file</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">ctags</span></tt></div><div class="option-list-description">create a tags file</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">buildIndex</span></tt></div><div class="option-list-description">build an index for the whole documentation</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">genDepend</span></tt></div><div class="option-list-description">generate a DOT file containing the module dependency graph</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">dump</span></tt></div><div class="option-list-description">dump all defined conditionals and search paths see also: --dump.format:json (useful with: <tt class="docutils literal"><span class="pre">| jq</span></tt>)</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">check</span></tt></div><div class="option-list-description">checks the project for syntax and semantics (can be combined with --defusages)</div></div>
- </div><p>Runtime checks (see -x):</p>
- <div class="option-list"><div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--objChecks:on|off</span></tt></div><div class="option-list-description">turn obj conversion checks on|off</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--fieldChecks:on|off</span></tt></div><div class="option-list-description">turn case variant field checks on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--rangeChecks:on|off</span></tt></div><div class="option-list-description">turn range checks on|off</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--boundChecks:on|off</span></tt></div><div class="option-list-description">turn bound checks on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--overflowChecks:on|off</span></tt></div><div class="option-list-description">turn int over-/underflow checks on|off</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--floatChecks:on|off</span></tt></div><div class="option-list-description">turn all floating point (NaN/Inf) checks on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--nanChecks:on|off</span></tt></div><div class="option-list-description">turn NaN checks on|off</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--infChecks:on|off</span></tt></div><div class="option-list-description">turn Inf checks on|off</div></div>
- </div><p>Advanced options:</p>
- <div class="option-list"><div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--defusages:FILE,LINE,COL</span></tt></div><div class="option-list-description">find the definition and all usages of a symbol</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">-o:FILE, --out:FILE</span></tt></div><div class="option-list-description">set the output filename</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--outdir:DIR</span></tt></div><div class="option-list-description">set the path where the output file will be written</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--usenimcache</span></tt></div><div class="option-list-description">will use <tt class="docutils literal"><span class="pre">outdir=$$nimcache</span></tt>, whichever it resolves to after all options have been processed</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--stdout:on|off</span></tt></div><div class="option-list-description">output to stdout</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--colors:on|off</span></tt></div><div class="option-list-description">turn compiler messages coloring on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--filenames:abs|canonical|legacyRelProj</span></tt></div><div class="option-list-description">customize how filenames are rendered in compiler messages, defaults to <tt class="docutils literal"><span class="pre">abs</span></tt> (absolute)</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--processing:dots|filenames|off</span></tt></div><div class="option-list-description">show files as they're being processed by nim compiler</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--unitsep:on|off</span></tt></div><div class="option-list-description">use the ASCII unit separator (31) between error messages, useful for IDE-like tooling</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--declaredLocs:on|off</span></tt></div><div class="option-list-description">show declaration locations in messages</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--spellSuggest:num</span></tt></div><div class="option-list-description">show at most <tt class="docutils literal"><span class="pre">num >= 0</span></tt> spelling suggestions on typos. if <tt class="docutils literal"><span class="pre">num</span></tt> is not specified (or <tt class="docutils literal"><span class="pre">auto</span></tt>), return an implementation defined set of suggestions.</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--hints:on|off|list</span></tt></div><div class="option-list-description"><tt class="docutils literal"><span class="pre">on|off</span></tt> enables or disables hints. <tt class="docutils literal"><span class="pre">list</span></tt> reports which hints are selected.</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--hint:X:on|off</span></tt></div><div class="option-list-description">turn specific hint X on|off. <tt class="docutils literal"><span class="pre">hint:X</span></tt> means <tt class="docutils literal"><span class="pre">hint:X:on</span></tt>, as with similar flags. <tt class="docutils literal"><span class="pre">all</span></tt> is the set of all hints (only <tt class="docutils literal"><span class="pre">all:off</span></tt> is supported).</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--hintAsError:X:on|off</span></tt></div><div class="option-list-description">turn specific hint X into an error on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">-w:on|off|list, --warnings:on|off|list</span></tt></div><div class="option-list-description"><tt class="docutils literal"><span class="pre">on|off</span></tt> enables or disables warnings. <tt class="docutils literal"><span class="pre">list</span></tt> reports which warnings are selected.</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--warning:X:on|off</span></tt></div><div class="option-list-description">turn specific warning X on|off. <tt class="docutils literal"><span class="pre">warning:X</span></tt> means <tt class="docutils literal"><span class="pre">warning:X:on</span></tt>, as with similar flags. <tt class="docutils literal"><span class="pre">all</span></tt> is the set of all warning (only <tt class="docutils literal"><span class="pre">all:off</span></tt> is supported).</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--warningAsError:X:on|off</span></tt></div><div class="option-list-description">turn specific warning X into an error on|off</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--styleCheck:off|hint|error</span></tt></div><div class="option-list-description">produce hints or errors for Nim identifiers that do not adhere to Nim's official style guide <a class="reference external" href="https://nim-lang.org/docs/nep1.html">https://nim-lang.org/docs/nep1.html</a></div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--styleCheck:usages</span></tt></div><div class="option-list-description">only enforce consistent spellings of identifiers, do not enforce the style on declarations</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--showAllMismatches:on|off</span></tt></div><div class="option-list-description">show all mismatching candidates in overloading resolution</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--lib:PATH</span></tt></div><div class="option-list-description">set the system library path</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--import:PATH</span></tt></div><div class="option-list-description">add an automatically imported module see also <tt class="docutils literal"><span class="pre">patchFile</span></tt> in nimscript which offers more flexibility.</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--include:PATH</span></tt></div><div class="option-list-description">add an automatically included module</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--nimcache:PATH</span></tt></div><div class="option-list-description">set the path used for generated files see also <a class="reference external" href="https://nim-lang.org/docs/nimc.html#compiler-usage-generated-c-code-directory">https://nim-lang.org/docs/nimc.html#compiler-usage-generated-c-code-directory</a></div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">-c, --compileOnly:on|off</span></tt></div><div class="option-list-description">compile Nim files only; do not assemble or link</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--noLinking:on|off</span></tt></div><div class="option-list-description">compile Nim and generated files but do not link</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--noMain:on|off</span></tt></div><div class="option-list-description">do not generate a main procedure</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--genScript:on|off</span></tt></div><div class="option-list-description">generate a compile script (in the 'nimcache' subdirectory named 'compile_$$project$$scriptext'), and a '.deps' file containing the dependencies; implies --compileOnly</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--os:SYMBOL</span></tt></div><div class="option-list-description">set the target operating system (cross-compilation)</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--cpu:SYMBOL</span></tt></div><div class="option-list-description">set the target processor (cross-compilation)</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--debuginfo:on|off</span></tt></div><div class="option-list-description">enables debug information</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">-t, --passC:OPTION</span></tt></div><div class="option-list-description">pass an option to the C compiler</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">-l, --passL:OPTION</span></tt></div><div class="option-list-description">pass an option to the linker</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--cc:SYMBOL</span></tt></div><div class="option-list-description">specify the C compiler</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--cincludes:DIR</span></tt></div><div class="option-list-description">modify the C compiler header search path</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--clibdir:DIR</span></tt></div><div class="option-list-description">modify the linker library search path</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--clib:LIBNAME</span></tt></div><div class="option-list-description">link an additional C library (you should omit platform-specific extensions)</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--project</span></tt></div><div class="option-list-description">document the whole project (doc)</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--docRoot:path</span></tt></div><div class="option-list-description"><tt class="docutils literal"><span class="pre">nim doc --docRoot:/foo --project --outdir:docs /foo/sub/main.nim</span></tt> generates: docs/sub/main.html if path == @pkg, will use nimble file enclosing dir if path == @path, will use first matching dir in <tt class="docutils literal"><span class="pre">--path</span></tt> if path == @default (the default and most useful), will use best match among @pkg,@path. if these are nonexistent, will use project path</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">-b, --backend:c|cpp|js|objc</span></tt></div><div class="option-list-description">sets backend to use with commands like <tt class="docutils literal"><span class="pre">nim doc</span></tt> or <tt class="docutils literal"><span class="pre">nim r</span></tt></div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--docCmd:cmd</span></tt></div><div class="option-list-description">if <tt class="docutils literal"><span class="pre">cmd == skip</span></tt>, skips runnableExamples else, runs runnableExamples with given options, e.g.: <tt class="docutils literal"><span class="pre">--docCmd:"-d:foo --threads:on"</span></tt></div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--docSeeSrcUrl:url</span></tt></div><div class="option-list-description">activate 'see source' for doc command (see doc.item.seesrc in config/nimdoc.cfg)</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--docInternal</span></tt></div><div class="option-list-description">also generate documentation for non-exported symbols</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--lineDir:on|off</span></tt></div><div class="option-list-description">generation of #line directive on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--embedsrc:on|off</span></tt></div><div class="option-list-description">embeds the original source code as comments in the generated output</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--tlsEmulation:on|off</span></tt></div><div class="option-list-description">turn thread local storage emulation on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--implicitStatic:on|off</span></tt></div><div class="option-list-description">turn implicit compile time evaluation on|off</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--trmacros:on|off</span></tt></div><div class="option-list-description">turn term rewriting macros on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--multimethods:on|off</span></tt></div><div class="option-list-description">turn multi-methods on|off</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--hotCodeReloading:on|off</span></tt></div><div class="option-list-description">turn support for hot code reloading on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--excessiveStackTrace:on|off</span></tt></div><div class="option-list-description">stack traces use full file paths</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--stackTraceMsgs:on|off</span></tt></div><div class="option-list-description">enable user defined stack frame msgs via <tt class="docutils literal"><span class="pre">setFrameMsg</span></tt></div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--skipCfg:on|off</span></tt></div><div class="option-list-description">do not read the nim installation's configuration file</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--skipUserCfg:on|off</span></tt></div><div class="option-list-description">do not read the user's configuration file</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--skipParentCfg:on|off</span></tt></div><div class="option-list-description">do not read the parent dirs' configuration files</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--skipProjCfg:on|off</span></tt></div><div class="option-list-description">do not read the project's configuration file</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--mm:orc|arc|refc|markAndSweep|boehm|go|none|regions</span></tt></div><div class="option-list-description">select which memory management to use; default is 'orc'</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--exceptions:setjmp|cpp|goto|quirky</span></tt></div><div class="option-list-description">select the exception handling implementation</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--index:on|off|only</span></tt></div><div class="option-list-description">docgen: turn index file generation on|off (<tt class="docutils literal"><span class="pre">only</span></tt> means not generate output files like HTML)</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--noImportdoc:on|off</span></tt></div><div class="option-list-description">turn loading documentation <tt class="docutils literal"><span class="pre">.idx</span></tt> files on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--putenv:key=value</span></tt></div><div class="option-list-description">set an environment variable</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--NimblePath:PATH</span></tt></div><div class="option-list-description">add a path for Nimble support</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--noNimblePath</span></tt></div><div class="option-list-description">deactivate the Nimble path</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--clearNimblePath</span></tt></div><div class="option-list-description">empty the list of Nimble package search paths</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--cppCompileToNamespace:namespace</span></tt></div><div class="option-list-description">use the provided namespace for the generated C++ code, if no namespace is provided "Nim" will be used</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--nimMainPrefix:prefix</span></tt></div><div class="option-list-description">use <tt class="docutils literal"><span class="pre">{prefix}NimMain</span></tt> instead of <tt class="docutils literal"><span class="pre">NimMain</span></tt> in the produced C/C++ code</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--expandMacro:MACRO</span></tt></div><div class="option-list-description">dump every generated AST from MACRO</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--expandArc:PROCNAME</span></tt></div><div class="option-list-description">show how PROCNAME looks like after diverse optimizations before the final backend phase (mostly ARC/ORC specific)</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--excludePath:PATH</span></tt></div><div class="option-list-description">exclude a path from the list of search paths</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--dynlibOverride:SYMBOL</span></tt></div><div class="option-list-description">marks SYMBOL so that dynlib:SYMBOL has no effect and can be statically linked instead; symbol matching is fuzzy so that --dynlibOverride:lua matches dynlib: "liblua.so.3"</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--dynlibOverrideAll</span></tt></div><div class="option-list-description">disables the effects of the dynlib pragma</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--listCmd</span></tt></div><div class="option-list-description">list the compilation commands; can be combined with <tt class="docutils literal"><span class="pre">--hint:exec:on</span></tt> and <tt class="docutils literal"><span class="pre">--hint:link:on</span></tt></div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--asm</span></tt></div><div class="option-list-description">produce assembler code</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--parallelBuild:0|1|...</span></tt></div><div class="option-list-description">perform a parallel build value = number of processors (0 for auto-detect)</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--incremental:on|off</span></tt></div><div class="option-list-description">only recompile the changed modules (experimental!)</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--verbosity:0|1|2|3</span></tt></div><div class="option-list-description">set Nim's verbosity level (1 is default)</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--errorMax:N</span></tt></div><div class="option-list-description">stop compilation after N errors; 0 means unlimited</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--maxLoopIterationsVM:N</span></tt></div><div class="option-list-description">set max iterations for all VM loops</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--maxCallDepthVM:N</span></tt></div><div class="option-list-description">set max call depth in the VM</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--experimental:$1</span></tt></div><div class="option-list-description">enable experimental language feature</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--legacy:$2</span></tt></div><div class="option-list-description">enable obsolete/legacy language feature</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--benchmarkVM:on|off</span></tt></div><div class="option-list-description">turn benchmarking of VM code with cpuTime() on|off</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--profileVM:on|off</span></tt></div><div class="option-list-description">turn compile time VM profiler on|off</div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--panics:on|off</span></tt></div><div class="option-list-description">turn panics into process terminations (default: off)</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--deepcopy:on|off</span></tt></div><div class="option-list-description">enable 'system.deepCopy' for <tt class="docutils literal"><span class="pre">--mm:arc|orc</span></tt></div></div>
- <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--jsbigint64:on|off</span></tt></div><div class="option-list-description">toggle the use of BigInt for 64-bit integers for the JavaScript backend (default: on)</div></div>
- <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--nimBasePattern:nimbase.h</span></tt></div><div class="option-list-description">allows to specify a custom pattern for <tt class="docutils literal"><span class="pre">nimbase.h</span></tt></div></div>
- </div>
- <h2><a class="toc-backref" id="compiler-usage-list-of-warnings" href="#compiler-usage-list-of-warnings">List of warnings</a></h2><p>Each warning can be activated individually with <tt class="docutils literal"><span class="pre option">--warning:NAME:on|off</span></tt> or in a <tt class="docutils literal"><span class="pre"><span class="Identifier">push</span></span></tt> pragma with <tt class="docutils literal"><span class="pre"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">warning</span><span class="Punctuation">[</span><span class="Identifier">NAME</span><span class="Punctuation">]</span><span class="Punctuation">:</span><span class="Identifier">on</span><span class="Operator">|</span><span class="Identifier">off</span><span class="Operator">.</span><span class="Punctuation">}</span></span></tt>.</p>
- <table border="1" class="docutils"><tr><th>Name</th><th>Description</th></tr>
- <tr><td>CannotOpenFile</td><td>Some file not essential for the compiler's working could not be opened.</td></tr>
- <tr><td>OctalEscape</td><td>The code contains an unsupported octal sequence.</td></tr>
- <tr><td>Deprecated</td><td>The code uses a deprecated symbol.</td></tr>
- <tr><td>ConfigDeprecated</td><td>The project makes use of a deprecated config file.</td></tr>
- <tr><td>SmallLshouldNotBeUsed</td><td>The letter 'l' should not be used as an identifier.</td></tr>
- <tr><td>EachIdentIsTuple</td><td>The code contains a confusing <tt class="docutils literal"><span class="pre"><span class="Keyword">var</span></span></tt> declaration.</td></tr>
- <tr><td>CStringConv</td><td>Warn about dangerous implicit conversions to <tt class="docutils literal"><span class="pre"><span class="Identifier">cstring</span></span></tt>.</td></tr>
- <tr><td>EnumConv</td><td>Warn about conversions from enum to enum.</td></tr>
- <tr><td>AnyEnumConv</td><td>Warn about any conversions to an enum type.</td></tr>
- <tr><td>HoleEnumConv</td><td>Warn about conversion to an enum with holes. These conversions are unsafe.</td></tr>
- <tr><td>ResultUsed</td><td>Warn about the usage of the built-in <tt class="docutils literal"><span class="pre"><span class="Identifier">result</span></span></tt> variable.</td></tr>
- <tr><td>User</td><td>Some user-defined warning.</td></tr>
- </table>
- <h2><a class="toc-backref" id="compiler-usage-list-of-hints" href="#compiler-usage-list-of-hints">List of hints</a></h2><p>Each hint can be activated individually with <tt class="docutils literal"><span class="pre option">--hint:NAME:on|off</span></tt> or in a <tt class="docutils literal"><span class="pre"><span class="Identifier">push</span></span></tt> pragma with <tt class="docutils literal"><span class="pre"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">hint</span><span class="Punctuation">[</span><span class="Identifier">NAME</span><span class="Punctuation">]</span><span class="Punctuation">:</span><span class="Identifier">on</span><span class="Operator">|</span><span class="Identifier">off</span><span class="Operator">.</span><span class="Punctuation">}</span></span></tt>.</p>
- <table border="1" class="docutils"><tr><th>Name</th><th>Description</th></tr>
- <tr><td>CC</td><td>Shows when the C compiler is called.</td></tr>
- <tr><td>CodeBegin</td><td></td></tr>
- <tr><td>CodeEnd</td><td></td></tr>
- <tr><td>CondTrue</td><td></td></tr>
- <tr><td>Conf</td><td>A config file was loaded.</td></tr>
- <tr><td>ConvToBaseNotNeeded</td><td></td></tr>
- <tr><td>ConvFromXtoItselfNotNeeded</td><td></td></tr>
- <tr><td>Dependency</td><td></td></tr>
- <tr><td>Exec</td><td>Program is executed.</td></tr>
- <tr><td>ExprAlwaysX</td><td></td></tr>
- <tr><td>ExtendedContext</td><td></td></tr>
- <tr><td>GCStats</td><td>Dumps statistics about the Garbage Collector.</td></tr>
- <tr><td>GlobalVar</td><td>Shows global variables declarations.</td></tr>
- <tr><td>Link</td><td>Linking phase.</td></tr>
- <tr><td>Name</td><td></td></tr>
- <tr><td>Path</td><td>Search paths modifications.</td></tr>
- <tr><td>Pattern</td><td></td></tr>
- <tr><td>Performance</td><td></td></tr>
- <tr><td>Processing</td><td>Artifact being compiled.</td></tr>
- <tr><td>QuitCalled</td><td></td></tr>
- <tr><td>Source</td><td>The source line that triggered a diagnostic message.</td></tr>
- <tr><td>StackTrace</td><td></td></tr>
- <tr><td>Success, SuccessX</td><td>Successful compilation of a library or a binary.</td></tr>
- <tr><td>User</td><td></td></tr>
- <tr><td>UserRaw</td><td></td></tr>
- <tr><td>XDeclaredButNotUsed</td><td>Unused symbols in the code.</td></tr>
- </table>
- <h2><a class="toc-backref" id="compiler-usage-verbosity-levels" href="#compiler-usage-verbosity-levels">Verbosity levels</a></h2><table border="1" class="docutils"><tr><th>Level</th><th>Description</th></tr>
- <tr><td>0</td><td>Minimal output level for the compiler.</td></tr>
- <tr><td>1</td><td>Displays compilation of all the compiled files, including those imported by other modules or through the <a class="reference external" href=" manual.html#implementation-specific-pragmas-compile-pragma">compile pragma</a>. This is the default level.</td></tr>
- <tr><td>2</td><td>Displays compilation statistics, enumerates the dynamic libraries that will be loaded by the final binary, and dumps to standard output the result of applying <a class="reference external" href=" filters.html">a filter to the source code</a> if any filter was used during compilation.</td></tr>
- <tr><td>3</td><td>In addition to the previous levels dumps a debug stack trace for compiler developers.</td></tr>
- </table>
- <h2><a class="toc-backref" id="compiler-usage-compileminustime-symbols" href="#compiler-usage-compileminustime-symbols">Compile-time symbols</a></h2><p>Through the <tt class="docutils literal"><span class="pre option">-d:x</span></tt> or <tt class="docutils literal"><span class="pre option">--define:x</span></tt> switch you can define compile-time symbols for conditional compilation. The defined switches can be checked in source code with the <a class="reference external" href=" manual.html#statements-and-expressions-when-statement">when statement</a> and <a class="reference external" href="system.html#defined,untyped">defined proc</a>. The typical use of this switch is to enable builds in release mode (<tt class="docutils literal"><span class="pre option">-d:release</span></tt>) where optimizations are enabled for better performance. Another common use is the <tt class="docutils literal"><span class="pre option">-d:ssl</span></tt> switch to activate SSL sockets.</p>
- <p>Additionally, you may pass a value along with the symbol: <tt class="docutils literal"><span class="pre option">-d:x=y</span></tt> which may be used in conjunction with the <a class="reference external" href="manual.html#implementation-specific-pragmas-compileminustime-define-pragmas">compile-time define pragmas</a> to override symbols during build time.</p>
- <p>Compile-time symbols are completely <strong>case insensitive</strong> and underscores are ignored too. <tt class="docutils literal"><span class="pre option">--define:FOO</span></tt> and <tt class="docutils literal"><span class="pre option">--define:foo</span></tt> are identical.</p>
- <p>Compile-time symbols starting with the <tt class="docutils literal"><span class="pre"><span class="Identifier">nim</span></span></tt> prefix are reserved for the implementation and should not be used elsewhere.</p>
- <table border="1" class="docutils"><tr><th>Name</th><th>Description</th></tr>
- <tr><td>nimStdSetjmp</td><td>Use the standard <tt class="docutils literal"><span class="pre"><span class="Identifier">setjmp</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Operator">/</span><span class="Identifier">longjmp</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> library functions for setjmp-based exceptions. This is the default on most platforms.</td></tr>
- <tr><td>nimSigSetjmp</td><td>Use <tt class="docutils literal"><span class="pre"><span class="Identifier">sigsetjmp</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Operator">/</span><span class="Identifier">siglongjmp</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> for setjmp-based exceptions.</td></tr>
- <tr><td>nimRawSetjmp</td><td>Use <tt class="docutils literal"><span class="pre"><span class="Identifier">_setjmp</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Operator">/</span><span class="Identifier">_longjmp</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> on POSIX and <tt class="docutils literal"><span class="pre"><span class="Identifier">_setjmp</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Operator">/</span><span class="Identifier">longjmp</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> on Windows, for setjmp-based exceptions. It's the default on BSDs and BSD-like platforms, where it's significantly faster than the standard functions.</td></tr>
- <tr><td>nimBuiltinSetjmp</td><td>Use <tt class="docutils literal"><span class="pre"><span class="Identifier">__builtin_setjmp</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Operator">/</span><span class="Identifier">__builtin_longjmp</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> for setjmp-based exceptions. This will not work if an exception is being thrown and caught inside the same procedure. Useful for benchmarking.</td></tr>
- </table>
- <h2><a class="toc-backref" id="compiler-usage-configuration-files" href="#compiler-usage-configuration-files">Configuration files</a></h2><p><strong>Note:</strong> The <em>project file name</em> is the name of the <tt class="docutils literal"><span class="pre">.nim</span></tt> file that is passed as a command-line argument to the compiler.</p>
- <p>The <tt class="docutils literal"><span class="pre"><span class="program">nim</span></span></tt> executable processes configuration files in the following directories (in this order; later files overwrite previous settings):</p>
- <ol class="simple"><li><tt class="docutils literal"><span class="pre">$nim/config/nim.cfg</span></tt>, <tt class="docutils literal"><span class="pre">/etc/nim/nim.cfg</span></tt> (UNIX) or <tt class="docutils literal"><span class="pre"><Nim's installation directory>\config\nim.cfg</span></tt> (Windows). This file can be skipped with the <tt class="docutils literal"><span class="pre option">--skipCfg</span></tt> command line option.</li>
- <li>If environment variable <tt class="docutils literal"><span class="pre"><span class="Identifier">XDG_CONFIG_HOME</span></span></tt> is defined, <tt class="docutils literal"><span class="pre">$XDG_CONFIG_HOME/nim/nim.cfg</span></tt> or <tt class="docutils literal"><span class="pre">~/.config/nim/nim.cfg</span></tt> (POSIX) or <tt class="docutils literal"><span class="pre">%APPDATA%/nim/nim.cfg</span></tt> (Windows). This file can be skipped with the <tt class="docutils literal"><span class="pre option">--skipUserCfg</span></tt> command line option.</li>
- <li><tt class="docutils literal"><span class="pre">$parentDir/nim.cfg</span></tt> where <tt class="docutils literal"><span class="pre">$parentDir</span></tt> stands for any parent directory of the project file's path. These files can be skipped with the <tt class="docutils literal"><span class="pre option">--skipParentCfg</span></tt> command-line option.</li>
- <li><tt class="docutils literal"><span class="pre">$projectDir/nim.cfg</span></tt> where <tt class="docutils literal"><span class="pre">$projectDir</span></tt> stands for the project file's path. This file can be skipped with the <tt class="docutils literal"><span class="pre option">--skipProjCfg</span></tt> command-line option.</li>
- <li>A project can also have a project-specific configuration file named <tt class="docutils literal"><span class="pre">$project.nim.cfg</span></tt> that resides in the same directory as <tt class="docutils literal"><span class="pre">$project.nim</span></tt>. This file can be skipped with the <tt class="docutils literal"><span class="pre option">--skipProjCfg</span></tt> command-line option.</li>
- </ol>
- <p>Command-line settings have priority over configuration file settings.</p>
- <p>The default build of a project is a <span id="debug-build_1">debug build</span>. To compile a <span id="release-build_1">release build</span> define the <tt class="docutils literal"><span class="pre"><span class="Identifier">release</span></span></tt> symbol:</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">-d:release</span> <span class="Identifier">myproject.nim</span></pre></p>
- <p>To compile a <span id="dangerous-release-build_1">dangerous release build</span> define the <tt class="docutils literal"><span class="pre"><span class="Identifier">danger</span></span></tt> symbol:</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">-d:danger</span> <span class="Identifier">myproject.nim</span></pre></p>
- <h2><a class="toc-backref" id="compiler-usage-search-path-handling" href="#compiler-usage-search-path-handling">Search path handling</a></h2><p>Nim has the concept of a global search path (PATH) that is queried to determine where to find imported modules or include files. If multiple files are found an ambiguity error is produced.</p>
- <p><tt class="docutils literal"><span class="pre"><span class="program">nim</span> <span class="option">dump</span></span></tt> shows the contents of the PATH.</p>
- <p>However before the PATH is used the current directory is checked for the file's existence. So if PATH contains <tt class="docutils literal"><span class="pre">$lib</span></tt> and <tt class="docutils literal"><span class="pre">$lib/bar</span></tt> and the directory structure looks like this:</p>
- <pre>$lib/x.nim
- $lib/bar/x.nim
- foo/x.nim
- foo/main.nim
- other.nim</pre>
- <p>And <tt class="docutils literal"><span class="pre"><span class="Identifier">main</span></span></tt> imports <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">foo</span><span class="Operator">/</span><span class="Identifier">x</span></span></tt> is imported. If <tt class="docutils literal"><span class="pre"><span class="Identifier">other</span></span></tt> imports <tt class="docutils literal"><span class="pre"><span class="Identifier">x</span></span></tt> then both <tt class="docutils literal"><span class="pre">$lib/x.nim</span></tt> and <tt class="docutils literal"><span class="pre">$lib/bar/x.nim</span></tt> match but <tt class="docutils literal"><span class="pre">$lib/x.nim</span></tt> is used as it is the first match.</p>
- <h2><a class="toc-backref" id="compiler-usage-generated-c-code-directory" href="#compiler-usage-generated-c-code-directory">Generated C code directory</a></h2><p>The generated files that Nim produces all go into a subdirectory called <tt class="docutils literal"><span class="pre">nimcache</span></tt>. Its full path is</p>
- <ul class="simple"><li><tt class="docutils literal"><span class="pre">$XDG_CACHE_HOME/nim/$projectname(_r|_d)</span></tt> or <tt class="docutils literal"><span class="pre">~/.cache/nim/$projectname(_r|_d)</span></tt> on Posix</li>
- <li><tt class="docutils literal"><span class="pre">$HOME\nimcache\$projectname(_r|_d)</span></tt> on Windows.</li>
- </ul>
- <p>The <tt class="docutils literal"><span class="pre"><span class="Identifier">_r</span></span></tt> suffix is used for release builds, <tt class="docutils literal"><span class="pre"><span class="Identifier">_d</span></span></tt> is for debug builds.</p>
- <p>This makes it easy to delete all generated files.</p>
- <p>The <tt class="docutils literal"><span class="pre option">--nimcache</span></tt> <a class="reference internal" href="#compiler-usage-commandminusline-switches">compiler switch</a> can be used to to change the <tt class="docutils literal"><span class="pre">nimcache</span></tt> directory.</p>
- <p>However, the generated C code is not platform-independent. C code generated for Linux does not compile on Windows, for instance. The comment on top of the C file lists the OS, CPU, and CC the file has been compiled for.</p>
- <h1><a class="toc-backref" id="compiler-selection" href="#compiler-selection">Compiler Selection</a></h1><p>To change the compiler from the default compiler (at the command line):</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">--cc:llvm_gcc</span> <span class="option">--compile_only</span> <span class="Identifier">myfile.nim</span></pre></p>
- <p>This uses the configuration defined in <tt class="docutils literal"><span class="pre">config\nim.cfg</span></tt> for <tt class="docutils literal"><span class="pre"><span class="program">llvm_gcc</span></span></tt>.</p>
- <p>If nimcache already contains compiled code from a different compiler for the same project, add the <tt class="docutils literal"><span class="pre option">-f</span></tt> flag to force all files to be recompiled.</p>
- <p>The default compiler is defined at the top of <tt class="docutils literal"><span class="pre">config\nim.cfg</span></tt>. Changing this setting affects the compiler used by <tt class="docutils literal"><span class="pre"><span class="program">koch</span></span></tt> to (re)build Nim.</p>
- <p>To use the <tt class="docutils literal"><span class="pre"><span class="Identifier">CC</span></span></tt> environment variable, use <tt class="docutils literal"><span class="pre"><span class="program">nim</span> <span class="option">c</span> <span class="option">--cc:env</span> <span class="Identifier">myfile.nim</span></span></tt>. To use the <tt class="docutils literal"><span class="pre"><span class="Identifier">CXX</span></span></tt> environment variable, use <tt class="docutils literal"><span class="pre"><span class="program">nim</span> <span class="option">cpp</span> <span class="option">--cc:env</span> <span class="Identifier">myfile.nim</span></span></tt>. <tt class="docutils literal"><span class="pre option">--cc:env</span></tt> is available since Nim version 1.4.</p>
- <h1><a class="toc-backref" id="crossminuscompilation" href="#crossminuscompilation">Cross-compilation</a></h1><p>To cross compile, use for example:</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">--cpu:i386</span> <span class="option">--os:linux</span> <span class="option">--compileOnly</span> <span class="option">--genScript</span> <span class="Identifier">myproject.nim</span></pre></p>
- <p>Then move the C code and the compile script <tt class="docutils literal"><span class="pre"><span class="program">compile_myproject.sh</span></span></tt> to your Linux i386 machine and run the script.</p>
- <p>Another way is to make Nim invoke a cross compiler toolchain:</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">--cpu:arm</span> <span class="option">--os:linux</span> <span class="Identifier">myproject.nim</span></pre></p>
- <p>For cross compilation, the compiler invokes a C compiler named like <tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">cpu</span><span class="Operator">.$</span><span class="Identifier">os</span><span class="Operator">.$</span><span class="Identifier">cc</span></span></tt> (for example <tt class="docutils literal"><span class="pre"><span class="Identifier">arm</span><span class="Operator">.</span><span class="Identifier">linux</span><span class="Operator">.</span><span class="Identifier">gcc</span></span></tt>) with options defined in <tt class="docutils literal"><span class="pre"><span class="Operator">$</span><span class="Identifier">cpu</span><span class="Operator">.$</span><span class="Identifier">os</span><span class="Operator">.$</span><span class="Identifier">cc</span><span class="Operator">.</span><span class="Identifier">options</span><span class="Operator">.</span><span class="Identifier">always</span></span></tt>. The configuration system is used to provide meaningful defaults. For example, for Linux on a 32-bit ARM CPU, your configuration file should contain something like:</p>
- <pre>arm.linux.gcc.path = "/usr/bin"
- arm.linux.gcc.exe = "arm-linux-gcc"
- arm.linux.gcc.linkerexe = "arm-linux-gcc"
- arm.linux.gcc.options.always = "-w -fmax-errors=3"</pre>
- <h1><a class="toc-backref" id="crossminuscompilation-for-windows" href="#crossminuscompilation-for-windows">Cross-compilation for Windows</a></h1><p>To cross-compile for Windows from Linux or macOS using the MinGW-w64 toolchain:</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">-d:mingw</span> <span class="Identifier">myproject.nim</span>
- <span class="Comment"># `nim r` also works, running the binary via `wine` or `wine64`:</span>
- <span class="program">nim</span> <span class="option">r</span> <span class="option">-d:mingw</span> <span class="option">--eval:</span><span class="option">'import os; echo "a" / "b"'</span></pre></p>
- <p>Use <tt class="docutils literal"><span class="pre option">--cpu:i386</span></tt> or <tt class="docutils literal"><span class="pre option">--cpu:amd64</span></tt> to switch the CPU architecture.</p>
- <p>The MinGW-w64 toolchain can be installed as follows:</p>
- <p><pre class="listing"><span class="program">apt</span> <span class="option">install</span> <span class="option">mingw-w64</span> <span class="Comment"># Ubuntu</span>
- <span class="program">yum</span> <span class="option">install</span> <span class="option">mingw32-gcc</span>
- <span class="program">yum</span> <span class="option">install</span> <span class="option">mingw64-gcc</span> <span class="Comment"># CentOS - requires EPEL</span>
- <span class="program">brew</span> <span class="option">install</span> <span class="option">mingw-w64</span> <span class="Comment"># OSX</span></pre></p>
- <h1><a class="toc-backref" id="crossminuscompilation-for-android" href="#crossminuscompilation-for-android">Cross-compilation for Android</a></h1><p>There are two ways to compile for Android: terminal programs (Termux) and with the NDK (Android Native Development Kit).</p>
- <p>The first one is to treat Android as a simple Linux and use <a class="reference external" href="https://wiki.termux.com">Termux</a> to connect and run the Nim compiler directly on android as if it was Linux. These programs are console-only programs that can't be distributed in the Play Store.</p>
- <p>Use regular <tt class="docutils literal"><span class="pre"><span class="program">nim</span> <span class="option">c</span></span></tt> inside termux to make Android terminal programs.</p>
- <p>Normal Android apps are written in Java, to use Nim inside an Android app you need a small Java stub that calls out to a native library written in Nim using the <a class="reference external" href="https://developer.android.com/ndk">NDK</a>. You can also use <a class="reference external" href="https://developer.android.com/ndk/samples/sample_na">native-activity</a> to have the Java stub be auto-generated for you.</p>
- <p>Use <tt class="docutils literal"><span class="pre"><span class="program">nim</span> <span class="option">c</span> <span class="option">-c</span> <span class="option">--cpu:arm</span> <span class="option">--os:android</span> <span class="option">-d:androidNDK</span> <span class="option">--noMain:on</span></span></tt> to generate the C source files you need to include in your Android Studio project. Add the generated C files to CMake build script in your Android project. Then do the final compile with Android Studio which uses Gradle to call CMake to compile the project.</p>
- <p>Because Nim is part of a library it can't have its own C-style <tt class="docutils literal"><span class="pre"><span class="Identifier">main</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> so you would need to define your own <tt class="docutils literal"><span class="pre"><span class="Identifier">android_main</span></span></tt> and init the Java environment, or use a library like SDL2 or GLFM to do it. After the Android stuff is done, it's very important to call <tt class="docutils literal"><span class="pre"><span class="Identifier">NimMain</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> in order to initialize Nim's garbage collector and to run the top level statements of your program.</p>
- <p><pre class="listing"><span class="Keyword">proc</span> <span class="Identifier">NimMain</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">importc</span><span class="Operator">.</span><span class="Punctuation">}</span>
- <span class="Keyword">proc</span> <span class="Identifier">glfmMain</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">display</span><span class="Punctuation">:</span> <span class="Keyword">ptr</span> <span class="Identifier">GLFMDisplay</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">exportc</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span>
- <span class="Identifier">NimMain</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Comment"># initialize garbage collector memory, types and stack</span></pre></p>
- <p>The name <tt class="docutils literal"><span class="pre"><span class="Identifier">NimMain</span></span></tt> can be influenced via the <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">nimMainPrefix</span><span class="Punctuation">:</span><span class="Identifier">prefix</span></span></tt> switch. Use <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">nimMainPrefix</span><span class="Punctuation">:</span><span class="Identifier">MyLib</span></span></tt> and the function to call is named <tt class="docutils literal"><span class="pre"><span class="Identifier">MyLibNimMain</span></span></tt>.</p>
- <h1><a class="toc-backref" id="crossminuscompilation-for-ios" href="#crossminuscompilation-for-ios">Cross-compilation for iOS</a></h1><p>To cross-compile for iOS you need to be on a macOS computer and use XCode. Normal languages for iOS development are Swift and Objective C. Both of these use LLVM and can be compiled into object files linked together with C, C++ or Objective C code produced by Nim.</p>
- <p>Use <tt class="docutils literal"><span class="pre"><span class="program">nim</span> <span class="option">c</span> <span class="option">-c</span> <span class="option">--os:ios</span> <span class="option">--noMain:on</span></span></tt> to generate C files and include them in your XCode project. Then you can use XCode to compile, link, package and sign everything.</p>
- <p>Because Nim is part of a library it can't have its own C-style <tt class="docutils literal"><span class="pre"><span class="Identifier">main</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> so you would need to define <tt class="docutils literal"><span class="pre"><span class="Identifier">main</span></span></tt> that calls <tt class="docutils literal"><span class="pre"><span class="Identifier">autoreleasepool</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">UIApplicationMain</span></span></tt> to do it, or use a library like SDL2 or GLFM. After the iOS setup is done, it's very important to call <tt class="docutils literal"><span class="pre"><span class="Identifier">NimMain</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt> to initialize Nim's garbage collector and to run the top-level statements of your program.</p>
- <p><pre class="listing"><span class="Keyword">proc</span> <span class="Identifier">NimMain</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">importc</span><span class="Operator">.</span><span class="Punctuation">}</span>
- <span class="Keyword">proc</span> <span class="Identifier">glfmMain</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">display</span><span class="Punctuation">:</span> <span class="Keyword">ptr</span> <span class="Identifier">GLFMDisplay</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">exportc</span><span class="Operator">.</span><span class="Punctuation">}</span> <span class="Operator">=</span>
- <span class="Identifier">NimMain</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Comment"># initialize garbage collector memory, types and stack</span></pre></p>
- <p>Note: XCode's "make clean" gets confused about the generated nim.c files, so you need to clean those files manually to do a clean build.</p>
- <p>The name <tt class="docutils literal"><span class="pre"><span class="Identifier">NimMain</span></span></tt> can be influenced via the <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">nimMainPrefix</span><span class="Punctuation">:</span><span class="Identifier">prefix</span></span></tt> switch. Use <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">nimMainPrefix</span><span class="Punctuation">:</span><span class="Identifier">MyLib</span></span></tt> and the function to call is named <tt class="docutils literal"><span class="pre"><span class="Identifier">MyLibNimMain</span></span></tt>.</p>
- <h1><a class="toc-backref" id="crossminuscompilation-for-nintendo-switch" href="#crossminuscompilation-for-nintendo-switch">Cross-compilation for Nintendo Switch</a></h1><p>Simply add <tt class="docutils literal"><span class="pre option">--os:nintendoswitch</span></tt> to your usual <tt class="docutils literal"><span class="pre"><span class="program">nim</span> <span class="option">c</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="program">nim</span> <span class="option">cpp</span></span></tt> command and set the <tt class="docutils literal"><span class="pre option">passC</span></tt> and <tt class="docutils literal"><span class="pre option">passL</span></tt> command line switches to something like:</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="Identifier">...</span> <span class="option">--d:nimAllocPagesViaMalloc</span> <span class="option">--mm:orc</span> <span class="option">--passC=</span><span class="option">"-I$DEVKITPRO/libnx/include"</span> <span class="Identifier">...</span>
- <span class="Identifier">--passL=</span><span class="option">"-specs=$DEVKITPRO/libnx/switch.specs -L$DEVKITPRO/libnx/lib -lnx"</span></pre></p>
- <p>or setup a <tt class="docutils literal"><span class="pre">nim.cfg</span></tt> file like so:</p>
- <pre>#nim.cfg
- --mm:orc
- --d:nimAllocPagesViaMalloc
- --define:nimInheritHandles
- --passC="-I$DEVKITPRO/libnx/include"
- --passL="-specs=$DEVKITPRO/libnx/switch.specs -L$DEVKITPRO/libnx/lib -lnx"</pre>
- <p>The devkitPro setup must be the same as the default with their new installer <a class="reference external" href="https://github.com/devkitPro/pacman/releases">here for Mac/Linux</a> or <a class="reference external" href="https://github.com/devkitPro/installer/releases">here for Windows</a>.</p>
- <p>For example, with the above-mentioned config:</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">--os:nintendoswitch</span> <span class="Identifier">switchhomebrew.nim</span></pre></p>
- <p>This will generate a file called <tt class="docutils literal"><span class="pre">switchhomebrew.elf</span></tt> which can then be turned into an nro file with the <tt class="docutils literal"><span class="pre"><span class="program">elf2nro</span></span></tt> tool in the devkitPro release. Examples can be found at <a class="reference external" href="https://github.com/jyapayne/nim-libnx.git">the nim-libnx github repo</a>.</p>
- <p>There are a few things that don't work because the devkitPro libraries don't support them. They are:</p>
- <ol class="simple"><li>Waiting for a subprocess to finish. A subprocess can be started, but right now it can't be waited on, which sort of makes subprocesses a bit hard to use</li>
- <li>Dynamic calls. Switch OS (Horizon) doesn't support dynamic libraries, so dlopen/dlclose are not available.</li>
- <li>mqueue. Sadly there are no mqueue headers.</li>
- <li>ucontext. No headers for these either. No coroutines for now :(</li>
- <li>nl_types. No headers for this.</li>
- <li>As mmap is not supported, the nimAllocPagesViaMalloc option has to be used.</li>
- </ol>
- <h1><a class="toc-backref" id="gpu-compilation" href="#gpu-compilation">GPU Compilation</a></h1><p>Compiling for GPU computation can be achieved with <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">cc</span><span class="Punctuation">:</span><span class="Identifier">nvcc</span></span></tt> for CUDA with nvcc, or with <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">cc</span><span class="Punctuation">:</span><span class="Identifier">hipcc</span></span></tt> for AMD GPUs with HIP. Both compilers require building for C++ with <tt class="docutils literal"><span class="pre"><span class="Identifier">nim</span> <span class="Identifier">cpp</span></span></tt>.</p>
- <p>Here's a very simple CUDA kernel example using emit, which can be compiled with <tt class="docutils literal"><span class="pre"><span class="Identifier">nim</span> <span class="Identifier">cpp</span> <span class="Operator">--</span><span class="Identifier">cc</span><span class="Punctuation">:</span><span class="Identifier">nvcc</span> <span class="Operator">--</span><span class="Identifier">define</span><span class="Punctuation">:</span><span class="StringLit">"useMalloc"</span> <span class="Identifier">hello_kernel</span><span class="Operator">.</span><span class="Identifier">nim</span></span></tt> assuming you have the CUDA toolkit installed.</p>
- <p><pre class="listing"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">emit</span><span class="Punctuation">:</span> <span class="LongStringLit">"""
- __global__ void add(int a, int b) {
- int c;
- c = a + b;
- }
- """</span><span class="Operator">.</span><span class="Punctuation">}</span>
- <span class="Keyword">proc</span> <span class="Identifier">main</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=</span>
- <span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">emit</span><span class="Punctuation">:</span> <span class="LongStringLit">"""
- add<<<1,1>>>(2,7);
- """</span><span class="Operator">.</span><span class="Punctuation">}</span>
- <span class="Identifier">main</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></p>
- <h1><a class="toc-backref" id="dll-generation" href="#dll-generation">DLL generation</a></h1><p><strong>Note</strong>: The same rules apply to <tt class="docutils literal"><span class="pre"><span class="Identifier">lib</span><span class="Operator">*.</span><span class="Identifier">so</span></span></tt> shared object files on UNIX. For better readability only the DLL version is described here.</p>
- <p>Nim supports the generation of DLLs. However, there must be only one instance of the GC per process/address space. This instance is contained in <tt class="docutils literal"><span class="pre">nimrtl.dll</span></tt>. This means that every generated Nim DLL depends on <tt class="docutils literal"><span class="pre">nimrtl.dll</span></tt>. To generate the "nimrtl.dll" file, use the command:</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">-d:release</span> <span class="Identifier">lib/nimrtl.nim</span></pre></p>
- <p>To link against <tt class="docutils literal"><span class="pre">nimrtl.dll</span></tt> use the command:</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">-d:useNimRtl</span> <span class="Identifier">myprog.nim</span></pre></p>
- <h1><a class="toc-backref" id="additional-compilation-switches" href="#additional-compilation-switches">Additional compilation switches</a></h1><p>The standard library supports a growing number of <tt class="docutils literal"><span class="pre"><span class="Identifier">useX</span></span></tt> conditional defines affecting how some features are implemented. This section tries to give a complete list.</p>
- <table border="1" class="docutils"><tr><th>Define</th><th>Effect</th></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">release</span></span></tt></td><td>Turns on the optimizer. More aggressive optimizations are possible, e.g.: <tt class="docutils literal"><span class="pre option">--passC:-ffast-math</span></tt> (but see issue #10305)</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">danger</span></span></tt></td><td>Turns off all runtime checks and turns on the optimizer.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">useFork</span></span></tt></td><td>Makes <tt class="docutils literal"><span class="pre"><span class="Identifier">osproc</span></span></tt> use <tt class="docutils literal"><span class="pre"><span class="Identifier">fork</span></span></tt> instead of <tt class="docutils literal"><span class="pre"><span class="Identifier">posix_spawn</span></span></tt>.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">useNimRtl</span></span></tt></td><td>Compile and link against <tt class="docutils literal"><span class="pre">nimrtl.dll</span></tt>.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">useMalloc</span></span></tt></td><td>Makes Nim use C's <span id="malloc_1">malloc</span> instead of Nim's own memory manager, albeit prefixing each allocation with its size to support clearing memory on reallocation. This only works with <tt class="docutils literal"><span class="pre option">--mm:none</span></tt>, <tt class="docutils literal"><span class="pre option">--mm:arc</span></tt> and <tt class="docutils literal"><span class="pre option">--mm:orc</span></tt>.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">useRealtimeGC</span></span></tt></td><td>Enables support of Nim's GC for <em>soft</em> realtime systems. See the documentation of the <a class="reference external" href="refc.html">refc</a> for further information.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">logGC</span></span></tt></td><td>Enable GC logging to stdout.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">nodejs</span></span></tt></td><td>The JS target is actually <tt class="docutils literal"><span class="pre">node.js</span></tt>.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">ssl</span></span></tt></td><td>Enables OpenSSL support for the sockets module.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">memProfiler</span></span></tt></td><td>Enables memory profiling for the native GC.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">uClibc</span></span></tt></td><td>Use uClibc instead of libc. (Relevant for Unix-like OSes)</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">checkAbi</span></span></tt></td><td>When using types from C headers, add checks that compare what's in the Nim file with what's in the C header. This may become enabled by default in the future.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">tempDir</span></span></tt></td><td>This symbol takes a string as its value, like <tt class="docutils literal"><span class="pre option">--define:tempDir:/some/temp/path</span></tt> to override the temporary directory returned by <tt class="docutils literal"><span class="pre"><span class="Identifier">os</span><span class="Operator">.</span><span class="Identifier">getTempDir</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt>. The value <strong>should</strong> end with a directory separator character. (Relevant for the Android platform)</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">useShPath</span></span></tt></td><td>This symbol takes a string as its value, like <tt class="docutils literal"><span class="pre option">--define:useShPath:/opt/sh/bin/sh</span></tt> to override the path for the <tt class="docutils literal"><span class="pre"><span class="program">sh</span></span></tt> binary, in cases where it is not located in the default location <tt class="docutils literal"><span class="pre">/bin/sh</span></tt>.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">noSignalHandler</span></span></tt></td><td>Disable the crash handler from <tt class="docutils literal"><span class="pre">system.nim</span></tt>.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">globalSymbols</span></span></tt></td><td>Load all <tt class="docutils literal"><span class="pre"><span class="Punctuation">{</span><span class="Operator">.</span><span class="Identifier">dynlib</span><span class="Operator">.</span><span class="Punctuation">}</span></span></tt> libraries with the <tt class="docutils literal"><span class="pre"><span class="Identifier">RTLD_GLOBAL</span></span></tt> flag on Posix systems to resolve symbols in subsequently loaded libraries.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">lto</span></span></tt></td><td>Enable link-time optimization in the backend compiler and linker.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">lto_incremental</span></span></tt></td><td>Enable link-time optimization and additionally enable incremental linking for compilers that support it. Currently only clang and vcc.</td></tr>
- <tr><td><tt class="docutils literal"><span class="pre"><span class="Identifier">strip</span></span></tt></td><td>Strip debug symbols added by the backend compiler from the executable.</td></tr>
- </table>
- <h1><a class="toc-backref" id="additional-features" href="#additional-features">Additional Features</a></h1><p>This section describes Nim's additional features that are not listed in the Nim manual. Some of the features here only make sense for the C code generator and are subject to change.</p>
- <h2><a class="toc-backref" id="additional-features-linedir-option" href="#additional-features-linedir-option">LineDir option</a></h2><p>The <tt class="docutils literal"><span class="pre option">--lineDir</span></tt> option can be turned on or off. If turned on the generated C code contains <tt class="docutils literal"><span class="pre"><span class="Preprocessor">#line</span></span></tt> directives. This may be helpful for debugging with GDB.</p>
- <h2><a class="toc-backref" id="additional-features-stacktrace-option" href="#additional-features-stacktrace-option">StackTrace option</a></h2><p>If the <tt class="docutils literal"><span class="pre option">--stackTrace</span></tt> option is turned on, the generated C contains code to ensure that proper stack traces are given if the program crashes or some uncaught exception is raised.</p>
- <h2><a class="toc-backref" id="additional-features-linetrace-option" href="#additional-features-linetrace-option">LineTrace option</a></h2><p>The <tt class="docutils literal"><span class="pre option">--lineTrace</span></tt> option implies the <tt class="docutils literal"><span class="pre option">stackTrace</span></tt> option. If turned on, the generated C contains code to ensure that proper stack traces with line number information are given if the program crashes or an uncaught exception is raised.</p>
- <h1><a class="toc-backref" id="dynliboverride" href="#dynliboverride">DynlibOverride</a></h1><p>By default Nim's <tt class="docutils literal"><span class="pre"><span class="Identifier">dynlib</span></span></tt> pragma causes the compiler to generate <tt class="docutils literal"><span class="pre"><span class="Identifier">GetProcAddress</span></span></tt> (or their Unix counterparts) calls to bind to a DLL. With the <tt class="docutils literal"><span class="pre option">dynlibOverride</span></tt> command line switch this can be prevented and then via <tt class="docutils literal"><span class="pre option">--passL</span></tt> the static library can be linked against. For instance, to link statically against Lua this command might work on Linux:</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">--dynlibOverride:lua</span> <span class="Identifier">--passL:liblua.lib</span> <span class="Identifier">program.nim</span></pre></p>
- <h1><a class="toc-backref" id="backend-language-options" href="#backend-language-options">Backend language options</a></h1><p>The typical compiler usage involves using the <tt class="docutils literal"><span class="pre option">compile</span></tt> or <tt class="docutils literal"><span class="pre option">c</span></tt> command to transform a <tt class="docutils literal"><span class="pre">.nim</span></tt> file into one or more <tt class="docutils literal"><span class="pre">.c</span></tt> files which are then compiled with the platform's C compiler into a static binary. However, there are other commands to compile to C++, Objective-C, or JavaScript. More details can be read in the <a class="reference external" href="backends.html">Nim Backend Integration document</a>.</p>
- <h1><a class="toc-backref" id="nim-documentation-tools" href="#nim-documentation-tools">Nim documentation tools</a></h1><p>Nim provides the <span id="doc_1">doc</span> command to generate HTML documentation from <tt class="docutils literal"><span class="pre">.nim</span></tt> source files. Only exported symbols will appear in the output. For more details see <a class="reference external" href="docgen.html">the docgen documentation</a>.</p>
- <h1><a class="toc-backref" id="nim-idetools-integration" href="#nim-idetools-integration">Nim idetools integration</a></h1><p>Nim provides language integration with external IDEs through the idetools command. See the documentation of <a class="reference external" href="idetools.html">idetools</a> for further information.</p>
- <h1><a class="toc-backref" id="nim-for-embedded-systems" href="#nim-for-embedded-systems">Nim for embedded systems</a></h1><p>While the default Nim configuration is targeted for optimal performance on modern PC hardware and operating systems with ample memory, it is very well possible to run Nim code and a good part of the Nim standard libraries on small embedded microprocessors with only a few kilobytes of memory.</p>
- <p>A good start is to use the <tt class="docutils literal"><span class="pre"><span class="Identifier">any</span></span></tt> operating target together with the <tt class="docutils literal"><span class="pre"><span class="Identifier">malloc</span></span></tt> memory allocator and the <tt class="docutils literal"><span class="pre"><span class="Identifier">arc</span></span></tt> garbage collector. For example:</p>
- <p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">--os:any</span> <span class="option">--mm:arc</span> <span class="option">-d:useMalloc</span> <span class="Identifier">[...]</span> <span class="Identifier">x.nim</span></pre></p>
- <ul class="simple"><li><tt class="docutils literal"><span class="pre option">--mm:arc</span></tt> will enable the reference counting memory management instead of the default garbage collector. This enables Nim to use heap memory which is required for strings and seqs, for example.</li>
- <li>The <tt class="docutils literal"><span class="pre option">--os:any</span></tt> target makes sure Nim does not depend on any specific operating system primitives. Your platform should support only some basic ANSI C library <tt class="docutils literal"><span class="pre"><span class="Identifier">stdlib</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">stdio</span></span></tt> functions which should be available on almost any platform.</li>
- <li>The <tt class="docutils literal"><span class="pre option">-d:useMalloc</span></tt> option configures Nim to use only the standard C memory manage primitives <tt class="docutils literal"><span class="pre"><span class="Identifier">malloc</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">free</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">realloc</span><span class="Punctuation">(</span><span class="Punctuation">)</span></span></tt>.</li>
- </ul>
- <p>If your platform does not provide these functions it should be trivial to provide an implementation for them and link these to your program.</p>
- <p>For targets with very restricted memory, it might be beneficial to pass some additional flags to both the Nim compiler and the C compiler and/or linker to optimize the build for size. For example, the following flags can be used when targeting a gcc compiler:</p>
- <p><tt class="docutils literal"><span class="pre option">--opt:size -d:lto -d:strip</span></tt></p>
- <p>The <tt class="docutils literal"><span class="pre option">--opt:size</span></tt> flag instructs Nim to optimize code generation for small size (with the help of the C compiler), the <tt class="docutils literal"><span class="pre option">-d:lto</span></tt> flags enable link-time optimization in the compiler and linker, the <tt class="docutils literal"><span class="pre option">-d:strip</span></tt> strips debug symbols.</p>
- <p>Check the <a class="reference internal" href="#crossminuscompilation">Cross-compilation</a> section for instructions on how to compile the program for your target.</p>
- <h2><a class="toc-backref" id="nim-for-embedded-systems-nimallocpagesviamalloc" href="#nim-for-embedded-systems-nimallocpagesviamalloc">nimAllocPagesViaMalloc</a></h2><p>Nim's default allocator is based on TLSF, this algorithm was designed for embedded devices. This allocator gets blocks/pages of memory via a currently undocumented <tt class="docutils literal"><span class="pre"><span class="Identifier">osalloc</span></span></tt> API which usually uses POSIX's <tt class="docutils literal"><span class="pre"><span class="Identifier">mmap</span></span></tt> call. On many environments <tt class="docutils literal"><span class="pre"><span class="Identifier">mmap</span></span></tt> is not available but C's <tt class="docutils literal"><span class="pre"><span class="Identifier">malloc</span></span></tt> is. You can use the <tt class="docutils literal"><span class="pre"><span class="Identifier">nimAllocPagesViaMalloc</span></span></tt> define to use <tt class="docutils literal"><span class="pre"><span class="Identifier">malloc</span></span></tt> instead of <tt class="docutils literal"><span class="pre"><span class="Identifier">mmap</span></span></tt>. <tt class="docutils literal"><span class="pre"><span class="Identifier">nimAllocPagesViaMalloc</span></span></tt> is currently only supported with <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">mm</span><span class="Punctuation">:</span><span class="Identifier">arc</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">mm</span><span class="Punctuation">:</span><span class="Identifier">orc</span></span></tt>. (Since version 1.6)</p>
- <h1><a class="toc-backref" id="nimpage256-slash-nimpage512-slash-nimpage1k" href="#nimpage256-slash-nimpage512-slash-nimpage1k">nimPage256 / nimPage512 / nimPage1k</a></h1><p>Adjust the page size for Nim's GC allocator. This enables using <tt class="docutils literal"><span class="pre"><span class="Identifier">nimAllocPagesViaMalloc</span></span></tt> on devices with less RAM. The default page size requires too much RAM to work.</p>
- <p>Recommended settings:</p>
- <ul class="simple"><li>< 32 kB of RAM use <tt class="docutils literal"><span class="pre"><span class="Identifier">nimPage256</span></span></tt></li>
- <li>< 512 kB of RAM use <tt class="docutils literal"><span class="pre"><span class="Identifier">nimPage512</span></span></tt></li>
- <li>< 2 MB of RAM use <tt class="docutils literal"><span class="pre"><span class="Identifier">nimPage1k</span></span></tt></li>
- </ul>
- <p>Initial testing hasn't shown much difference between 512B or 1kB page sizes in terms of performance or latency. Using <tt class="docutils literal"><span class="pre"><span class="Identifier">nimPages256</span></span></tt> will limit the total amount of allocatable RAM.</p>
- <h1><a class="toc-backref" id="nimmemaligntiny" href="#nimmemaligntiny">nimMemAlignTiny</a></h1><p>Sets <tt class="docutils literal"><span class="pre"><span class="Identifier">MemAlign</span></span></tt> to <tt class="docutils literal"><span class="pre"><span class="DecNumber">4</span></span></tt> bytes which reduces the memory alignment to better match some embedded devices.</p>
- <h1><a class="toc-backref" id="thread-stack-size" href="#thread-stack-size">Thread stack size</a></h1><p>Nim's thread API provides a simple wrapper around more advanced RTOS task features. Customizing the stack size and stack guard size can be done by setting <tt class="docutils literal"><span class="pre"><span class="Operator">-</span><span class="Identifier">d</span><span class="Punctuation">:</span><span class="Identifier">nimThreadStackSize</span><span class="Operator">=</span><span class="DecNumber">16384</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Operator">-</span><span class="Identifier">d</span><span class="Punctuation">:</span><span class="Identifier">nimThreadStackGuard</span><span class="Operator">=</span><span class="DecNumber">32</span></span></tt>.</p>
- <p>Currently only Zephyr, NuttX and FreeRTOS support these configurations.</p>
- <h1><a class="toc-backref" id="nim-for-realtime-systems" href="#nim-for-realtime-systems">Nim for realtime systems</a></h1><p>See the <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">mm</span><span class="Punctuation">:</span><span class="Identifier">arc</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Operator">--</span><span class="Identifier">mm</span><span class="Punctuation">:</span><span class="Identifier">orc</span></span></tt> memory management settings in <a class="reference external" href="mm.html">MM</a> for further information.</p>
- <h1><a class="toc-backref" id="signal-handling-in-nim" href="#signal-handling-in-nim">Signal handling in Nim</a></h1><p>The Nim programming language has no concept of Posix's signal handling mechanisms. However, the standard library offers some rudimentary support for signal handling, in particular, segmentation faults are turned into fatal errors that produce a stack trace. This can be disabled with the <tt class="docutils literal"><span class="pre option">-d:noSignalHandler</span></tt> switch.</p>
- <h1><a class="toc-backref" id="optimizing-for-nim" href="#optimizing-for-nim">Optimizing for Nim</a></h1><p>Nim has no separate optimizer, but the C code that is produced is very efficient. Most C compilers have excellent optimizers, so usually it is not needed to optimize one's code. Nim has been designed to encourage efficient code: The most readable code in Nim is often the most efficient too.</p>
- <p>However, sometimes one has to optimize. Do it in the following order:</p>
- <ol class="simple"><li>switch off the embedded debugger (it is <strong>slow</strong>!)</li>
- <li>turn on the optimizer and turn off runtime checks</li>
- <li>profile your code to find where the bottlenecks are</li>
- <li>try to find a better algorithm</li>
- <li>do low-level optimizations</li>
- </ol>
- <p>This section can only help you with the last item.</p>
- <h2><a class="toc-backref" id="optimizing-for-nim-optimizing-string-handling" href="#optimizing-for-nim-optimizing-string-handling">Optimizing string handling</a></h2><p>String assignments are sometimes expensive in Nim: They are required to copy the whole string. However, the compiler is often smart enough to not copy strings. Due to the argument passing semantics, strings are never copied when passed to subroutines. The compiler does not copy strings that are a result of a procedure call, because the callee returns a new string anyway. Thus it is efficient to do:</p>
- <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Identifier">procA</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Comment"># assignment will not copy the string; procA allocates a new</span>
- <span class="Comment"># string already</span></pre></p>
- <p>However, it is not efficient to do:</p>
- <p><pre class="listing"><span class="Keyword">var</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Identifier">varA</span> <span class="Comment"># assignment has to copy the whole string into a new buffer!</span></pre></p>
- <p>For <tt class="docutils literal"><span class="pre"><span class="Keyword">let</span></span></tt> symbols a copy is not always necessary:</p>
- <p><pre class="listing"><span class="Keyword">let</span> <span class="Identifier">s</span> <span class="Operator">=</span> <span class="Identifier">varA</span> <span class="Comment"># may only copy a pointer if it safe to do so</span></pre></p>
- <p>The compiler optimizes string case statements: A hashing scheme is used for them if several different string constants are used. So code like this is reasonably efficient:</p>
- <p><pre class="listing"><span class="Keyword">case</span> <span class="Identifier">normalize</span><span class="Punctuation">(</span><span class="Identifier">k</span><span class="Operator">.</span><span class="Identifier">key</span><span class="Punctuation">)</span>
- <span class="Keyword">of</span> <span class="StringLit">"name"</span><span class="Punctuation">:</span> <span class="Identifier">c</span><span class="Operator">.</span><span class="Identifier">name</span> <span class="Operator">=</span> <span class="Identifier">v</span>
- <span class="Keyword">of</span> <span class="StringLit">"displayname"</span><span class="Punctuation">:</span> <span class="Identifier">c</span><span class="Operator">.</span><span class="Identifier">displayName</span> <span class="Operator">=</span> <span class="Identifier">v</span>
- <span class="Keyword">of</span> <span class="StringLit">"version"</span><span class="Punctuation">:</span> <span class="Identifier">c</span><span class="Operator">.</span><span class="Identifier">version</span> <span class="Operator">=</span> <span class="Identifier">v</span>
- <span class="Keyword">of</span> <span class="StringLit">"os"</span><span class="Punctuation">:</span> <span class="Identifier">c</span><span class="Operator">.</span><span class="Identifier">oses</span> <span class="Operator">=</span> <span class="Identifier">split</span><span class="Punctuation">(</span><span class="Identifier">v</span><span class="Punctuation">,</span> <span class="Punctuation">{</span><span class="Punctuation">'</span><span class="Punctuation">;</span><span class="Punctuation">'</span><span class="Punctuation">}</span><span class="Punctuation">)</span>
- <span class="Keyword">of</span> <span class="StringLit">"cpu"</span><span class="Punctuation">:</span> <span class="Identifier">c</span><span class="Operator">.</span><span class="Identifier">cpus</span> <span class="Operator">=</span> <span class="Identifier">split</span><span class="Punctuation">(</span><span class="Identifier">v</span><span class="Punctuation">,</span> <span class="Punctuation">{</span><span class="Punctuation">'</span><span class="Punctuation">;</span><span class="Punctuation">'</span><span class="Punctuation">}</span><span class="Punctuation">)</span>
- <span class="Keyword">of</span> <span class="StringLit">"authors"</span><span class="Punctuation">:</span> <span class="Identifier">c</span><span class="Operator">.</span><span class="Identifier">authors</span> <span class="Operator">=</span> <span class="Identifier">split</span><span class="Punctuation">(</span><span class="Identifier">v</span><span class="Punctuation">,</span> <span class="Punctuation">{</span><span class="Punctuation">'</span><span class="Punctuation">;</span><span class="Punctuation">'</span><span class="Punctuation">}</span><span class="Punctuation">)</span>
- <span class="Keyword">of</span> <span class="StringLit">"description"</span><span class="Punctuation">:</span> <span class="Identifier">c</span><span class="Operator">.</span><span class="Identifier">description</span> <span class="Operator">=</span> <span class="Identifier">v</span>
- <span class="Keyword">of</span> <span class="StringLit">"app"</span><span class="Punctuation">:</span>
- <span class="Keyword">case</span> <span class="Identifier">normalize</span><span class="Punctuation">(</span><span class="Identifier">v</span><span class="Punctuation">)</span>
- <span class="Keyword">of</span> <span class="StringLit">"console"</span><span class="Punctuation">:</span> <span class="Identifier">c</span><span class="Operator">.</span><span class="Identifier">app</span> <span class="Operator">=</span> <span class="Identifier">appConsole</span>
- <span class="Keyword">of</span> <span class="StringLit">"gui"</span><span class="Punctuation">:</span> <span class="Identifier">c</span><span class="Operator">.</span><span class="Identifier">app</span> <span class="Operator">=</span> <span class="Identifier">appGUI</span>
- <span class="Keyword">else</span><span class="Punctuation">:</span> <span class="Identifier">quit</span><span class="Punctuation">(</span><span class="Identifier">errorStr</span><span class="Punctuation">(</span><span class="Identifier">p</span><span class="Punctuation">,</span> <span class="StringLit">"expected: console or gui"</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
- <span class="Keyword">of</span> <span class="StringLit">"license"</span><span class="Punctuation">:</span> <span class="Identifier">c</span><span class="Operator">.</span><span class="Identifier">license</span> <span class="Operator">=</span> <span class="Identifier">UnixToNativePath</span><span class="Punctuation">(</span><span class="Identifier">k</span><span class="Operator">.</span><span class="Identifier">value</span><span class="Punctuation">)</span>
- <span class="Keyword">else</span><span class="Punctuation">:</span> <span class="Identifier">quit</span><span class="Punctuation">(</span><span class="Identifier">errorStr</span><span class="Punctuation">(</span><span class="Identifier">p</span><span class="Punctuation">,</span> <span class="StringLit">"unknown variable: "</span> <span class="Operator">&</span> <span class="Identifier">k</span><span class="Operator">.</span><span class="Identifier">key</span><span class="Punctuation">)</span><span class="Punctuation">)</span></pre></p>
- </p>
-
- </div>
- </div>
- <div class="twelve-columns footer">
- <span class="nim-sprite"></span>
- <br>
- <small style="color: var(--hint);">Made with Nim. Generated: 2025-01-09 11:59:35 UTC</small>
- </div>
- </div>
- </div>
- <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
-
- </body>
- </html>
|