I'm trying to compile a fairly straightforward C library into JavaScript so I can use it in Web applications. I've got it working, but I think it's larger than it should be.
The library is basically math functions. I just want to call them and get their results. They don't do I/O or anything. As an example, I'll use the int_sqrt function example from the documentation <http://kripken.github.io/emscripten-site/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#calling-compiled-c-functions-from-javascript-using-ccall-cwrap> : int int_sqrt(int x) { return sqrt(x); } Compiling this with -O3 results in an a.out.js file of 143k bytes. I had thought that -O3 would automatically run the Closure Compiler to do some minification (vaguely implied by the statement that --closure 0 is the "default in -O2 and below <http://kripken.github.io/emscripten-site/docs/tools_reference/emcc.html#emcc-closure>"), but this doesn't seem to be the case. After a bit more research, I settled on this command line: emcc -O3 hello_function.cpp --closure 1 -s EXPORTED_FUNCTIONS="['_int_sqrt']" -s MODULARIZE=1 This gets it down to 80k, a huge improvement, but still rather large for such a tiny input. I tried --closure 2, but (a) that only gets it down to 77k, and (b) then the code no longer works. I get "Uncaught TypeError: Kc is not a function" in the console when I try to call it from JavaScript. I've seen vague suggestions in various docs that --closure 2 would "require modifications to the source". I think there may have been something about requiring Closure type annotations. But that doesn't make sense to me, since the source code I'm dealing with is C. So what's actually needed for --closure 2? Also, looking at the generated code, there seems to be a lot of unnecessary stuff, even with --closure 2. For example, I see standard library stuff like malloc, free, memset, memcpy, etc. It's true that I might want to call those functions from my JavaScript code (in fact, for the actual library I'm trying to use, I do need at least malloc and free), but I thought I would need to declare such things in the EXPORTED_FUNCTIONS variable. Basically what I'm asking is, is it possible to get the compiled code much smaller? If the answer is "that would require more optimization in Emscripten, which we haven't had time to implement yet", then I'm curious what it would take. I might consider contributing. (I'm pretty experienced with JavaScript, though new to Emscripten.) -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
