如何使用 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.
检查文档,我看到我必须"构建和链接"库:
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
相关文章:
- 绕过 Windows 中的 __declspec(dllimport) 到 Linux 项目的转换
- 如何将我的小程序转换为用于项目的函数?
- 将Qt Designer项目转换为C++代码
- 将 VS 32 位项目转换为 64
- 如何使用 emscripten 将使用 zlib 的项目转换为 javascript?
- 将QMAKE项目转换为CMAKE
- 为什么这个GitHub项目将字符串转换为bool
- C/C++:是否可以将 Eclipse 项目设置配置为接受 void 指针而不对所有 Linux 发行版进行强制转换
- 从多字节字符集转换旧的Visual Studio C++项目
- Winforms-将Visual C 项目UI转换为Visual C#的任何方法
- 用中断函数将Keil5C项目转换为C++项目错误
- 将Qt项目转换为VC++项目 - 未解析的外部符号
- 适当的JNI转换将数组项目从jobjectarray到分开const char
- 在Visual Studio 2010中将控制台项目转换/移植到无形应用程序
- 使用ITK或OpenCV库将现有的VC++项目转换为DLL是可能的吗
- 在不引入错误的情况下转换项目
- 创建进程在转换项目以支持 Unicode 后不运行 .cmd 文件
- 您可以轻松地在Qt Creator和Eclipse之间来回转换c++项目吗?
- VS 2010智能感知不适用于上转换项目
- Visual Studio 2010 2005解决方案的转换(c++项目)失去了项目依赖关系