hansioux hansioux - 8 months ago 146
R Question

Undefined reference errors when including Rcpp.h

I am using 64bit Ubuntu, and I am trying to write C++.

I discovered if I use

#include <Rcpp.h>
, I don't even need to call any functions in the R namespace, and I would already receive undefired reference errors:

obj/x.o: In function `Rcpp::Rstreambuf<true>::xsputn(char const*, long)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:61: undefined reference to `Rprintf'
obj/x.o: In function `Rcpp::Rstreambuf<false>::xsputn(char const*, long)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:65: undefined reference to `REprintf'
obj/x.o: In function `Rcpp::Rstreambuf<true>::overflow(int)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:70: undefined reference to `Rprintf'
obj/x.o: In function `Rcpp::Rstreambuf<false>::overflow(int)':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:74: undefined reference to `REprintf'
obj/x.o: In function `Rcpp::Rstreambuf<true>::sync()':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:79: undefined reference to `R_FlushConsole'
obj/x.o: In function `Rcpp::Rstreambuf<false>::sync()':
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/iostream/Rstreambuf.h:83: undefined reference to `R_FlushConsole'


I have installed r-base and r-base-dev. I installed Rcpp by running R as root and did a
install.package("Rcpp")


I compile the C++ program using g++ with
-I/usr/local/lib/R/site-library/Rcpp/include


What am I missing here? Thanks for any replies.

Answer Source

Just pulling Rcpp headers is not enough. You also need R headers and linking against R's library. You could use e.g. R CMD SHLIB to do this for you.

However, I'd suggest you either:

  • create a package that has LinkingTo: Rcpp etc ... (see Rcpp's documentation).
  • use sourceCpp on your .cpp file. See ?sourceCpp
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download