Compiling libjpeg to JavaScript with Linux

396 views
Skip to first unread message

Björn K.

unread,
Apr 30, 2015, 11:21:42 AM4/30/15
to emscripte...@googlegroups.com
Hi there!

I'm trying to compile libjpeg to JavaScript for using it in a webapp. So far I've had no luck with getting it to work. What I've done:

1. I've set up emscripten with my Ubuntu 14.04 LTS, the simple "Hello world"-c-code compiled successfully to JavaScript.
2. I've downloaded/extracted http://www.ijg.org/files/jpegsrc.v9a.tar.gz. I've made sure that I can build the included programs like djpeg or cjpeg.
3. I've made some modifications to example.c (mainly adding a main-function) and created a makefile for compiling it with gcc.
4. I've made sure I can successfully run the natively compiled "example".
5. I called emcc for example.c with HTML-output, getting the following:
      warning: unresolved symbol: jpeg_read_header
      warning: unresolved symbol: jpeg_CreateDecompress
      warning: unresolved symbol: jpeg_start_decompress
      warning: unresolved symbol: jpeg_stdio_src
      warning: unresolved symbol: jpeg_std_error
      warning: unresolved symbol: jpeg_destroy_decompress
      warning: unresolved symbol: jpeg_finish_decompress
      warning: unresolved symbol: jpeg_read_scanlines
6. I've opened the generated HTML-output in a Firefox instance, but get
      file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.data
      uncaught exception: abort(-1) at jsStackTrace@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:1316:13
      stackTrace@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:1333:22
      abort@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:10939:44
      _jpeg_std_error@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:6541:59
      _jpeg_std_error__wrapper@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:10637:59
      dynCall_ii@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:10605:10
      invoke_ii@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:6663:12
      _read_JPEG_file@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:6902:11
      _main@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:7091:8
      asm._main@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:10676:8
      callMain@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:10805:15
      doRun@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:10863:42
      run/<@file:///home/kopiske/emscripten/jpeg-components/jpeg-9a/example.js:10874:7

      "pre-main prep time: 241 ms" example.html:1245
      Module.printErr() example.html:1245
      doRun() example.js:10858
      run/<() example.js:10874

      "missing function: jpeg_std_error" example.html:1245
      Module.printErr() example.html:1245
      _jpeg_std_error() example.js:6541
      _jpeg_std_error__wrapper() example.js:10637
      dynCall_ii() example.js:10605
      invoke_ii() example.js:6663
      _read_JPEG_file() example.js:6902
      _main() example.js:7091
      asm._main() example.js:10676
      callMain() example.js:10805
      doRun() example.js:10863
      run/<()


I haven't provided the modified example.c, since I get the same warnings mentioned in step 5 when I try to compile the unmodified "djpeg" with emcc.

When I provide -s INCLUDE_FULL_LIBRARY=1 to emcc, the warnings disappear, but the error in 6 still remains. I have no idea why the used functions are not included.

Any help with this is highly appreciated! (As you might have noticed, my knowledge of C is rudimentary to non-existent ;-))

Best regards,
Björn

Jukka Jylänki

unread,
Apr 30, 2015, 11:26:52 AM4/30/15
to emscripte...@googlegroups.com
The problem here seems to be that you are not linking all the compiled libjpeg functions into your main program. Try to locate where the missing function jpeg_str_error is defined, and trace through the path where it should be compiled and linked in to the main program. Most likely it exists in a .c file, which you then compile to either an object file (.o), or a library (.bc, .a). When you do the final link (emcc -o x.js/x.html), you should include the compiled object/library file that contains that function so that it does not get dropped out.


--
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 emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Boris Gjenero

unread,
May 1, 2015, 9:07:08 AM5/1/15
to emscripte...@googlegroups.com
This simply works for me in Emscripten 1.32.0. Disable shared libraries when configuring, because those are not supported by Emscripten. I used "emconfigure ./configure --disable-shared". The library is generated at .libs/libjpeg.a. Then I added main() to example.c:
int main(void) {
    printf("got %i\n", read_JPEG_file("testimg.jpg"));
    return 0;
}
I also commented out "put_scanline_someplace(buffer[0], row_stride);" because it doesn't exist and added "#include <stdlib.h>". Finally, I built it with: 
emcc --preload-file testimg.jpg example.c .libs/libjpeg.a -o example.html

Björn K.

unread,
May 5, 2015, 8:13:51 AM5/5/15
to emscripte...@googlegroups.com
Thanks, that works for me. Until I read http://mozakai.blogspot.de/2012/03/howto-port-cc-library-to-javascript.html I had no clue how to get a proper build. Thanks for the --disable-shared-tip!
Reply all
Reply to author
Forward
0 new messages