title: How to call a Nim function from Rust through C-FFI?
body: <p><a href="https://nim-lang.org/docs/backends.html" rel="noreferrer">Nim backend integration guide</a> describes how to call a Nim function from C.</p>
  
  <p>Example function:</p>
  
  <pre><code>proc fib(a: cint): cint {.exportc.} =
    if a &lt;= 2:
      result = 1
    else:
      result = fib(a - 1) + fib(a - 2)
  </code></pre>
  
  <p>The procedure requires that the Nim compiler is instructed not to create a <code>main</code> function, avoid linking and creating a header file to FFI from:</p>
  
  <p><code>$ nim c --noMain --noLinking --header:fib.h fib.nim</code></p>
  
  <p>To be able to use that function, the C main has to invoke a function called <code>NimMain()</code> as below:</p>
  
  <pre><code>#include "fib.h"
  #include &lt;stdio.h&gt;
  
  int main(void)
  {
    NimMain();
    for (int f = 0; f &lt; 10; f++)
      printf("Fib of %d is %d\n", f, fib(f));
    return 0;
  }
  </code></pre>
  
  <p>The previously mentioned generated header file is placed in the <code>nimcache</code> directory. The C compiler has to be instructed to compile all the files under the generated <code>nimcache</code> sub-directory, <code>nimbase.h</code> and <code>main.c</code>:</p>
  
  <p><code>$ gcc -o m -I$HOME/.cache/nim/fib_d -Ipath/to/nim/lib $HOME/.cache/nim/fib_d/*.c maths.c</code></p>
  
  <p>How can I instruct the rust compiler to look for those translation units under <code>nimcache</code>?</p>