8

Github GitHub - Yardanico/cosmonim: A simple example to show how cosmopolitan li...

 3 years ago
source link: https://github.com/Yardanico/cosmonim
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Cosmopolitan Libc for Nim

This is a simple example to show how can you use Cosmopolitan with Nim. Directory structure:

  • stubs - contains empty include files that Nim expects to be available. Cosmopolitan provides all of these in a single include file.

  • hello.nim - Nim file that we want to compile

  • nim.cfg - Neccessary configuration file to set all the C compiler options to the ones required by Cosmopolitan.

First you need to get the Cosmopolitan itself - simply go to the downloads and grab the latest release (0.2 as of 03-03-2021). Then extract it into the cosmopolitan folder so that it looks something like this:

cosmopolitan/
├── ape.lds
├── ape.o
├── cosmopolitan.a
├── cosmopolitan.h
└── crt.o

Now you can actually compile the first example with:

# Compile an ELF binary
nim c -d:danger --opt:size -o:hello.elf hello.nim

# Get the actual portable executable
objcopy -SO binary hello.elf hello.com

If you import some other stdlib modules and the compilation fails, first check if the C compiler is complaining about missing headers - if so, just add that header into the stubs directory (just create an empty file with the right directory hierarchy), and it'll probably work :P

Apart from hello.nim, this repo also has gethttp.nim and asyncserv.nim. To compile them you need to apply the asyncserv.diff patch to your local Nim installation. This is also pretty simple:

cd nimGitDir
git apply /path/to/cosmonim/asyncserv.diff 

Then you can compile those examples as usual.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK