Hi everyone,
I know that in Nim is possible to call C functions compiled to a shared library, but I was wondering if it could be possible to compile a C file with a set of functions directly with the Nim source, without having to compile the C file to a shared library first. If this is possible, does anyone have a simple example to showcase?
Thanks
c file left as exercise for reader.
{.compile: "only_adds_ints.c".}
proc add_int(a, b: cint): cint
{.importc: "add_int".}
echo add_int(1, 2)
#[
alternatively using emit
{.emit: """
/*INCLUDESECTION*/ // if you want this placed near the top of the generated code
#include "only_adds_ints.c"
""".}
]#
https://nim-lang.github.io/Nim/manual.html#implementation-specific-pragmas-compile-pragma
https://nim-lang.github.io/Nim/manual.html#implementation-specific-pragmas-emit-pragma
If you include C code directly in a Nim repo, you may eventually want to use your new Nim library from other Nim code. In that case, you have to to pass compiler flags in a special way, since the Nim compiler (unfortunately) does not do this for you automatically.