如何使用 emscripten 将使用 zlib 的项目转换为 javascript?

How to convert a project that uses zlib into javascript using emscripten?

我正在尝试使用 emscripten 将 C++ 程序转换为 javascript。当我尝试在项目目录中 运行 emconfigure ./configure 时,我得到 configure: error: Zlib not found.

的错误

查看文档我发现我必须 "build and link" 库:

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

但我不确定如何解释这一点。我可以从官方 github 存储库克隆 zlib 然后尝试构建它,配置步骤工作正常但我在 make 步骤中收到以下错误:

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

然后我注意到 emscripten 测试目录有一个 zlib 的副本(一个旧版本,看起来像)。我尝试构建它并且它有效。它 returns 一堆 *.o 文件(目标文件?)我假设是位码,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

文档说我现在应该 link 项目的库,但我不完全理解这意味着什么。他们的示例没有显示任何类型的 linking 它似乎首先构建库,然后是项目(我试过,它给出了相同的错误,正如预期的那样),然后用 emcc 编译。

我希望一旦我 link 将库添加到项目(同样,我不确定他们的意思)并使用 emmake 构建项目,我将 运行 emcc project libz.so.1.2.5 -o project.js 这将给我编译为 javascript.

的程序

Link指的是标准linking of libraries.

我整理了一个简单的emscripten程序,使用zlib来演示。

#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;
}

编译命令正常启动:

emcc main.c

然后你必须告诉它从哪里获取包含文件和库

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

然后实际上 link 图书馆

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

并输出HTML:

-o test.html

我的命令总共是

emcc main.c -I${ZLIB_DIR} -L${ZLIB_DIR} -lz -o test.html