-3

I heard that if you run Nim to generate C-code:
nim c -d: release try1.nim
Then the further generated C code can be slipped into any compiler on any operating system.

In the nimcache folder, the following is generated:
@ mtry1.nim.c
stdlib_io.nim.c
stdlib_system.nim.c
try1.json

What to do next with this for compilation?

2 Answers2

5

You might want to try and run nim c -d:release --genScript try1.nim. This will generate a compile_try1 script in the nimcache folder that should be able to compile the C sources generated by Nim.

PMunch
  • 1,525
  • 11
  • 20
  • Alternatively you can invoke the compiler using `nim c --verbosity:3 try1.nim` and the Nim compiler will log whatever command it's executing to compile your code. The final option I'm aware of is opening the generated source file(s) in the nimcache directory, as the compilation command is also included in a comment at the top of the file. – Zachary Carter Jun 02 '21 at 10:50
  • This "script" allow me compiling only on current OS? or i can use, for example, `mipsel-linux-gnu-gcc-6 -static -o 123` and it will working? – Dub Awakener Jun 02 '21 at 10:50
  • 2
    If you set the compiler flags appropriately, it will allow you to compile the generated code using whatever toolchain you want. See this section of the compiler manual: https://nim-lang.org/docs/nimc.html#compiler-selection – Zachary Carter Jun 02 '21 at 10:52
  • we want use `Nim` for generating C-code, whichone we will compiling how we want, Idea was that. Im not sure what i understand all sence of this . . . – Dub Awakener Jun 02 '21 at 10:54
  • i expected next thing: -> i wrote some code on Nim -> translate it into C-code -> compiling C code everythere i want (crosstoolng and others, cause even `go` have not all OS nessessary for me :/ ) And thats all... is it possible? – Dub Awakener Jun 02 '21 at 10:58
  • @DubAwakener I'm having some trouble understanding you, so please accept my apologies. Yes Nim can compile to C and then you can use whatever compiler, linker, etc.. you want to. So you could compile for Android on Windows for instance, assuming you have the correct compiler toolchain installed. Nim's compiler though needs to be told what compiler, linker, etc.. you want to use prior to starting compilation, for obvious reasons. You might also want to checkout `zig cc` if you're doing a lot of cross compilation. https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html – Zachary Carter Jun 02 '21 at 13:28
  • @ZacharyCarter big tnx for ur answers! I need read more books about compiling >< Posted answer on my question. – Dub Awakener Jun 02 '21 at 13:38
0
  1. nim c --cpu:mips --os:linux --compileOnly --genScript [name_project].nim

  2. Copy everything from nimcache DIR to ur OS (which have ur toolchain (ecc, crosstoolng and etc.))

  3. Edit script compile_[name_project].sh with necessary gcc lib

for example, mipsel-linux-gnu-gcc-9 script (If u haven't it try on Ubuntu sudo apt install gcc-9-multilib-mipsel-linux-gnu):

mipsel-linux-gnu-gcc-9 -c -w -fmax-errors=3 -IC:\nim\lib -IC:\nim\nim_practice -o stdlib_io.nim.c.o stdlib_io.nim.c
mipsel-linux-gnu-gcc-9 -c -w -fmax-errors=3 -IC:\nim\lib -IC:\nim\nim_practice -o stdlib_system.nim.c.o stdlib_system.nim.c
mipsel-linux-gnu-gcc-9 -c -w -fmax-errors=3 -IC:\nim\lib -IC:\nim\nim_practice -o @m[name_projet].nim.c.o @m[name_projet].nim.c
mipsel-linux-gnu-gcc-9 -static -o3 -Wall -fPIC -o [name_projet] stdlib_io.nim.c.o stdlib_system.nim.c.o @m[name_projet].nim.c.o -ldl
  • you can specify the nimcache directory by passing `--nimcache:/path/to/dir` relative paths also work, i.e. `--nimcache:.` outputs the files in the current directory – shirleyquirk Jun 02 '21 at 18:00
  • also, you don't need to do this to cross-compile, you can specify the compiler to use and get nim to do it for you – shirleyquirk Jun 02 '21 at 18:36