2

I'm trying to convert a c++ program into javascript using emscripten. When I try to run emconfigure ./configure in the project directory I get the error that configure: error: Zlib not found.

Checking the documentation I see that I must "build and link" the library:

If your project uses other libraries, for example zlib or glib, you will need to build and link them. The normal approach is to build the libraries to bitcode and then compile library and main program bitcode together to JavaScript.

For example, consider the case where a project “project” uses a library “libstuff”:

# Compile libstuff to bitcode
./emconfigure ./configure
./emmake make

# Compile project to bitcode
./emconfigure ./configure
./emmake make

# Compile the library and code together to HTML
emcc project.bc libstuff.bc -o final.html

But I'm not sure how to interpret this. I can clone zlib from the official github repo and then try to build it, the configuration step works fine but I get the following error for the make step:

error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: adler32.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: crc32.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: deflate.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: infback.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: inffast.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: inflate.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: inftrees.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: trees.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: zutil.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: compress.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: uncompr.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: gzclose.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: gzlib.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: gzread.o is not an object file (not allowed in a library)
error: /Library/Developer/CommandLineTools/usr/bin/libtool: file: gzwrite.o is not an object file (not allowed in a library)
make: *** [libz.a] Error 1

So then I noticed that the emscripten test directory has a copy of zlib (an older version, it looks like). I tried building this instead and it works. It returns a bunch of *.o files (object files?) and what I assume is the bitcode, libz.so.1.2.5:

adler32.o   deflate.o   gzclose.o   gzwrite.o   inflate.o   libz.so.1   minigzip64.o    zutil.o
compress.o  example.o   gzlib.o     infback.o   inftrees.o  libz.so.1.2.5   trees.o
crc32.o     example64.o gzread.o    inffast.o   libz.a      minigzip.o  uncompr.o

The documentation says I should now link the library to the project, but I don't fully understand what this means. Their example doesn't show any kind of linking it just seems to build the library first, then the project (which I tried, it gives the same error, as expected), then compile with emcc.

I expect that once I link the library to the project (again, I'm not sure what they mean by this) and build the project with emmake, I will run emcc project libz.so.1.2.5 -o project.js and this will give me the program compiled as javascript.

Dianne
  • 166
  • 1
  • 13
  • Building zlib with emconfigure/emmake worked fine for me. Can you show how you're trying to build? –  Aug 17 '19 at 14:31
  • Are you talking about the official zlib distribution (from the github) or the one included with emscripten? The latter does build as shown above, it's the only the former that doesn't. My problem is with what to do with the `libz.so.1.2.5` file afterwards and what "linking" it to my project means. – Dianne Aug 22 '19 at 10:17

1 Answers1

1

Link refers to the standard linking of libraries.

I put together a simple emscripten program that uses zlib to demonstrate.

#include "zlib.h"
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    uLong crc = crc32(0L, Z_NULL, 0);
    crc = crc32(crc, "Hello, world", strlen("Hello, world"));
    printf("CRC is %lu\n", crc);
    return 0;
}

The compile command starts as normal:

emcc main.c

Then you have to tell it where to pick up the include files and the library

-I${EM_HOME}/tests/zlib -L${EM_HOME}/tests/zlib

And then actually link the library

-lz  # Tells to look for something named `libz.*` and link with it

And output HTML:

-o test.html

Altogether my command was

emcc main.c -I${ZLIB_DIR} -L${ZLIB_DIR} -lz -o test.html
  • 1
    I ended up running `./configure` by itself (not using emconfigure) to generate the makefile, omitting the `-lz` from the Makefile and then using the port functionality of emscripten with `-s USE_ZLIB=1` when running emcc – Dianne Sep 16 '19 at 01:05