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

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

本文关键字:转换 项目 javascript zlib emscripten 何使用      更新时间:2023-10-16

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

检查文档,我看到我必须"构建和链接"库:

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 的副本(看起来像一个旧版本(。我尝试构建它,它可以工作。它返回一堆 *.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

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

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

链接是指库的标准链接。

我整理了一个简单的 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 %lun", crc);
return 0;
}

编译命令正常启动:

emcc main.c

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

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

然后实际链接库

-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