# nim c -f --cc:zig --zig.options.linker:"-target arm-linux-musleabihf" --zig.options.always:"-target arm-linux-musleabihf" hello.nim
command line(1, 2) Error: unknown C compiler: 'zig'. Available options are: gcc, switch_gcc, llvm_gcc, clang, bcc, vcc, tcc, env, icl, icc, clang_cl
# nim --version
Nim Compiler Version 1.3.5 [Linux: amd64]
Compiled at 2020-05-20
Copyright (c) 2006-2020 by Andreas Rumpf
git hash: 6969a468ceaab7384d7448bfd88e47a5b24c3a97
active boot switches: -d:release
# zig version
0.6.0
# cat hello.nim
echo "Hello World"
You forgot to provide architecture for code generation.
Please add --cpu:arm just after nim command:
nim --cpu:arm --cc:zig --zig.options.linker: "-target arm-linux-musleabihf" --zig.options.always: "-target arm-linux-musleabihf" c ./hello.nim
# nim --cpu:arm --cc:zig --zig.options.linker: "-target arm-linux-musleabihf" --zig.options.always: "-target arm-linux-musleabihf" c ./hello.nim
command line(1, 2) Error: unknown C compiler: 'zig'. Available options are: gcc, switch_gcc, llvm_gcc, clang, bcc, vcc, tcc, env, icl, icc, clang_cl
still raise error
# nim c --cpu:arm --cc:clang --clang.exe=zig --clang.linkerexe=zig hello.nim
Hint: used config file '/root/nim/config/nim.cfg' [Conf]
Hint: used config file '/root/nim/config/config.nims' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: hello [Processing]
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: hello.nim
Invalid argument: -c
See `zig --help` for detailed usage information
Invalid argument: -c
See `zig --help` for detailed usage information
Invalid argument: -c
See `zig --help` for detailed usage information
Error: execution of an external compiler program 'zig -c -w -I/root/nim/lib -I/root/gencfg_arm -o /root/.cache/nim/hello_d/stdlib_io.nim.c.o /root/.cache/nim/hello_d/stdlib_io.nim.c' failed with exit code: 1
still error
# nim c -d:release --opt=size --passL:'-s -static -target arm' --cc:clang --clang.exe=clang --clang.linkerexe=clang hello.nim
Hint: used config file '/root/nim/config/nim.cfg' [Conf]
Hint: used config file '/root/nim/config/config.nims' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: hello [Processing]
Hint: [Link]
Hint: 37816 LOC; 0.215 sec; 49.371MiB peakmem; Release build; proj: /root/gencfg_arm/hello.nim; out: /root/gencfg_arm/hello [SuccessX]
# file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, BuildID[sha1]=36900acac886a71566ca020d8e766a22ba6ba154, for GNU/Linux 3.2.0, stripped
Why is x86-64 arch? not arm?
# nim c -d:danger --opt:size --cpu:arm --cc:clang --clang.exe="zigcc" --clang.linkerexe="zigcc" --passC:"-O2 -target mipsel-linux-musl" --passL:"-O2 -target mipsel-linux-musl" gencfg.nim
Hint: used config file '/root/nim/config/nim.cfg' [Conf]
Hint: used config file '/root/nim/config/config.nims' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: gencfg [Processing]
Hint: os [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: bitops [Processing]
Hint: macros [Processing]
Hint: algorithm [Processing]
Hint: unicode [Processing]
Hint: pathnorm [Processing]
Hint: osseps [Processing]
Hint: posix [Processing]
Hint: times [Processing]
Hint: options [Processing]
Hint: typetraits [Processing]
Hint: streams [Processing]
Hint: [Link]
Hint: 61796 LOC; 0.435 sec; 76.281MiB peakmem; Dangerous Release build; proj: /root/gencfg_arm/gencfg.nim; out: /root/gencfg_arm/gencfg [SuccessX]
and then I transfer the gencfg program to my mipsel arch router:
# ./gencfg 7.3.3.4
SIGILL: Illegal operation.
still errorYou are completely correct, I am so sloppy ^_^ ^_^
# nim c -d:danger --opt:size --cpu:mipsel --cc:clang --clang.exe="zigcc" --clang.linkerexe="zigcc" --passC:"-target mipsel-linux-musl" --passL:"-target mipsel-linux-musl" gencfg.nim
Hint: used config file '/root/nim/config/nim.cfg' [Conf]
Hint: used config file '/root/nim/config/config.nims' [Conf]
Hint: system [Processing]
Hint: widestrs [Processing]
Hint: io [Processing]
Hint: gencfg [Processing]
Hint: os [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: bitops [Processing]
Hint: macros [Processing]
Hint: algorithm [Processing]
Hint: unicode [Processing]
Hint: pathnorm [Processing]
Hint: osseps [Processing]
Hint: posix [Processing]
Hint: times [Processing]
Hint: options [Processing]
Hint: typetraits [Processing]
Hint: streams [Processing]
CC: stdlib_dollars.nim
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: stdlib_strutils.nim
CC: stdlib_pathnorm.nim
CC: stdlib_times.nim
CC: stdlib_os.nim
CC: stdlib_streams.nim
CC: gencfg.nim
Hint: [Link]
Hint: 61796 LOC; 2.123 sec; 76.289MiB peakmem; Dangerous Release build; proj: /root/gencfg_arm/gencfg.nim; out: /root/gencfg_arm/gencfg [SuccessX]
# ls -hl gencfg
-rwxr-xr-x 1 root root 157K May 22 17:03 gencfg
# file gencfg
gencfg: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1 (SYSV), statically linked, not stripped
# ldd gencfg
not a dynamic executable
--cpu:mipsel
not --cpu:arm
execute OK on my mipsel arch router ^_^ ^_^
Is there a way to strip the size when compiling?
"undefined behaviour" in Nim's default GC is "by design" too - https://github.com/nim-lang/Nim/blob/devel/lib/system/gc_common.nim#L383
/home/dian/.nim/lib/system/gc_common.nim:387:50: runtime error: load of misaligned address 0x7ffc26aede64 for type 'void *', which requires 8 byte alignment
0x7ffc26aede64: note: pointer points here
00 00 00 00 00 00 00 00 37 90 03 99 9d e6 2d 7b c0 44 40 00 00 00 00 00 d0 eb ae 26 fc 7f 00 00
^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/dian/.nim/lib/system/gc_common.nim:387:50