Environment variables
Passing environment variables to your application
Cheerp has support for environment variables.
Providing environment variables
The ways to provide environment variables depend on which target you are compiling to.
JavaScript modules
For JavaScript modules, you can pass environment variables through the initialization function like this:
WASI-CLI
When targeting WASI, Cheerp uses the WASI-CLI interface to recieve environment variables, meaning that it will work on any runtime that has implemented the WASI-CLI proposal.
The exact flag differs between runtimes. With Wasmtime, itβs --env
:
Other
When targeting something else, you can provide environment variables and program arguments by defining a global variable called CHEERP_ENV
:
Changes to CHEERP_ENV
after main
has run will not be reflected in the environment.
Reading environment variables
In C/C++, environment variables can be read as usual, for example using getenv
.
Nonstandard third main
parameter
Alternatively, Cheerp supports this nonstandard main
signature:
Limitations
It is not possible to receive environment variables in main
when the main
function is in a different section than the target.
In these cases, you can still get environment variables with the getenv
function.