Skip to content

Instantly share code, notes, and snippets.

Last active Aug 5, 2020
What would you like to do?
Trivial Janet project with a bundled native C module

Blog 2020/7/29

<- previous | index | next ->

Trivial Janet project with a bundled native C module

This is the simplest example of how to write a Janet project which is a mixture of C and Janet, which results in a single, statically-linked executable.

Run make to build and test the executable build/baz.


foo.c is a Janet native C module which implements a namespace foo, which contains one function bar, which returns the number 42.

To build and test this module, run make test-foo. You can also start a interactive REPL with this module imported via make repl-foo (type (foo/bar) at the prompt).

baz.janet is a Janet program which imports the foo module, then prints foo/bar multiplied by 2.

To build and test the build/baz statically-linked executable, run make test-baz, or simply make.

(import build/foo :as foo)
(defn main [& args]
(print (* 2 (foo/bar))))
// Trivial Janet native module which contains one function which returns 42.
#include <janet.h>
static Janet cfun_foo_bar(int32_t argc, Janet *argv) {
return janet_wrap_number(42);
static const JanetReg cfuns[] = {
"bar", cfun_foo_bar,
"(foo/bar)\n\nReturns 42."
JANET_MODULE_ENTRY(JanetTable *env) {
janet_cfuns(env, "foo", cfuns);
default: test-foo test-baz
build/foo.a: foo.c
jpm --verbose build
test-foo: build/foo.a
test 42 = $(shell janet -e '(import build/foo :as foo) (print (foo/bar))')
repl-foo: build/foo.a
janet -e '(import build/foo :as foo)' -r
build/baz: build/foo.a baz.janet
jpm --verbose build
test-baz: build/baz
test 84 = $(shell ./build/baz)
rm -rf build
.PHONY: default test-foo repl-foo test-baz clean
:name "baz")
:name "foo"
:source @["foo.c"]
:cflags [;default-cflags "-Wno-unused-parameter"])
:name "baz"
:entry "baz.janet")
Copy link

pyrmont commented Jul 30, 2020

This is really helpful. Thanks for sharing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment