QuickJS: creating a shared c module and import it as a module in js
- After a day exploration and a lot of frustration, finaly some clarity found thanks to https://github.com/khanhas/minnet-quickjs
- the first few lines of your .c/.h files are very important !
#define countof(x) (sizeof(x) / sizeof((x)[0]))
=> this one should be copy & pasted, or inclued- the JS_INIT_MOULE is also critical as quickjs relies on that ! typos are critical (spent an hour on a typo ...)
the only (and simplest) example of a makefile I found was https://github.com/khanhas/minnet-quickjs/blob/master/Makefile
- critical flags (pulled my hair out over them !)
- -fPIC
- -shared
- -DJS_SHARED_LIBRARY * clang & gcc both work with those flags
- all this got the shared library working for me under Ubutu 20.04
if all went well , (and you are in your compilation/ code folder)
just run (asuming you have qjs installed at the system level)
qjs ./index.js
Voila ! if it went wrong, quickjs will likely complain about not
- being able to load a shared libary , which is a bit of a misdirection sometimes : even if your .so file does not exist at all you will get the same message !
- JS_INIT_MODULE not being found : it means something sorta worked, but you either have some typo in the crucial naming parts of your source file, or some less obvious compilation issue (ie, no errors, .so file is generated, but it still does not work)