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.