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