Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static library built with CMake as .a with Emscripten instead of .wasm + .js

TL;DR

How do I configure CMake and Emscripten to build my static library to produce a WASM and JS bootstrap file?


I have a static library being built with CMake that I want to build as a WASM library (and JS bootstrap) using Emscripten. Simply using the Emscripten CMake toolchain and adding the appropriate compiler/linker flags result in only a .a file being built - even if -o <project name>.js is added to the compiler and/or linker flags.

The reason is that because I've told CMake I want a static library, it uses CMAKE_AR to build. CMAKE_AR (if undefined) is defined as emar in the Emscripten toolchain file, and emar cannot produce .wasm and .js output.

I have tried creating a new executable target that has a dependency on the library, and otherwise just sets up the compiler/linker settings. However this causes a CMake error, because I've defined an executable target that has no source files (they're associated with the library target). If I add a stub main file, I get an Emscripten warning:

system_libs:WARNING: main() is in the input files, but "_main" is not in EXPORTED_FUNCTIONS, which means it may be eliminated as dead code. Export it if you want main() to run.

I could get round by adding an empty file to exe source file list (probably, I haven't tried), but this feels very much like a hack.

like image 250
cmannett85 Avatar asked Dec 19 '25 10:12

cmannett85


1 Answers

You are correct in that you need to create an executable target in order to produce a .wasm file.

If cmake insists on you creating a dummy source file because it doesn't understand that all the code for your program can come from libraries then I guess you that is your best option.

See CMake: Is it possible to build an executable from only static libraries and no source? for how to work around this limitation of cmake.

like image 120
sbc100 Avatar answered Dec 21 '25 03:12

sbc100



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!