Hi all -- I'm using Nim 0.20.2 on Ubuntu 16.04 and reading this page: https://nim-lang.org/docs/backends.html
I'm looking at the code to statically generate a library.
$ nim c --app:staticLib --noMain --header fib.nim
$ gcc -o m -Inimcache -Ipath/to/nim/lib libfib.nim.a maths.c
The first command does not produce a file called libfib.nim.a as shown, which was my first problem.
It does produce the header file fib.h in the nimcache and a binary ar output fib in the current directory, so I tried renaming fib to libfib.nim.a and proceeding. Then I got a linker error:
$ gcc -o m -I$HOME/.cache/nim/fib_d/ -I$HOME/.choosenim/toolchains/nim-0.20.2/lib/ libfib.nim.a maths.c
/tmp/ccm37fDt.o: In function `main':
maths.c:(.text+0x9): undefined reference to `NimMain'
maths.c:(.text+0x1c): undefined reference to `fib'
collect2: error: ld returned 1 exit status
Both fib.nim and maths.c are copy/pasted from the example.
Anyone have more experience doing these type of builds? Thanks!
Thanks for your reply! could you explain in a little more detail how that repository answers the question?
I'm trying to compile Nim code to a static library and import it into C. I'm not trying to import C code into Nim, which is what all the examples in that repo seem to do.
I tried reverting to Nim 0.11.0, and the libfib.nim.a file was now produced when I ran compilation. I guess the documentation might be pretty out of date in spots. However, I still had the same linker errors.
I am able to compile things just fine by passing in the C files directly and not producing an intermediate library, as in the other example:
$ nim c --noMain --noLinking --header:fib.h fib.nim
$ gcc -o m -I$HOME/.cache/nim/fib_d -Ipath/to/nim/lib $HOME/.cache/nim/fib_d/*.c maths.c
That works for me. I might stick to this for now if I can.