Using zig-compiler to compile a nim-file or chicken-scheme-file to an executable

The purpose is to demonstrate that you can use zig to compile a nim-file or chicken-scheme-file to an executable.
Start by installing nim,zig,chicken-scheme:
Code:
pkg install nim
pkg install zig
pkg install chicken5

Create a file written in nim language named "hello.nim" with contents:
Code:
echo "hello world"

Create a nim configuration file /home/myuser/.config/nim/nim.cfg with contents:
Code:
--opt:none
--verbosity:3
--genScript
--cpu:amd64
--os:FreeBSD
--forceBuild
--backend:c
--cc:gcc
Adapt as needed.

Compile the "hello.nim" file with the script
Code:
export CC=gcc
nim --nimcache:. compileToC hello.nim
mv @mhello.nim.c mhello.nim.c
zig cc -c -o           stdlib_io.nim.o     stdlib_io.nim.c -I/usr/local/lib/nim 
zig cc -c -o       stdlib_system.nim.o stdlib_system.nim.c -I/usr/local/lib/nim 
zig cc -c -o              mhello.nim.o        mhello.nim.c -I/usr/local/lib/nim  
zig cc    -o                     hello     stdlib_io.nim.o stdlib_system.nim.o mhello.nim.o
This script will compile hello.nim first to c using the nim-compiler then compile this to o and link using the zig-compiler.
An executable ./hello is build

-------------------------------------------------------------------------------------
Idem for Chicken-scheme
Create a "helloworld.scm" Chicken-scheme file with contents
Code:
(print "Hello, World!")

Compile this program with the following script
Code:
chicken5 helloworld.scm
zig cc -c -o helloworld.o helloworld.c -DHAVE_CHICKEN_CONFIG_H -DC_ENABLE_PTABLES -I/usr/local/include/chicken5
zig cc    -o helloworld   helloworld.o -lchicken5 -lm -lpthread
This script will compile helloworld.scm with the chicken compiler to c and then compile this to o and link using the zig-compiler.
An executable ./helloworld is build

Running "zig targets" will list all possible compilation targets, i.e. other cpus/architectures
 
Back
Top