5

好吧,这让我发疯了。我已经尝试了至少一个月,互联网上没有任何帮助。

我按照这个的步骤。当我执行这些步骤时,甚至该示例都不起作用,因为当我这样做时,我得到了这个。

bitcode ==> javascript
warning: unresolved symbol: __gmpz_cmp
warning: unresolved symbol: __gmpz_mul_ui
warning: unresolved symbol: __gmpz_submul_ui
warning: unresolved symbol: __gmpz_init_set_ui
warning: unresolved symbol: __gmpz_mul_2exp
warning: unresolved symbol: __gmpz_init
warning: unresolved symbol: __gmpz_fdiv_qr
warning: unresolved symbol: __gmpz_add

当我运行生成的 complete.js 文件时 -

missing function: __gmpz_init
-1
-1

/home/ubuntu/workspace/gmp.js/complete.js:117
      throw ex;
      ^
abort(-1) at Error
    at jsStackTrace (/home/ubuntu/workspace/gmp.js/complete.js:1045:13)
    at stackTrace (/home/ubuntu/workspace/gmp.js/complete.js:1062:22)
    at abort (/home/ubuntu/workspace/gmp.js/complete.js:6743:44)
    at ___gmpz_init (/home/ubuntu/workspace/gmp.js/complete.js:1744:56)
    at Object._main (/home/ubuntu/workspace/gmp.js/complete.js:4978:2)
    at Object.callMain (/home/ubuntu/workspace/gmp.js/complete.js:6627:30)
    at doRun (/home/ubuntu/workspace/gmp.js/complete.js:6681:60)
    at run (/home/ubuntu/workspace/gmp.js/complete.js:6695:5)
    at Object.<anonymous> (/home/ubuntu/workspace/gmp.js/complete.js:6769:1)
    at Module._compile (module.js:541:32)
If this abort() is unexpected, build with -s ASSERTIONS=1 which can give more information.
4

3 回答 3

9

这些说明适用于运行 amd64 Debian Buster 的主机。似乎 GMP 不再需要 32 位来与 Emscripten 一起工作(无论如何似乎不再支持 32 位 Emscripten?),但我使用 chroot 来清洁环境。安装后,我的 chroot 大小为 1.6GB。但如果可以避免的话,我不建议将它用于计算密集型代码,在一个基准测试中,我的本机代码比在 nodejs 中运行 Emscripten 编译的相同代码快 15 倍......

Debian Buster chroot

mkdir emscripten
sudo debootstrap buster emscripten
sudo chroot emscripten /bin/bash
echo "deb http://security.debian.org/debian-security buster/updates main" >> /etc/apt/sources.list
apt update
apt install python cmake g++ git lzip wget nodejs m4
echo "none /dev/shm tmpfs rw,nosuid,nodev,noexec 0 0" >> /etc/fstab
mount /dev/shm
echo "none /proc proc defaults 0 0" >> /etc/fstab
mount /proc
adduser emscripten
su - emscripten

emsdk 最新

在撰写本文时,已安装 releases-upstream-b024b71038d1291ed7ec23ecd553bf2c0c8d6da6-64bit 并且node-12.9.1-64bit

git clone https://github.com/juj/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
mkdir -p ${HOME}/opt/src
cd ${HOME}/opt/src

GMP 6.1.2

wget https://gmplib.org/download/gmp/gmp-6.1.2.tar.lz
tar xf gmp-6.1.2.tar.lz
cd gmp-6.1.2
emconfigure ./configure --disable-assembly --host none --enable-cxx --prefix=${HOME}/opt
make
make install
cd ..

mpfr 4.0.2

wget https://www.mpfr.org/mpfr-current/mpfr-4.0.2.tar.xz
wget https://www.mpfr.org/mpfr-current/allpatches
tar xf mpfr-4.0.2.tar.xz
cd mpfr-4.0.2
patch -N -Z -p1 < ../allpatches 
emconfigure ./configure --host none --prefix=${HOME}/opt --with-gmp=${HOME}/opt
make
make install
cd ..

mpc 1.1.0

wget https://ftp.gnu.org/gnu/mpc/mpc-1.1.0.tar.gz
tar xf mpc-1.1.0.tar.gz
cd mpc-1.1.0
emconfigure ./configure --host none --prefix=${HOME}/opt --with-gmp=${HOME}/opt --with-mpfr=${HOME}/opt
make
make install
cd ..

你好世界

您最喜欢的使用 GMP/MPFR/MPC 的程序:

emcc -o hello.js hello.c \
  ${HOME}/opt/lib/libmpc.a ${HOME}/opt/lib/libmpfr.a ${HOME}/opt/lib/libgmp.a
nodejs hello.js
于 2017-04-24T08:24:54.933 回答
2

我发现要这样做,您需要使用 32 位机器。我有一台 64 位机器,所以我使用本教程chroot 到 32 位文件系统。

在那之后,一切都很顺利。我正在使用 GMP 和 MPFR 制作 Mandelbrot 程序,并将编译脚本(连同程序本身)在线发布在 GitHub 上。在这里。适应您自己的项目。

于 2016-12-24T12:20:56.630 回答
0

我将它打包到一个名为gmp-wasm的 NPM 库中。您可以在 source 中找到构建库的 Dockerized代码。它同时导出低级函数和不可变的高级包装器:

<script src="https://cdn.jsdelivr.net/npm/gmp-wasm"></script>
<script>
  gmp.init().then(({
    calculate
  }) => {
    // calculate() automatically deallocates all objects
    // created within the callback function
    const result = calculate((g) => {
      const six = g.Float(1).add(5);
      const res = g.Pi().div(six).sin();
      return res;
    });
    document.write(`sin(Pi/6) = ` + result);
  });
</script>

使用低级函数:

<script src="https://cdn.jsdelivr.net/npm/gmp-wasm"></script>
<script>
  gmp.init().then(({
    calculate, binding
  }) => {
    const result = calculate((g) => {
      const a = g.Float(1);
      const b = g.Float(2);
      const c = g.Float(0);
      // c = a + b
      binding.mpfr_add(c.mpfr_t, a.mpfr_t, b.mpfr_t, 0);
      return c;
    });
    document.write(`1 + 2 = ` + result);
  });
</script>

于 2021-11-29T11:38:05.137 回答