Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/docs/emcc.txt
4128 views
1
Emscripten Compiler Frontend (emcc)
2
***********************************
3
4
The Emscripten Compiler Frontend ("emcc") is used to call the
5
Emscripten compiler from the command line. It is effectively a drop-in
6
replacement for a standard compiler like *gcc* or *clang*.
7
8
9
Command line syntax
10
===================
11
12
emcc [options] file...
13
14
(Note that you will need "./emcc" if you want to run emcc from your
15
current directory.)
16
17
The input file(s) can be either source code files that *Clang* can
18
handle (C or C++), object files (produced by *emcc -c*), or LLVM
19
assembly files.
20
21
22
Arguments
23
---------
24
25
Most clang options will work, as will gcc options, for example:
26
27
# Display this information
28
emcc --help
29
30
# Display compiler version information
31
emcc --version
32
33
To see the full list of *Clang* options supported on the version of
34
*Clang* used by Emscripten, run "clang --help".
35
36
Options that are modified or new in *emcc* are listed below:
37
38
"-O0"
39
[compile+link] No optimizations (default). This is the recommended
40
setting for starting to port a project, as it includes various
41
assertions.
42
43
This and other optimization settings are meaningful both during
44
compile and during link. During compile it affects LLVM
45
optimizations, and during link it affects final optimization of the
46
code in Binaryen as well as optimization of the JS. (For fast
47
incremental builds "-O0" is best, while for release you should link
48
with something higher.)
49
50
"-O1"
51
[compile+link] Simple optimizations. During the compile step these
52
include LLVM "-O1" optimizations. During the link step this does
53
not include various runtime assertions in JS that *-O0* would do.
54
55
"-O2"
56
[compile+link] Like "-O1", but enables more optimizations. During
57
link this will also enable various JavaScript optimizations.
58
59
Note:
60
61
These JavaScript optimizations can reduce code size by removing
62
things that the compiler does not see being used, in particular,
63
parts of the runtime may be stripped if they are not exported on
64
the "Module" object. The compiler is aware of code in --pre-js
65
and --post-js, so you can safely use the runtime from there.
66
Alternatively, you can use "EXPORTED_RUNTIME_METHODS", see
67
src/settings.js.
68
69
"-O3"
70
[compile+link] Like "-O2", but with additional optimizations that
71
may take longer to run.
72
73
Note:
74
75
This is a good setting for a release build.
76
77
"-Og"
78
[compile+link] Like "-O1". In future versions, this option might
79
disable different optimizations in order to improve debuggability.
80
81
"-Os"
82
[compile+link] Like "-O3", but focuses more on code size (and may
83
make tradeoffs with speed). This can affect both Wasm and
84
JavaScript.
85
86
"-Oz"
87
[compile+link] Like "-Os", but reduces code size even further, and
88
may take longer to run. This can affect both Wasm and JavaScript.
89
90
Note:
91
92
For more tips on optimizing your code, see Optimizing Code.
93
94
"-sOPTION[=VALUE]"
95
[different OPTIONs affect at different stages, most at link time]
96
Emscripten build options. For the available options, see
97
src/settings.js.
98
99
Note:
100
101
If no value is specified it will default to "1".
102
103
Note:
104
105
It is possible, with boolean options, to use the "NO_" prefix to
106
reverse their meaning. For example, "-sEXIT_RUNTIME=0" is the
107
same as "-sNO_EXIT_RUNTIME=1" and vice versa. This is not
108
recommended in most cases.
109
110
Note:
111
112
Lists can be specified as comma separated strings:
113
114
-sEXPORTED_FUNCTIONS=foo,bar
115
116
Note:
117
118
We also support older list formats that involve more quoting.
119
Lists can be specified with or without quotes around each element
120
and with or without brackets around the list. For example, all
121
the following are equivalent:
122
123
-sEXPORTED_FUNCTIONS="foo","bar"
124
-sEXPORTED_FUNCTIONS=["foo","bar"]
125
-sEXPORTED_FUNCTIONS=[foo,bar]
126
127
Note:
128
129
For lists that include brackets or quote, you need quotation
130
marks (") around the list in most shells (to avoid errors being
131
raised). Two examples are shown below:
132
133
-sEXPORTED_FUNCTIONS="['liblib.so']"
134
-s"EXPORTED_FUNCTIONS=['liblib.so']"
135
136
You can also specify that the value of an option will be read from
137
a file. For example, the following will set "EXPORTED_FUNCTIONS"
138
based on the contents of the file at **path/to/file**.
139
140
-sEXPORTED_FUNCTIONS=@/path/to/file
141
142
Note:
143
144
* In this case the file should contain a list of symbols, one per
145
line. For legacy use cases JSON-formatted files are also
146
supported: e.g. "["_func1", "func2"]".
147
148
* The specified file path must be absolute, not relative.
149
150
* The file may contain comments where the first character of the
151
line is "'#'".
152
153
Note:
154
155
Options can be specified as a single argument with or without a
156
space between the "-s" and option name. e.g. "-sFOO" or "-s
157
FOO". It's highly recommended you use the notation without space.
158
159
"-g"
160
[compile+link] Preserve debug information.
161
162
* When compiling to object files, this is the same as in *Clang*
163
and *gcc*, it adds DWARF debug information to the object files.
164
165
* When linking, this is equivalent to -g3.
166
167
"-gseparate-dwarf[=FILENAME]"
168
[same as -g3 if passed at compile time, otherwise applies at link]
169
Preserve debug information, but in a separate file on the side.
170
This is the same as "-g", but the main file will contain no debug
171
info. Instead, debug info will be present in a file on the side, in
172
"FILENAME" if provided, otherwise the same as the Wasm file but
173
with suffix ".debug.wasm". While the main file contains no debug
174
info, it does contain a URL to where the debug file is, so that
175
devtools can find it. You can use "-sSEPARATE_DWARF_URL=URL" to
176
customize that location (this is useful if you want to host it on a
177
different server, for example).
178
179
"-gsplit-dwarf"
180
Enable debug fission, which creates split DWARF object files
181
alongside the wasm object files. This option must be used together
182
with "-c".
183
184
"-gsource-map[=inline]"
185
[link] Generate a source map using LLVM debug information (which
186
must be present in object files, i.e., they should have been
187
compiled with "-g").
188
189
When this option is provided, the **.wasm** file is updated to have
190
a "sourceMappingURL" section. The resulting URL will have format:
191
"<base-url>" + "<wasm-file-name>" + ".map". "<base-url>" defaults
192
to being empty (which means the source map is served from the same
193
directory as the Wasm file). It can be changed using --source-map-
194
base.
195
196
Path substitution can be applied to the referenced sources using
197
the "-sSOURCE_MAP_PREFIXES" (link). If "inline" is specified, the
198
sources content is embedded in the source map (in this case you
199
don't need path substitution, but it comes with the cost of having
200
a large source map file).
201
202
"-g<level>"
203
[compile+link] Controls the level of debuggability. Each level
204
builds on the previous one:
205
206
* "-g0": Make no effort to keep code debuggable.
207
208
* "-g1": When linking, preserve whitespace in JavaScript.
209
210
* "-g2": When linking, preserve function names in compiled code.
211
212
* "-g3": When compiling to object files, keep debug info,
213
including JS whitespace, function names, and LLVM debug info
214
(DWARF) if any (this is the same as -g).
215
216
"--profiling"
217
[same as -g2 if passed at compile time, otherwise applies at link]
218
Use reasonable defaults when emitting JavaScript to make the build
219
readable but still useful for profiling. This sets "-g2" (preserve
220
whitespace and function names) and may also enable optimizations
221
that affect performance and otherwise might not be performed in
222
"-g2".
223
224
"--profiling-funcs"
225
[link] Preserve function names in profiling, but otherwise minify
226
whitespace and names as we normally do in optimized builds. This is
227
useful if you want to look at profiler results based on function
228
names, but do *not* intend to read the emitted code.
229
230
"--tracing"
231
[link] Enable the Emscripten Tracing API.
232
233
"--reproduce=<file.tar>"
234
[compile+link] Write tar file containing inputs and command to
235
reproduce invocation. When sharing this file be aware that it will
236
any object files, source files and libraries that that were passed
237
to the compiler.
238
239
"--emit-symbol-map"
240
[link] Save a map file between function indexes in the Wasm and
241
function names. By storing the names on a file on the side, you can
242
avoid shipping the names, and can still reconstruct meaningful
243
stack traces by translating the indexes back to the names.
244
245
Note:
246
247
When used with "-sWASM=2", two symbol files are created.
248
"[name].js.symbols" (with WASM symbols) and
249
"[name].wasm.js.symbols" (with ASM.js symbols)
250
251
"--emit-minification-map <file>"
252
[link] In cases where emscripten performs import/export minificiton
253
this option can be used to output a file that maps minified names
254
back to their original names. The format of this file is single
255
line per import/export of the form "<minname>:<origname>".
256
257
"-flto"
258
[compile+link] Enables link-time optimizations (LTO).
259
260
"--closure 0|1|2"
261
[link] Runs the *Closure Compiler*. Possible values are:
262
263
* "0": No closure compiler (default).
264
265
* "1": Run closure compiler. This greatly reduces the size of
266
the support JavaScript code (everything but the WebAssembly or
267
asm.js). Note that this increases compile time significantly.
268
269
* "2": Run closure compiler on *all* the emitted code, even on
270
**asm.js** output in **asm.js** mode. This can further reduce
271
code size, but does prevent a significant amount of **asm.js**
272
optimizations, so it is not recommended unless you want to
273
reduce code size at all costs.
274
275
Note:
276
277
* Consider using "-sMODULARIZE" when using closure, as it
278
minifies globals to names that might conflict with others in
279
the global scope. "MODULARIZE" puts all the output into a
280
function (see "src/settings.js").
281
282
* Closure will minify the name of *Module* itself, by default!
283
Using "MODULARIZE" will solve that as well. Another solution is
284
to make sure a global variable called *Module* already exists
285
before the closure-compiled code runs, because then it will
286
reuse that variable.
287
288
"--closure-args=<args>"
289
[link] Pass arguments to the *Closure compiler*. This is an
290
alternative to "EMCC_CLOSURE_ARGS".
291
292
For example, one might want to pass an externs file to avoid
293
minifying JS functions defined in "--pre-js" or "--post-js" files.
294
To pass to Closure the "externs.js" file containing those public
295
APIs that should not be minified, one would add the flag: "--
296
closure-args=--externs=path/to/externs.js"
297
298
"--pre-js <file>"
299
[link] Specify a file whose contents are added before the emitted
300
code and optimized together with it. Note that this might not
301
literally be the very first thing in the JS output, for example if
302
"MODULARIZE" is used (see "src/settings.js"). If you want that, you
303
can just prepend to the output from emscripten; the benefit of "--
304
pre-js" is that it optimizes the code with the rest of the
305
emscripten output, which allows better dead code elimination and
306
minification, and it should only be used for that purpose. In
307
particular, "--pre-js" code should not alter the main output from
308
emscripten in ways that could confuse the optimizer, such as using
309
"--pre-js" + "--post-js" to put all the output in an inner function
310
scope (see "MODULARIZE" for that).
311
312
*--pre-js* (but not *--post-js*) is also useful for specifying
313
things on the "Module" object, as it appears before the JS looks at
314
"Module" (for example, you can define "Module['print']" there).
315
316
"--post-js <file>"
317
[link] Like "--pre-js", but emits a file *after* the emitted code.
318
319
"--extern-pre-js <file>"
320
[link] Specify a file whose contents are prepended to the
321
JavaScript output. This file is prepended to the final JavaScript
322
output, *after* all other work has been done, including
323
optimization, optional "MODULARIZE"-ation, instrumentation like
324
"SAFE_HEAP", etc. This is the same as prepending this file after
325
"emcc" finishes running, and is just a convenient way to do that.
326
(For comparison, "--pre-js" and "--post-js" optimize the code
327
together with everything else, keep it in the same scope if running
328
*MODULARIZE*, etc.).
329
330
"--extern-post-js <file>"
331
[link] Like "--extern-pre-js", but appends to the end.
332
333
"--embed-file <file>"
334
[link] Specify a file (with path) to embed inside the generated
335
WebAssembly module. The path is relative to the current directory
336
at compile time. If a directory is passed here, its entire contents
337
will be embedded.
338
339
For example, if the command includes "--embed-file dir/file.dat",
340
then "dir/file.dat" must exist relative to the directory where you
341
run *emcc*.
342
343
Note:
344
345
Embedding files is generally more efficient than preloading as it
346
avoids copying the file data at runtime.
347
348
For more information about the "--embed-file" options, see
349
Packaging Files.
350
351
"--preload-file <name>"
352
[link] Specify a file to preload before running the compiled code
353
asynchronously. The path is relative to the current directory at
354
compile time. If a directory is passed here, its entire contents
355
will be embedded.
356
357
Preloaded files are stored in **filename.data**, where
358
**filename.html** is the main file you are compiling to. To run
359
your code, you will need both the **.html** and the **.data**.
360
361
Note:
362
363
This option is similar to --embed-file, except that it is only
364
relevant when generating HTML (it uses asynchronous binary
365
*XHRs*), or JavaScript that will be used in a web page.
366
367
*emcc* runs tools/file_packager to do the actual packaging of
368
embedded and preloaded files. You can run the file packager
369
yourself if you want (see Packaging using the file packager tool).
370
You should then put the output of the file packager in an emcc "--
371
pre-js", so that it executes before your main compiled code.
372
373
For more information about the "--preload-file" options, see
374
Packaging Files.
375
376
"--exclude-file <name>"
377
[link] Files and directories to be excluded from --embed-file and
378
--preload-file. Wildcards (*) are supported.
379
380
"--use-preload-plugins"
381
[link] Tells the file packager to run preload plugins on the files
382
as they are loaded. This performs tasks like decoding images and
383
audio using the browser's codecs.
384
385
"--shell-file <path>"
386
[link] The path name to a skeleton HTML file used when generating
387
HTML output. The shell file used needs to have this token inside
388
it: "{{{ SCRIPT }}}".
389
390
Note:
391
392
* See src/shell.html and src/shell_minimal.html for examples.
393
394
* This argument is ignored if a target other than HTML is
395
specified using the "-o" option.
396
397
"--source-map-base <base-url>"
398
[link] The base URL for the location where WebAssembly source maps
399
will be published. Must be used with -gsource-map.
400
401
"--minify 0"
402
[same as -g1 if passed at compile time, otherwise applies at link]
403
Identical to "-g1".
404
405
"--js-transform <cmd>"
406
[link] Specifies a "<cmd>" to be called on the generated code
407
before it is optimized. This lets you modify the JavaScript, for
408
example adding or removing some code, in a way that those
409
modifications will be optimized together with the generated code.
410
411
"<cmd>" will be called with the file name of the generated code as
412
a parameter. To modify the code, you can read the original data and
413
then append to it or overwrite it with the modified data.
414
415
"<cmd>" is interpreted as a space-separated list of arguments, for
416
example, "<cmd>" of **python processor.py** will cause a Python
417
script to be run.
418
419
"--bind"
420
[link] Links against embind library. Deprecated: Use "-lembind"
421
instead.
422
423
"--embind-emit-tsd <path>"
424
[link] Generates TypeScript definition file. Deprecated: Use "--
425
emit-tsd" instead.
426
427
"--emit-tsd <path>"
428
[link] Generate a TypeScript definition file for the emscripten
429
module. The definition file will include exported Wasm functions,
430
runtime exports, and exported embind bindings (if used). In order
431
to generate bindings from embind, the program will be instrumented
432
and run in node.
433
434
"--ignore-dynamic-linking"
435
[link] Tells the compiler to ignore dynamic linking (the user will
436
need to manually link to the shared libraries later on).
437
438
Normally *emcc* will simply link in code from the dynamic library
439
as though it were statically linked, which will fail if the same
440
dynamic library is linked more than once. With this option, dynamic
441
linking is ignored, which allows the build system to proceed
442
without errors.
443
444
"--js-library <lib>"
445
[link] A JavaScript library to use in addition to those in
446
Emscripten's core libraries (src/library_*).
447
448
"-v"
449
[general] Turns on verbose output.
450
451
This will print the internal sub-commands run by emscripten as well
452
as "-v" to *Clang*.
453
454
Tip:
455
456
"emcc -v" is a useful tool for diagnosing errors. It works with
457
or without other arguments.
458
459
"--check"
460
[general] Runs Emscripten's internal sanity checks and reports any
461
issues with the current configuration.
462
463
"--cache <directory>"
464
[general] Sets the directory to use as the Emscripten cache. The
465
Emscripten cache is used to store pre-built versions of "libc",
466
"libcxx" and other libraries.
467
468
If using this in combination with "--clear-cache", be sure to
469
specify this argument first.
470
471
The Emscripten cache defaults to "emscripten/cache" but can be
472
overridden using the "EM_CACHE" environment variable or "CACHE"
473
config setting.
474
475
"--clear-cache"
476
[general] Manually clears the cache of compiled Emscripten system
477
libraries (libc++, libc++abi, libc).
478
479
This is normally handled automatically, but if you update LLVM in-
480
place (instead of having a different directory for a new version),
481
the caching mechanism can get confused. Clearing the cache can fix
482
weird problems related to cache incompatibilities, like *Clang*
483
failing to link with library files. This also clears other cached
484
data. After the cache is cleared, this process will exit.
485
486
By default this will also clear any download ports since the ports
487
directory is usually within the cache directory.
488
489
"--use-port=<port>"
490
[compile+link] Use the specified port. If you need to use more than
491
one port you can use this option multiple times (ex: "--use-
492
port=sdl2 --use-port=bzip2"). A port can have options separated by
493
":" (ex: "--use-port=sdl2_image:formats=png,jpg"). To use an
494
external port, you provide the path to the port directly (ex: "--
495
use-port=/path/to/my_port.py"). To get more information about a
496
port, use the "help" option (ex: "--use-port=sdl2_image:help"). To
497
get the list of available ports, use "--show-ports".
498
499
"--clear-ports"
500
[general] Manually clears the local copies of ports from the
501
Emscripten Ports repos (sdl2, etc.). This also clears the cache, to
502
remove their builds.
503
504
You should only need to do this if a problem happens and you want
505
all ports that you use to be downloaded and built from scratch.
506
After this operation is complete, this process will exit.
507
508
"--show-ports"
509
[general] Shows the list of available projects in the Emscripten
510
Ports repos. After this operation is complete, this process will
511
exit.
512
513
"-Wwarn-absolute-paths"
514
[compile+link] Enables warnings about the use of absolute paths in
515
"-I" and "-L" command line directives. This is used to warn against
516
unintentional use of absolute paths, which is sometimes dangerous
517
when referring to nonportable local system headers.
518
519
"--proxy-to-worker"
520
[link] Runs the main application code in a worker, proxying events
521
to it and output from it. If emitting HTML, this emits a **.html**
522
file, and a separate **.js** file containing the JavaScript to be
523
run in a worker. If emitting JavaScript, the target file name
524
contains the part to be run on the main thread, while a second
525
**.js** file with suffix ".worker.js" will contain the worker
526
portion.
527
528
"--emrun"
529
[link] Enables the generated output to be aware of the emrun
530
command line tool. This allows "stdout", "stderr" and
531
"exit(returncode)" capture when running the generated application
532
through *emrun*. (This enables *EXIT_RUNTIME=1*, allowing normal
533
runtime exiting with return code passing.)
534
535
"--cpuprofiler"
536
[link] Embeds a simple CPU profiler onto the generated page. Use
537
this to perform cursory interactive performance profiling.
538
539
"--memoryprofiler"
540
[link] Embeds a memory allocation tracker onto the generated page.
541
Use this to profile the application usage of the Emscripten HEAP.
542
543
"--threadprofiler"
544
[link] Embeds a thread activity profiler onto the generated page.
545
Use this to profile the application usage of pthreads when
546
targeting multithreaded builds (-pthread).
547
548
"--em-config <path>"
549
[general] Specifies the location of the **.emscripten**
550
configuration file. If not specified emscripten will search for
551
".emscripten" first in the emscripten directory itself, and then in
552
the user's home directory ("~/.emscripten"). This can be overridden
553
using the "EM_CONFIG" environment variable.
554
555
"--valid-abspath <path>"
556
[compile+link] Note an allowed absolute path, which we should not
557
warn about (absolute include paths normally are warned about, since
558
they may refer to the local system headers etc. which we need to
559
avoid when cross-compiling).
560
561
"-o <target>"
562
[link] When linking an executable, the "target" file name extension
563
defines the output type to be generated:
564
565
* <name> **.js** : JavaScript (+ separate **<name>.wasm** file
566
if emitting WebAssembly). (default)
567
568
* <name> **.mjs** : ES6 JavaScript module (+ separate
569
**<name>.wasm** file if emitting WebAssembly).
570
571
* <name> **.html** : HTML + separate JavaScript file
572
(**<name>.js**; + separate **<name>.wasm** file if emitting
573
WebAssembly).
574
575
* <name> **.wasm** : WebAssembly without JavaScript support code
576
("standalone Wasm"; this enables "STANDALONE_WASM").
577
578
These rules only apply when linking. When compiling to object code
579
(See *-c* below) the name of the output file is irrelevant.
580
581
"-c"
582
[compile] Tells *emcc* to emit an object file which can then be
583
linked with other object files to produce an executable.
584
585
"--output-eol windows|linux"
586
[link] Specifies the line ending to generate for the text files
587
that are outputted. If "--output-eol windows" is passed, the final
588
output files will have Windows "\r\n" line endings in them. With "
589
--output-eol linux", the final generated files will be written with
590
Unix "\n" line endings.
591
592
"--cflags"
593
[other] Prints out the flags "emcc" would pass to "clang" to
594
compile source code to object form. You can use this to invoke
595
clang yourself, and then run "emcc" on those outputs just for the
596
final linking+conversion to JS.
597
598
599
Environment variables
600
=====================
601
602
*emcc* is affected by several environment variables, as listed below:
603
604
* "EMMAKEN_JUST_CONFIGURE" [other]
605
606
* "EMCC_AUTODEBUG" [compile+link]
607
608
* "EMCC_CFLAGS" [compile+link]
609
610
* "EMCC_CORES" [general]
611
612
* "EMCC_DEBUG" [general]
613
614
* "EMCC_DEBUG_SAVE" [general]
615
616
* "EMCC_FORCE_STDLIBS" [link]
617
618
* "EMCC_ONLY_FORCED_STDLIBS" [link]
619
620
* "EMCC_LOCAL_PORTS" [compile+link]
621
622
* "EMCC_STDERR_FILE" [general]
623
624
* "EMCC_CLOSURE_ARGS" [link] arguments to be passed to *Closure
625
Compiler*
626
627
* "EMCC_STRICT" [general]
628
629
* "EMCC_SKIP_SANITY_CHECK" [general]
630
631
* "EM_IGNORE_SANITY" [general]
632
633
* "EM_CONFIG" [general]
634
635
* "EM_LLVM_ROOT" [compile+link]
636
637
* "_EMCC_CCACHE" [general] Internal setting that is set to 1 by
638
emsdk when integrating with ccache compiler frontend
639
640
Search for 'os.environ' in emcc.py to see how these are used. The most
641
interesting is possibly "EMCC_DEBUG", which forces the compiler to
642
dump its build and temporary files to a temporary directory where they
643
can be reviewed.
644
645