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

标签 javascript c++ emscripten

我正在尝试使用 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 repo 克隆 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 项目 libz.so.1.2.5 -o project.js 这将给我编译为 javascript 的程序。

最佳答案

链接引用标准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

然后实际链接库

-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

关于javascript - 如何使用 emscripten 将使用 zlib 的项目转换为 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57488813/

相关文章:

c++ - typedef 一个指向指针的类型

c++ - v8 的替代方案,用于将 C++ 对象暴露给 JavaScript(在 PowerPC 上)

emscripten - WebAssembly 链接错误 : _sprintf function import requires a callable

javascript - Accordion 面板不应在 if 条件下打开 - coffeescript

javascript - Adm Zip 将文件压缩为目录

javascript - 为什么我在使用 Array concat 时不断收到 TypeScript 错误 "No overload matches this call"?

Javascript Onclick 事件错误 : $ is not defined

c++ - 如何找到包含在多态基类 vector 中的对象的类型?

javascript - 使用 Emscripten 编译 GMP/MPFR

c++ - 为什么在 ADL 中重载优先于显式特化