0

This question is related to: C++ and R: Create a .so or .dll plus i have read the questions and replies of these posts:

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

I try to run the code provided as an example in the answer provided

#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);
}

The error in the CMD is the following

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

Clearly it cant find the RInside.h header. I have the R installed in a folder without spaces. The PATH in global variables have: C:\R\R-3.2.4\bin; C:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin

I understand that in the CMD i cant introduce comands like

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

Which first defines and exports two relevant environment variables which R CMD SHLIB then relies on (as put in the FAQ file)

Any advice on this? I need to do a Makefile for each cpp file that i want to compile?

Community
  • 1
  • 1
donpresente
  • 1,230
  • 2
  • 13
  • 24

1 Answers1

3

The error is in your approach. You did

R CMD SHLIB final.cpp

which is nowhere given as the correct approach for working with RInside.

Because we need to tell R about headers and libraries for several components, you are supposed to

cd inst/examples/standard
make                    # build all

or

make rinside_sample3    # build just this

or, if you're on that OS,

make -f Makefile.win    # all

or

make -f Makefile.win rinside_sample3

as the Makefile tells R where do find this. That also answers your second question: One Makefile per directory will do. And look at the Makefile: it sets several include directives; your approach only dealt with Rcpp so of course you get an error about RInside.h not found.

I think you keep asking the same question over and over.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725