【问题标题】:C++ / R: RInside in Windows 7 machineC++/R:Windows 7 机器中的 RInside
【发布时间】:2016-07-02 06:30:11
【问题描述】:

此问题与:C++ and R: Create a .so or .dll 相关,并且我已阅读这些帖子的问题和回复:

  1. Compiling RInside programs on Windows
  2. Problem with compiling RInside examples under Windows

我尝试运行提供的答案中作为示例提供的代码

#include <RInside.h>                    // for the embedded R via RInside
#include <iomanip>

int main(int argc, char *argv[]) {

  RInside R(argc, argv);              // create an embedded R instance 

  std::string txt =                   // load library, run regression, create summary
    "suppressMessages(require(stats));"     
    "swisssum <- summary(lm(Fertility ~ . , data = swiss));" 
    "print(swisssum)";             
  R.parseEvalQ(txt);                  // eval command, no return

  // evaluate R expressions, and assign directly into Rcpp types
  Rcpp::NumericMatrix     M( (SEXP) R.parseEval("swcoef <- coef(swisssum)"));                 
  Rcpp::StringVector cnames( (SEXP) R.parseEval("colnames(swcoef)"));
  Rcpp::StringVector rnames( (SEXP) R.parseEval("rownames(swcoef)")); 

  std::cout << "\n\nAnd now from C++\n\n\t\t\t";
  for (int i=0; i<cnames.size(); i++) {
    std::cout << std::setw(11) << cnames[i] << "\t";
  }
  std::cout << std::endl;
  for (int i=0; i<rnames.size(); i++) {
    std::cout << std::setw(16) << rnames[i] << "\t";
    for (int j=0; j<cnames.size(); j++) {
      std::cout << std::setw(11) << M(i,j) << "\t";
    }
    std::cout << std::endl;
  }
  std::cout << std::endl;

  exit(0);
}

CMD中的错误如下

C:\Users\DON\Desktop>R CMD SHLIB final.cpp
g++ -m64 -I"C:/R/R-3.2.4/include" -DNDEBUG     -I"d:/RCompile/r-compiling/local/
local323/include"     -O2 -Wall  -mtune=core2 -c final.cpp -o final.o
final.cpp:1:74: fatal error: RInside.h: No such file or directory
compilation terminated.
make: *** [final.o] Error 1
Warning message:
comando ejecutado 'make -f "C:/R/R-3.2.4/etc/x64/Makeconf" -f "C:/R/R-3.2.4/shar
e/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)
' SHLIB="final.dll" WIN=64 TCLBIN=64 OBJECTS="final.o"' tiene estatus 2

显然它找不到RInside.h 标头。我将 R 安装在没有空格的文件夹中。全局变量中的PATH有:C:\R\R-3.2.4\bin; C:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin

我知道在 CMD 中我不能引入类似的命令

$ export PKG_LIBS=‘Rscript -e "Rcpp:::LdFlags()"‘ # if Rcpp older than 0.11.0
$ export PKG_CXXFLAGS=‘Rscript -e "Rcpp:::CxxFlags()"‘

首先定义并导出 R CMD SHLIB 然后依赖的两个相关环境变量(如放在FAQ file 中)

对此有何建议?我需要为每个要编译的cpp 文件做一个Makefile

【问题讨论】:

    标签: c++ r windows compilation rcpp


    【解决方案1】:

    错误在于您的方法。你做到了

    R CMD SHLIB final.cpp
    

    这是 nowhere 作为使用 RInside 的正确方法给出的。

    因为我们需要告诉 R 一些组件的头文件和库,你应该这样做

    cd inst/examples/standard
    make                    # build all
    

    make rinside_sample3    # build just this
    

    或者,如果您使用的是该操作系统,

    make -f Makefile.win    # all
    

    make -f Makefile.win rinside_sample3
    

    Makefile 告诉 R 在哪里可以找到它。这也回答了您的第二个问题:每个目录一个 Makefile 就可以了。 看看Makefile:它设置了几个包含指令;你的方法只处理了 Rcpp,所以当然你会得到一个关于 RInside.h not found 的错误。

    我想你会一遍又一遍地问同样的问题。

    【讨论】:

    • 输出是 exe 而不是 dll。那么当你在 C++ 中有 R 代码时,就不能制作 dll 或 .so 了吗?
    • 请重新阅读 RInside 的用途,以及 Rcpp 和其他扩展的用途。从 RInside 获取 .exe 是一项非常重要的功能。
    • 也许我需要买你的书 :) 谢谢