WCPP
A node module to make writing C/C++ in JavaScript not painful.
Using the power of WebAssebly, wcpp projects run both in node and on the web.
npm i -g wcpp
Installing Emscripten
WCPP comes with an Emscripten installer and will automatically source environment variables upon compile.
$ wcpp-install
A C++ File
// addTwo.cpp export int
We need export
to tell our compiler to make this function available to JavaScript.
To compile all of our C/C++ files to wasm, we enter our project root and run:
$ wcpp
The first time running this command will be a bit slow.
You should see a list of C/C++ files that have been compiled.
Our JavaScript
All we have to do now is require our C++ file the same way we would require a JS file.
const ourModule = console
If you want to both use wcpp in the web and inculde a lare C++ file, you'll need to use it asynchronously.
We could put this in an anonymous async function:
;async { // Require our module const addTwo = await './addTwo.cpp' console}
Or use it as a promise:
'./addTwo.cpp'
Use Functions as Modules
We can make our function into a module by naming the function module
// C++ export int export int
// JavaScript const addTwo = await consoleconsole // Our other function