-1

I have tried this cpp code with Rcpp header.

#include <Rcpp.h>
using namespace Rcpp;

// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp 
// function (or via the Source button on the editor toolbar). 


// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
    return x * 2;
}



/*** R
timesTwo(42)
*/

First i have compiled like g++ -I/usr/share/R/include -I/usr/lib/R/site-library/Rcpp/include -c rcpp.cpp . Later i used g++ -I/usr/share/R/include -I/usr/lib/R/site-library/Rcpp/include -c rcpp.cpp -L/usr/share/R/include -L/usr/lib/R/site-library/Rcpp/include for linking. But both creates a object file. But i need to show the output.

  1. For making object file executable, i used chmod u+x rcpp.o command.
  2. For unning ./rcpp But this creates error.bash: ./reg: No such file or directory But i am compile the code from the same directory itself. Anybody know how to resolve the issue.
9113303
  • 852
  • 1
  • 16
  • 30
  • 1
    The object file is not executable. Try `g++ -I/usr/share/R/include -I/usr/lib/R/site-library/Rcpp/include rcpp.cpp -L/usr/share/R/include -L/usr/lib/R/site-library/Rcpp/include -o rcpp`. Parameter `-c` is for compile only and creates object files. Without `-c` the compiler will compile and link in one step. – Thomas Sablik Jul 02 '18 at 07:24
  • Getting some undefined references like _reg.cpp:(.text._ZN4Rcpp10RstreambufILb0EE6xsputnEPKcl[_ZN4Rcpp10RstreambufILb0EE6xsputnEPKcl]+0x2c): undefined reference to `REprintf' /tmp/cclCOgI4.o: In function `Rcpp::Rstreambuf::overflow(int)': reg.cpp:(.text._ZN4Rcpp10RstreambufILb1EE8overflowEi[_ZN4Rcpp10RstreambufILb1EE8overflowEi]+0x2b): undefined reference to `Rprintf' /tmp/cclCOgI4.o: In function `Rcpp::Rstreambuf::overflow(int)': reg.cpp:(.text._ZN4Rcpp10RstreambufILb0EE8overflowEi[_ZN4Rcpp10RstreambufILb0EE8overflowEi]+0x2b): undefined reference to `REprintf' – 9113303 Jul 02 '18 at 07:41
  • 2
    Did you try to use `sourceCpp()` from your R script? – Thomas Sablik Jul 02 '18 at 08:50
  • This same code could be run in R script. as a result will get, Rcpp::sourceCpp('Desktop/R_TENS/eg.cpp') > timesTwo(42) [1] 84 – 9113303 Jul 02 '18 at 08:53
  • 1
    Functions like this are meant to be called from R! Why do you want to compile it into a stand-alone program? – Ralf Stubner Jul 02 '18 at 09:22
  • C/Cpp codes could be run by visual studio,code blocks etc. I am just writing codes on gedit and compile it by gcc/g++ compiler. Then i faced the above problem regarding the object file. – 9113303 Jul 02 '18 at 09:26
  • I understand **what** you are doing. I do not understand **why** you are doing it. To me it looks like using a screw driver instead of a hammer for driving in nails. – Ralf Stubner Jul 02 '18 at 09:51
  • I understood. But i am trying to call Some R code from Cpp program. I'm a beginner in this area. I thought like Rcpp and Rinside will be used for this. – 9113303 Jul 02 '18 at 10:03
  • 4
    See my answer to your other question: https://stackoverflow.com/questions/51041271/how-to-call-a-r-function-from-c-with-passing-the-parameters/ Does that help you? – Ralf Stubner Jul 02 '18 at 11:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174160/discussion-between-9113303-and-ralf-stubner). – 9113303 Jul 02 '18 at 11:17
  • yes, Thanks for your immediate response. – 9113303 Jul 02 '18 at 11:17

1 Answers1

4

Just re-read the very segment you quoted. It contains these three very important lines:

// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp 
// function (or via the Source button on the editor toolbar). 

That means in R you are supposed to do

Rcpp::sourceCpp("theFileYouSave.cpp")

or if you displike :: load the Rcpp package first:

library(Rcpp)
sourceCpp("theFileYouSave.cpp")

or do as the comment says (if you use RStudio) and press the `source' button.

Nowhere does it suggest to hit g++ directly.

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