[Rcpp-devel] Using Rcpp without R in a main() function

Josh.Bowden at csiro.au Josh.Bowden at csiro.au
Wed Nov 7 10:12:23 CET 2012


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,

More information about the Rcpp-devel mailing list