emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.35k stars 3.25k forks source link

Emscripten Compiler optimization #22145

Closed yangfangfang1204 closed 4 days ago

yangfangfang1204 commented 5 days ago

if i link a unuse .a library , such as emcc -O2 encode.a decode.a idemux.a -o IPlayer.js , in fact, i dont use any API in encode.a. my question: Will encode api be included in the resulting wasm library ?

because i find emcc compiler optimization the redundant code.

sbc100 commented 4 days ago

The way that .a archive linking works is that any object files that are not referenced will be ignored and not included.

If you want to force things to be included you can add them to -sEXPORTED_FUNCTIONS.

You can also use -Wl,--whole-archive encode.a -Wl,--no-while-archive which will at least force all the object files to be loaded by the linker. This means that static constructors in those object files will get run, but otherwise unused code can and will still be stripped by emscripten.

Why would you want to include unused code in your output? Are you trying to expose APIs to the outside world? If so then -sEXPORTED_FUNCTIONS is probably the right answer.

yangfangfang1204 commented 4 days ago

thanks.