C++/R : RInside in Windows 7 machine

标签 c++ r windows compilation rcpp

这个问题是关于: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 header 。我将 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 吗?

最佳答案

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

R CMD SHLIB final.cpp

这是 无处 作为处理 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:它设置了几个 include 指令;您的方法只处理 Rcpp,所以当然您会收到有关 RInside.h not found 的错误。

我认为您一直在一遍又一遍地问同一个问题。

关于C++/R : RInside in Windows 7 machine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36034628/

相关文章:

c++ - C++ 中的稀疏非负最小二乘

c++ - 关于多态性应该了解的 C++ 知识有哪些

c++ - 在 C++ 中正确使用 "interfaces"?

selectInput 中的 R Shiny 自定义图标/图像

r - R中的垃圾收集com对象

r - 对 4 个因素的组合迭代 3 向交互 ggplot 调用

c - 如何为二进制和 ascii 文件定义 EOF

windows - VBScript 关闭消息困惑

java - Windows 在生成许多图像的 Java 代码执行期间(或之后)挂起

c++ - CRITICAL_SECTION 用于设置和获取单个 bool 值