[Rcpp-devel] Using Rcpp without R in a main() function
Romain Francois
romain at r-enthusiasts.com
Wed Nov 7 10:22:22 CET 2012
Hello,
Rcpp is for calling C++ code from R. Why did you think you can use it
without R ?
Have a look at RInside, which will let you embed R in a c++ application.
But no miracle there either, you will still need R.
Romain
Le 07/11/12 10:12, Josh.Bowden at csiro.au a écrit :
> Hi,
>
> I am trying to test some code and in doing so I want to call the code from a main() function. The test code is as follows:
>
> #include <Rinternals.h>
> #include <Rcpp.h>
> #include <iostream>
> #include <fstream>
>
> int main(int argc, char* argv[])
> {
> Int arraySize = 1689 ;
> // read in the test data
> // Rcpp::NumericVector Avec(arraySize * arraySize) ; // code fails here on initialisation
> std::vector<double> Amat(arraySize * arraySize) ;
> std::ifstream arrayData("/home/array.bin", std::ios::in | std::ios::binary); // File Open
> if(!arrayData){Rcpp::Rcout << "ERROR";exit(1);}
> for(int t1=0; t1 < arraySize; t1++)
> {
> for(int t2=0; t2 < arraySize; t2++)
> {
> // arrayData << Amat(t1,t2) << "\t" ;
> double tdouble ;
> arrayData.read((char*) &tdouble, sizeof(double) ) ; //Reads array from binary file
> Amat[t1 * arraySize + t2] = tdouble ;
> }
> }
> arrayData.close() ;
>
> Rcpp::NumericVector xx(arraySize * arraySize); // or code fails here on initialisation
>
> return 0 ;
>
> }
>
> I compile using the following:
> icpc -I/apps/R/2.15.1/lib64/R/include -I"/home/bow355/R/library/Rcpp/include" -O0 -g -fp-model precise main.cpp -L/home/bow355/R/library/Rcpp/lib -lRcpp
>
> When I run the executable it seg faults at the initialisation of Rcpp::NumericVector objects.
> I have tested the function body with inline and it compiles and runs perfectly.
>
> Is there something I should specify so that the Rcpp library objects initialise correctly?
>
> Thanks a lot for any feedback,
> Josh
--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
R Graph Gallery: http://gallery.r-enthusiasts.com
`- http://bit.ly/SweN1Z : SuperStorm Sandy
blog: http://romainfrancois.blog.free.fr
|- http://bit.ly/RE6sYH : OOP with Rcpp modules
`- http://bit.ly/Thw7IK : Rcpp modules more flexible
More information about the Rcpp-devel
mailing list