Compiling the Nim program on the OpenWRT router across platforms on Linux

Nim  is a very fun language, let's see how to run a program written by Nim on a router.

Ready to work

  • Install the Nim compiler, if you use Debian GNU/Linux, apt install nimyou can
  • A mipsel OpenWRT router

Write a Nim program

Come to Hello World:
echo "hello world"

Prepare OpenWRT's build toolchain

aria2c 'http://archive.openwrt.org/chaos_calmer/15.05.1/ramips/mt7621/OpenWrt-SDK-15.05.1-ramips-mt7621_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64.tar.bz2'
tar xvf OpenWrt-SDK-15.05.1-ramips-mt7621_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64.tar.bz2

Configure the PATH environment variable to let the Nim compiler find the build toolchain

export PATH=/dev/shm/OpenWrt-SDK-15.05.1-ramips-mt7621_gcc-4.8-linaro_uClibc-0.9.33.2.Linux-x86_64/staging_dir/toolchain-mipsel_1004kc+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/:$PATH

Start compiling!

nim -d:release --opt=size -d:ssl --cpu:mipsel --os:linux --gcc.exe:mipsel-openwrt-linux-gcc --gcc.linkerexe:mipsel-openwrt-linux-gcc  c  helloworld.nim
After compiling on my machine, 187KB, compared to the dozens of MB compiled by Golang, it is too light,
If you want to be small, you can upxcompress it with an artifact:
upx -9 --best --lzma helloworld
This is only 70KB, for a router with only a few MB of ROM, it is a good news.

评论

此博客中的热门博文

将博客部署到星际文件系统(IPFS)

高中地理必修一知识点总结

一场CF的台前幕后(下)