Skip to content
/ cosmonim Public

A simple example to show how cosmopolitan libc can be used with Nim

License

Notifications You must be signed in to change notification settings

rfesi/cosmonim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

A simple example to show how cosmopolitan libc can be used with Nim

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages