使用Emscripten将R函数编译为JavaScript [英] Compiling R functions to JavaScript with Emscripten
问题描述
我正在尝试使用Emscripten将一些用C编写的R函数编译为JavaScript.我的第一个任务是移植一个名为 pf
的函数.
I'm trying to compile some R functions, written in C, to JavaScript using Emscripten. My first mission is to port a function called pf
.
The source can be found here. So, I go into the src directory and try running:
(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);
上短路.将 ML_POSINF
设置为0会给出奇怪的结果.因此,算法的核心并未执行.
I get a function _Rf_pf
in the output JavaScript. I can actually call this function and it returns a result. But, because R_PosInf
and company are not resolved, it short circuits on R_P_bounds_01(x, 0., ML_POSINF);
. ML_POSINF
is somehow set to 0 giving weird results. So, the heart of the algorithm is not executed.
有人知道我如何解决这些符号并移植此功能吗?
Does anyone know how I can get these symbols resolved and get this function ported?
我可以尝试编译更多的源代码,这似乎使我有所了解:
I can try to compile more source and that seems to get me somewhere:
$ 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
但是,这似乎使我跌入了一个巨大的兔子洞.然后,我遇到了一些障碍:
But, this seems to lead me down a huge rabbit hole. And, then, I hit some snags:
$ 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.我不知道这是否是将编译器指向我需要的不同标头的方法:
Note that I have CPATH set. I don't know if this is the way to go about pointing the compiler to different headers I need:
(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-来源/blob/trunk/src/gnuwin32/Makefile
这篇关于使用Emscripten将R函数编译为JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!