EMCC(1)

🌸 EMCC(1) β€” User Commands 🌸

Oh hey bestie! Welcome to the main character manual page for emcc. πŸ’…

✨ NAME ✨

emcc β€” emscripten compiler for WASM and JavaScript like gcc or clang

πŸ’– DESCRIPTION πŸ’–

Emscripten Compiler Frontend (emcc) ***

The Emscripten Compiler Frontend (aka emcc) is what you call from the command line to, like, use the Emscripten compiler. It's basically a drop-in replacement for a standard compiler like gcc or clang. periodt. πŸ”₯

πŸŽ€ Command line syntax πŸŽ€

emcc [options] file...

(Note: you'll need ./emcc if you want to run emcc from your current directory. just so you know 🫢)

The input file(s) can be either source code files that Clang can handle (C or C++), object files (produced by emcc -c), or LLVM assembly files. ✨

πŸ¦‹ Arguments πŸ¦‹

Most clang options will work, and gcc options too β€” for example:

# Display this information emcc --help

# Display compiler version information emcc --version

To see the full list of Clang options supported on the version of Clang used by Emscripten, run clang --help.

Options that are modified or new in emcc are listed below. get ready, there's a lot πŸ’–

  • -O0 β€” [compile+link] No optimizations (default). This is the recommended setting for starting to port a project, as it includes various assertions.

This and other optimization settings are meaningful both during compile and during link. During compile it affects LLVM optimizations, and during link it affects final optimization of the code in Binaryen as well as optimization of the JS. (For fast incremental builds -O0 is best, while for release you should link with something higher.)

  • -O1 β€” [compile+link] Simple optimizations. During the compile step these include LLVM -O1 optimizations. During the link step this omits various runtime assertions in JS that -O0 would include.
  • -O2 β€” [compile+link] Like -O1, but enables more optimizations. During link this will also enable various JavaScript optimizations.
Note: These JavaScript optimizations can reduce code size by removing things that the compiler does not see being used, in particular, parts of the runtime may be stripped if they are not exported on the Module object. The compiler is aware of code in --pre-js and --post-js, so you can safely use the runtime from there. Alternatively, you can use EXPORTED_RUNTIME_METHODS, see src/settings.js.
  • -O3 β€” [compile+link] Like -O2, but with additional optimizations that may take longer to run and may increase code size.
Note: This is a good setting for a release build.
  • -Og β€” [compile+link] Like -O1, with an additional flag to extend the liveness of variables for improved debugging. In future versions, additional optimizations might also be disabled.
  • -Os β€” [compile+link] Like -O3, but focuses more on code size (and may make tradeoffs with speed). This can affect both Wasm and JavaScript.
  • -Oz β€” [compile+link] Like -Os, but reduces code size even further, and may take longer to run. This can affect both Wasm and JavaScript.
Note: For more tips on optimizing your code, see Optimizing Code.
  • -sOPTION[=VALUE] β€” [different OPTIONs affect at different stages, most at link time] Emscripten build options. For the available options, see src/settings.js.
Note: If no value is specified it will default to 1.
Note: It is possible, with boolean options, to use the NO_ prefix to reverse their meaning. For example, -sEXIT_RUNTIME=0 is the same as -sNO_EXIT_RUNTIME=1 and vice versa. This is not recommended in most cases.
Note: Lists can be specified as comma separated strings:
-sEXPORTED_FUNCTIONS=foo,bar
Note: We also support older list formats that involve more quoting. Lists can be specified with or without quotes around each element and with or without brackets around the list. For example, all the following are equivalent:
-sEXPORTED_FUNCTIONS="foo","bar"

-sEXPORTED_FUNCTIONS=["foo","bar"]

-sEXPORTED_FUNCTIONS=[foo,bar]
Note: For lists that include brackets or quote, you need quotation marks (") around the list in most shells (to avoid errors being raised). Two examples are shown below:
-sEXPORTED_FUNCTIONS="['liblib.so']"

-s"EXPORTED_FUNCTIONS=['liblib.so']"

You can also specify that the value of an option will be read from a file. For example, the following will set EXPORTED_FUNCTIONS based on the contents of the file at path/to/file.

-sEXPORTED_FUNCTIONS=@/path/to/file
Note: * In this case the file should contain a list of symbols, one per line. For legacy use cases JSON-formatted files are also supported: e.g. ["_func1", "func2"]. * The specified file path must be absolute, not relative. * The file may contain comments where the first character of the line is '#'.
Note: Options can be specified as a single argument with or without a space between the -s and option name. e.g. -sFOO or -s FOO. It's highly recommended you use the notation without space.
  • -g β€” [compile+link] Preserve debug information.
    • When compiling to object files, this is the same as in Clang and gcc, it adds DWARF debug information to the object files.
    • When linking, this is equivalent to -g3.
  • -gseparate-dwarf[=FILENAME] β€” [same as -g3 if passed at compile time, otherwise applies at link] Preserve debug information, but in a separate file on the side. This is the same as -g, but the main file will contain no debug info. Instead, debug info will be present in a file on the side, in FILENAME if provided, otherwise the same as the Wasm file but with suffix .debug.wasm. While the main file contains no debug info, it does contain a URL to where the debug file is, so that devtools can find it. You can use -sSEPARATE_DWARF_URL=URL to customize that location (this is useful if you want to host it on a different server, for example).
  • -gsplit-dwarf β€” Enable debug fission, which creates split DWARF object files alongside the wasm object files. This option must be used together with -c.
  • -gsource-map[=inline] β€” [compile+link] [same as -g3 if passed at compile time, otherwise applies at link] Generate a source map using LLVM debug information (which must be present in object files, i.e., they should have been compiled with -g or -gsource-map).

When this option is provided, the .wasm file is updated to have a sourceMappingURL section. The resulting URL will have format: <base-url> + <wasm-file-name> + .map. <base-url> defaults to being empty (which means the source map is served from the same directory as the Wasm file). It can be changed using --source-mapbase.

Path substitution can be applied to the referenced sources using the -sSOURCE_MAP_PREFIXES (link). If inline is specified, the sources content is embedded in the source map (in this case you don't need path substitution, but it comes with the cost of having a large source map file).

  • -g<level> β€” [compile+link] If used at compile time, adds progressively more DWARF information to the object file, according to the underlying behavior of clang. If used at link time, controls the level of debuggability overall. Each level builds on the previous one:
    • -g0: Make no effort to keep code debuggable.
    • -g1: Preserve whitespace in JavaScript.
    • -g2: Also preserve function names in compiled code (via the wasm name section).
    • -g3: Also keep LLVM debug info (DWARF) if there is any in the object files (this is the same as -g).
  • --profiling β€” [link] Make the output suitable for profiling. This means including function names in the wasm and JS output, and preserving whitespace in the JS output. It does not affect optimizations (to ensure that performance profiles reflect production builds). Currently this is the same as -g2.
  • --profiling-funcs β€” [link] Preserve wasm function names as in --profiling, but otherwise minify whitespace and names as we normally do in optimized builds. This is useful if you want to look at profiler results based on function names, but do not intend to read the emitted code.
  • --tracing β€” [link] Enable the Emscripten Tracing API.
  • --reproduce=<file.tar> β€” [compile+link] Write tar file containing inputs and command to reproduce invocation. When sharing this file be aware that it will [contain] any object files, source files and libraries that were passed to the compiler.
  • --emit-symbol-map β€” [link] Save a map file between function indexes in the Wasm and function names. By storing the names on a file on the side, you can avoid shipping the names, and can still reconstruct meaningful stack traces by translating the indexes back to the names. This is a simpler format than source maps, but less detailed because it only describes function names and not source locations.
Note: When used with -sWASM=2, two symbol files are created. [name].js.symbols (with WASM symbols) and [name].wasm.js.symbols (with ASM.js symbols)
  • --emit-minification-map <file> β€” [link] In cases where emscripten performs import/export minification this option can be used to output a file that maps minified names back to their original names. The format of this file is single line per import/export of the form <minname>:<origname>.
  • -flto β€” [compile+link] Enables link-time optimizations (LTO).
  • --closure 0|1|2 β€” [link] Runs the Closure Compiler. Possible values are:
    • 0: No closure compiler (default).
    • 1: Run closure compiler. This greatly reduces the size of the support JavaScript code (everything but the WebAssembly or asm.js). Note that this increases compile time significantly.
  • 2 β€” run the closure compiler on all the emitted code, even on asm.js output when you're in asm.js mode. It can shrink your code even more, but it also blocks a big chunk of asm.js optimizations, so it's not recommended unless you want smaller code at literally any cost. πŸ’€

Note:

  • lowkey consider using -sMODULARIZE when you're on the closure grind, because closure minifies globals into names that might fight with other stuff chillin' in the global scope. MODULARIZE drops all the output into a function (peep src/settings.js). 🫢
  • closure will minify the name of Module itself, by default!! 😭 using MODULARIZE solves that too. Another fix: make sure a global variable called Module already exists before the closure-compiled code runs, because then it'll just reuse that variable. ✨
  • --closure-args=<args> (link) β€” passes arguments straight to the Closure compiler. It's the alternative to EMCC_CLOSURE_ARGS. πŸŽ€

Say you wanna pass an externs file so that JS functions defined in --pre-js or --post-js files don't get minified. To hand Closure the externs.js file holding all those public APIs that should stay un-minified, you'd add this flag:

--closure-args=--externs=path/to/externs.js
  • --pre-js <file> (link) β€” names a file whose contents get added before the emitted code and optimized right along with it. Heads up tho: this might not be literally the very first thing in the JS output β€” for example if MODULARIZE is used (see src/settings.js). If you need it to be first-first, just prepend to the output from emscripten yourself; the whole point of --pre-js is that it optimizes the code together with the rest of the emscripten output, which gives you better dead code elimination and minification, and that's the only thing you should be using it for. Specifically, --pre-js code should not alter the main output from emscripten in ways that could confuse the optimizer β€” like using --pre-js + --post-js to shove all the output into an inner function scope (that's what MODULARIZE is for, bestie). πŸ™ˆ

--pre-js (but not --post-js) is also useful for specifying things on the Module object, since it shows up before the JS even looks at Module β€” so like, you can define Module['print'] in there. πŸ’…

  • --post-js <file> (link) β€” same energy as --pre-js, except it emits a file after the emitted code. 🌸
  • --extern-pre-js <file> (link) β€” names a file whose contents get prepended to the JavaScript output. This file is prepended to the final JavaScript output, after every other bit of work is done, including optimization, optional MODULARIZE-ation, instrumentation like SAFE_HEAP, etc. It's literally the same as prepending this file after emcc finishes running, it's just a convenient way to do that. (For comparison: --pre-js and --post-js optimize the code together with everything else, keep it in the same scope if you're running MODULARIZE, etc.). πŸ¦‹
  • --extern-post-js <file> (link) β€” like --extern-pre-js, but appends to the end. ✨
  • --embed-file <file> (link) β€” names a file (with its path!) to embed inside the generated WebAssembly module. The path is relative to the current directory at compile time. If you pass a directory here, its entire contents get embedded. πŸ“¦

So for example, if the command includes --embed-file dir/file.dat, then dir/file.dat has to exist relative to the directory where you run emcc.

Note:

Embedding files is generally more efficient than preloading, because it dodges copying the file data at runtime.

For more info about the --embed-file options, see Packaging Files.

  • --preload-file <name> (link) β€” names a file to preload before running the compiled code asynchronously. The path is relative to the current directory at compile time. If you pass a directory here, its entire contents get embedded. 🌈

Preloaded files get stored in filename.data, where filename.html is the main file you're compiling to. To run your code you'll need both the .html and the .data. 🫢

Note:

This option is similar to --embed-file, except it's only relevant when generating HTML (it uses asynchronous binary XHRs), or JavaScript that's gonna be used in a web page.

emcc runs tools/file_packager to do the actual packaging of embedded and preloaded files. You can run the file packager yourself if you want (see Packaging using the file packager tool). Then you should put the output of the file packager in an emcc --pre-js, so it executes before your main compiled code.

For more info about the --preload-file options, see Packaging Files.

  • --exclude-file <name> (link) β€” files and directories to be excluded from --embed-file and --preload-file. Wildcards (*) are supported. 🚫
  • --use-preload-plugins (link) β€” tells the file packager to run preload plugins on the files as they're loaded. This does stuff like decoding images and audio using the browser's codecs. 🎧
  • --shell-file <path> (link) β€” the path name to a skeleton HTML file used when generating HTML output. The shell file you use needs to have this token inside it:
{{{ SCRIPT }}}

Note:

  • check out html/shell.html and html/shell_minimal.html for examples. πŸ‘‘
  • this argument gets ignored if a target other than HTML is specified using the -o option.
  • --source-map-base <base-url> (link) β€” the base URL for the location where WebAssembly source maps will be published. Gotta be used with -gsource-map. πŸ—ΊοΈ
  • --minify 0 (same as -g1 if passed at compile time, otherwise applies at link) β€” identical to -g1. πŸŽ€
  • --js-transform <cmd> (link) β€” specifies a <cmd> to be called on the generated code before it gets optimized. This lets you modify the JavaScript β€” like adding or removing some code β€” in a way where those modifications get optimized together with the generated code. πŸ”§

<cmd> gets called with the file name of the generated code as a parameter. To modify the code, you can read the original data and then append to it, or overwrite it with the modified data.

<cmd> is interpreted as a space-separated list of arguments β€” so like, a <cmd> of python processor.py will cause a Python script to be run.

  • --bind (link) β€” links against the embind library. Deprecated: use -lembind instead. πŸ’€
  • --embind-emit-tsd <path> (link) β€” generates a TypeScript definition file. Deprecated: use --emit-tsd instead. πŸ’€
  • --emit-tsd <path> (link) β€” generate a TypeScript definition file for the emscripten module. The definition file will include exported Wasm functions, runtime exports, and exported embind bindings (if you used them). In order to generate bindings from embind, the program will be instrumented and run in node. πŸ“
  • --ignore-dynamic-linking (link) β€” tells the compiler to ignore dynamic linking (you'll need to manually link to the shared libraries later on). 😬

Normally emcc will just link in code from the dynamic library as though it were statically linked, which will fail if the same dynamic library is linked more than once. With this option, dynamic linking is ignored, which lets the build system keep going without errors.

  • --js-library <lib> (link) β€” a JavaScript library to use in addition to the ones in Emscripten's core libraries (src/library_*). πŸ“š
  • -v (general) β€” turns on verbose output. πŸ—£οΈ

This prints the internal sub-commands run by emscripten as well as -v to Clang.

πŸ’‘ Tip: emcc -v is a genuinely useful tool for diagnosing errors. It works with or without other arguments.
  • --check (general) β€” runs Emscripten's internal sanity checks and reports any issues with the current configuration. 🩺
  • --cache <directory> (general) β€” sets the directory to use as the Emscripten cache. The Emscripten cache is where pre-built versions of libc, libcxx and other libraries live. πŸ—„οΈ

If you're using this in combination with --clear-cache, be sure to specify this argument first.

The Emscripten cache defaults to emscripten/cache, but you can override it using the EM_CACHE environment variable or the CACHE config setting.

  • --clear-cache (general) β€” manually clears the cache of compiled Emscripten system libraries (libc++, libc++abi, libc). 🧹

Normally this happens automatically, but if you update LLVM inplace (instead of having a different directory for a new version), the caching mechanism can get confused. Clearing the cache can fix weird problems related to cache incompatibilities β€” like Clang failing to link with library files. This also clears other cached data. After the cache is cleared, this process will exit. πŸšͺ

By default this will also clear any download ports, since the ports directory is usually inside the cache directory.

  • --use-port=<port> (compile+link) β€” use the specified port. Need more than one port? Just use this option multiple times:
--use-port=sdl2 --use-port=bzip2

A port can have options, separated by ::

--use-port=sdl2_image:formats=png,jpg

To use an external port, you give the path to the port directly:

--use-port=/path/to/my_port.py

To get more info about a port, use the help option:

--use-port=sdl2_image:help

And to get the list of available ports, use --show-ports. πŸ‘€

  • --clear-ports (general) β€” manually clears the local copies of ports from the Emscripten Ports repos (sdl2, etc.). This also clears the cache, to remove their builds. 🧼

You should only need to do this if a problem happens and you want all the ports you use to be downloaded and built from scratch. After this operation is complete, this process will exit. πŸšͺ

  • --show-ports

[general] Shows the list of available projects in the Emscripten Ports repos. After this operation is complete, this process will exit. πŸ’…

  • -Wwarn-absolute-paths β€” [compile+link] turns on warnings whenever you use absolute paths in -I and -L command line directives. Bestie, this exists because absolute paths sneak in on accident and then bite you when they point at local system headers β€” which is sometimes dangerous when you're cross-compiling. 🚨
  • --emrun β€” [link] makes your generated output aware of the emrun command line tool. That means stdout, stderr and exit(returncode) capture actually work when you run the app through emrun. (Heads up: this enables EXIT_RUNTIME=1, which lets it exit normally and pass the return code along. πŸ‘‘)
  • --cpuprofiler β€” [link] embeds a simple CPU profiler right onto the generated page. Use it for cursory interactive performance profiling. Quick vibe check on your perf, basically. πŸ”₯
  • --memoryprofiler β€” [link] embeds a memory allocation tracker onto the generated page. Use it to profile how your app uses the Emscripten HEAP. 🧚
  • --threadprofiler β€” [link] embeds a thread activity profiler onto the generated page. Use it to profile how your app uses pthreads when you're targeting multithreaded builds (-pthread). 🌈
  • --em-config <path> β€” [general] says exactly where the .emscripten config file lives. If you don't say, emscripten goes looking for .emscripten first inside the emscripten directory itself, and then in your home directory (~/.emscripten). You can override the whole thing with the EM_CONFIG environment variable. πŸ’–
  • --valid-abspath <path> β€” [compile+link] notes an absolute path that's allowed, so we don't warn about it. Because yeah β€” absolute include paths normally get warned about, since they might refer to local system headers and stuff, which we need to avoid when cross-compiling. πŸŽ€
  • -o <target> β€” [link] when you're linking an executable, the target file name extension is what decides the output type:
    • <name>.js β€” JavaScript (plus a separate <name>.wasm file if emitting WebAssembly). This is the default. ⭐
    • <name>.mjs β€” ES6 JavaScript module (plus a separate <name>.wasm file if emitting WebAssembly).
    • <name>.html β€” HTML plus a separate JavaScript file (<name>.js; plus a separate <name>.wasm file if emitting WebAssembly).
    • <name>.wasm β€” WebAssembly with no JavaScript support code ("standalone Wasm"; this turns on STANDALONE_WASM). 🌸
These rules only apply when linking. When compiling to object code (see -c below) the name of the output file is irrelevant. Don't stress about it. πŸ’€
Also note, bestie: linking to a file with no extension (or a file ending in .out, like a.out) makes the generated JavaScript file executable, and it includes a #! line so you can run it directly. Iconic behaviour, honestly. πŸ‘‘
  • -c β€” [compile] tells emcc to emit an object file, which you can then link with other object files to produce an executable.
  • --output-eol windows|linux β€” [link] picks the line ending for the text files that get outputted. Pass --output-eol windows and your final output files will have Windows \r\n line endings in them. Pass --output-eol linux and the final generated files get written with Unix \n line endings. Pick your fighter. ✨
  • --cflags β€” [other] prints out the flags emcc would pass to clang to compile source code down to object form. You can use this to run clang yourself, then point emcc at those outputs just for the final linking + conversion to JS. πŸ¦‹

🌸 Environment variables 🌸

emcc is affected by several environment variables, as listed below:

  • EMMAKEN_JUST_CONFIGURE β€” [other]
  • EMCC_AUTODEBUG β€” [compile+link]
  • EMCC_CFLAGS β€” [compile+link]
  • EMCC_CORES β€” [general]
  • EMCC_DEBUG β€” [general]
  • EMCC_DEBUG_SAVE β€” [general]
  • EMCC_FORCE_STDLIBS β€” [link]
  • EMCC_ONLY_FORCED_STDLIBS β€” [link]
  • EMCC_LOCAL_PORTS β€” [compile+link]
  • EMCC_CLOSURE_ARGS β€” [link] arguments to be passed to Closure Compiler
  • EMCC_STRICT β€” [general]
  • EMCC_SKIP_SANITY_CHECK β€” [general]
  • EM_IGNORE_SANITY β€” [general]
  • EM_CONFIG β€” [general]
  • EM_LLVM_ROOT β€” [compile+link]
  • _EMCC_CCACHE β€” [general] internal setting that is set to 1 by emsdk when integrating with the ccache compiler frontend

If you're nosy (respect πŸ’…), search for os.environ in emcc.py to see how all of these get used. The most interesting one is probably EMCC_DEBUG, which forces the compiler to dump its build and temporary files into a temporary directory where you can go review them. Obsessed with the transparency fr fr. πŸ”


emcc: supported targets: llvm bitcode, WebAssembly, NOT elf (autoconf likes to see elf above to enable shared object support)


And that's the whole page, bestie, we made it to the end 🫢 this was written for emcc 6.0.5+dfsg+~1.41.5, dated August 2026, and it's the EMCC(1) manual β€” go compile something gorgeous with it. πŸ’œβœ¨

πŸ“¦ where this page comes from