Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nim cross compilation to C

I wrote a Nim program,

echo("Hello.")

And then I tried to cross compile for a Linux machine,

nim c --cpu:i386 --os:linux -c hello.nim

This produced the following output:

config/nim.cfg(45, 2) Hint: added path: '/Users/connor/.babel/pkgs/' [Path]
config/nim.cfg(46, 2) Hint: added path: '/Users/connor/.nimble/pkgs/' [Path]
Hint: used config file '/usr/local/lib/nim-0.10.2/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: hello [Processing]
Hint: operation successful (8753 lines compiled; 0.140 sec total; 14.148MB)[SuccessX]

At this point I changed into the nimcache/ directory and tried to execute:

gcc hello.c -o hello.o

But that gave me an error:

hello.c:5:10: fatal error: 'nimbase.h' file not found
#include "nimbase.h"
         ^
1 error generated.

I thought, "no biggie, I'll just find nimbase.h and drop it in the nimcache directory there," but after that I got a new error,

In file included from hello.c:5:
./nimbase.h:385:28: error: 'assert_numbits' declared as an array with a
      negative size
  ...sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8 ? 1 : -1];
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

I'm not sure what I'm supposed to do with that. I had tried to use the --genScript option, but that resulted in similar errors. I'm running OS X Yosemite.

Thanks!

Update:

I wasn't sure how many architectures were supported for the --cpu: option, but I found a (partial?) list on the What makes Nim practical blog post. I ended up calling,

nim c --cpu:amd64 --os:linux -c hello.nim

This prevented the error I saw when compiling on my Linux box. If you're using Linux or OS X not sure what your CPU architecture is you can call,

less /proc/cpuinfo
like image 477
cjohnson318 Avatar asked Apr 29 '15 06:04

cjohnson318


People also ask

Does Nim compile to C?

The Nim compiler supports mainly two backend families: the C, C++ and Objective-C targets and the JavaScript target. The C like targets creates source files that can be compiled into a library or a final executable. The JavaScript target can generate a .

What does Nim compile to?

Some languages may have multiple implementations, but Nim's only implementation is a compiler. The compiler compiles Nim source code by first translating the code to another programming language, C, and then passing that C source code to a C compiler, which then compiles it into a binary executable.

Does Nim use LLVM?

Introduction. nlvm (the nim-level virtual machine?) is an LLVM-based compiler for the Nim language. From Nim's point of view, it's a backend just like C or JavaScript - from LLVM's point of view, it's a language frontend that emits IR.

How do I run a Nim code?

To see all compiler options, type nim --help in your terminal. If you're using VSCode with the Code Runner extension mentioned before, you'll just have to press Ctrl+Alt+N and your file will be compiled and run.


1 Answers

The last problem is because you're running gcc for x86_64 arch, while the sources were generated for i386 arch.

like image 174
uran Avatar answered Sep 30 '22 19:09

uran