c-使用Emscripten将R函数编译为JavaScript

c - Compiling R functions to JavaScript with Emscripten

本文关键字:编译 JavaScript 函数 使用 Emscripten      更新时间:2024-01-14

我正在尝试使用Emscripten将一些用C编写的R函数编译为JavaScript。我的第一个任务是移植一个名为pf的函数。

来源可以在这里找到。因此,我进入src目录并尝试运行:

(trunk)⚡ % emcc -s EXPORTED_FUNCTIONS="['_Rf_pf']" nmath/pf.c
warning: unresolved symbol: Rf_pbeta
warning: unresolved symbol: Rf_pchisq
warning: unresolved symbol: R_NaN
warning: unresolved symbol: R_NegInf
warning: unresolved symbol: R_PosInf

我在输出JavaScript中得到一个函数_Rf_pf。我实际上可以调用这个函数,它会返回一个结果。但是,由于R_PosInf和公司没有解决,它在R_P_bounds_01(x, 0., ML_POSINF);上短路。CCD_ 5以某种方式被设置为0,从而给出奇怪的结果。因此,算法的核心没有被执行。

有人知道我如何解决这些符号并移植这个函数吗?

我可以尝试编译更多的源代码,这似乎让我有所收获:

$ emcc -s EXPORTED_FUNCTIONS="['_Rf_pf']" nmath/pf.c nmath/pbeta.c nmath/pchisq.c 
warning: unresolved symbol: R_finite
warning: unresolved symbol: Rf_pgamma
warning: unresolved symbol: Rf_warning
warning: unresolved symbol: bratio
warning: unresolved symbol: gettext
warning: unresolved symbol: R_NaN
warning: unresolved symbol: R_NegInf
warning: unresolved symbol: R_PosInf

但是,这似乎把我带进了一个巨大的兔子洞。然后,我遇到了一些障碍:

 $ emcc -s EXPORTED_FUNCTIONS="['_Rf_pf']" nmath/pf.c nmath/pbeta.c nmath/pchisq.c main/arithmetic.c 
In file included from main/arithmetic.c:35:
include/Defn.h:201:3: error: SIZE_MAX is required for C99
# error SIZE_MAX is required for C99
  ^
include/Defn.h:639:9: error: unknown type name 'R_size_t'
extern0 R_size_t R_NSize  INI_as(R_NSIZE);/* Size of cons cell heap */
        ^
include/Defn.h:640:9: error: unknown type name 'R_size_t'
extern0 R_size_t R_VSize  INI_as(R_VSIZE);/* Size of the vector heap */
        ^
...
fatal error: too many errors emitted, stopping now [-ferror-limit=]

注意,我有CPATH设置。我不知道这是否是将编译器指向我需要的不同标头的方法:

(trunk)⚡ % echo $CPATH
gnuwin32/fixed/h/:/usr/local/Cellar/r/3.1.2_1/include/:/usr/local/Cellar/r/3.1.2_1/R.framework/Versions/3.1/Resources/include/:nmath/:include/:main/:include/R_ext

如果要使用gnuwin32/fixed/h/config.h,则需要-DHAVE_CONFIG_H

https://github.com/wch/r-source/blob/trunk/src/gnuwin32/Makefile