将libR(来自R统计包)编译为java+jni的独立C库

问题描述 投票:0回答:1

我正在尝试将 R 编译为带有

-fPIC
标志的静态库,以便我可以在 java+JNI 中使用它(只有这可能吗?),但我在“.configure”中找不到正确的标志这样编译 R。

我测试了各种标志,但找不到正确的语法。

目前,我最后的尝试是

rm -rvf  "TMP/R-4.3.2" TMP/tmp.tar.gz
mkdir -p TMP/R-4.3.2/lib/
wget -O TMP/tmp.tar.gz "https://pbil.univ-lyon1.fr/CRAN/src/base/R-4/R-4.3.2.tar.gz"
cd TMP && tar xfz tmp.tar.gz && rm tmp.tar.gz &&  cd R-4.3.2 && \
    CPICFLAGS=fpic FPICFLAGS=fpic CXXPICFLAGS=fpic SHLIB_LDFLAGS=shared  SHLIB_CXXLDFLAGS=shared  ./configure --enable-R-static-lib --prefix=/path/to/TMP --with-x=no --disable-BLAS-shlib && make

witch 在配置过程中出现以下错误:

configure: WARNING: you cannot build info or HTML versions of the R manuals
configure: WARNING: you cannot build PDF versions of the R manuals
configure: WARNING: you cannot build PDF versions of vignettes and help pages
make[1]: Entering directory 'R-4.3.2'
configure.ac:278: error: possibly undefined macro: AM_CONDITIONAL
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
configure.ac:870: error: possibly undefined macro: AC_DISABLE_STATIC
configure.ac:2226: error: possibly undefined macro: AM_LANGINFO_CODESET
configure.ac:2876: error: possibly undefined macro: AM_NLS
configure.ac:2880: error: possibly undefined macro: AM_GNU_GETTEXT_VERSION
configure.ac:2881: error: possibly undefined macro: AM_GNU_GETTEXT
make[1]: *** [Makefile:49: configure] Error 1

删除 XXXFLAGS=YYY 和 --prefix (?) 允许编译 R,但它不会加载到 java 中。

gcc  -ITMP -I${JAVA_HOME}/include/ -I${JAVA_HOME}/include/linux \
    -LTMP/R-4.3.2/lib `TMP/R-4.3.2/bin/R CMD config --cppflags` -shared -fPIC -o TMP/libRSession.so  -g RSession.c TMP/R-4.3.2/lib/libR.a
/usr/bin/ld: TMP/R-4.3.2/lib/libR.a(objects.o): warning: relocation against `R_dot_Method' in read-only section `.text'
/usr/bin/ld: TMP/R-4.3.2/lib/libR.a(altrep.o): relocation R_X86_64_PC32 against symbol `R_NilValue' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value

有什么想法吗?谢谢

r java-native-interface static-linking configure fpic
1个回答
1
投票

您可以使用

--enable-R-shlib
标志将 R 编译为共享库。然后你将你的 JNI 库链接到它。

在运行时,R 库必须存在,但您可以将版本与 JNI 库一起发布。为了更加方便,您可以使用

-rpath
链接器选项来确保 JNI 库可以找到 R 库,无论这两个文件是在哪里提取的。

© www.soinside.com 2019 - 2024. All rights reserved.