Rcpp installation error.

Hi I am having an issue installing Rcpp package:
I tried install.packages("Rcpp",dependecies=F) also tried with T but same error:

  • installing source package ‘Rcpp’ ...
    ** package ‘Rcpp’ successfully unpacked and MD5 sums checked
    ** using staged installation
    ** libs
    g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG -I../inst/include/ -include /usr/lib/R/site-library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp -I /usr/lib/R/site-library/StanHeaders/include -I /home/biostat_laptop/R/x86_64-pc-linux-gnu-library/3.6/RcppEigen/include -fpic -g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c api.cpp -o api.o
    In file included from :
    /usr/lib/R/site-library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:10: fatal error: Eigen/Dense: No such file or directory
    13 | #include <Eigen/Dense>
    | ^~~~~~~~~~~~~
    compilation terminated.
    make: *** [/usr/lib/R/etc/Makeconf:177: api.o] Error 1
    ERROR: compilation failed for package ‘Rcpp’
  • removing ‘/home/biostat_laptop/R/x86_64-pc-linux-gnu-library/3.6/Rcpp’
    Warning in install.packages :
    installation of package ‘Rcpp’ had non-zero exit status

The downloaded source packages are in
‘/tmp/Rtmpove0nY/downloaded_packages’
I navigated to the Makeconf file BUT NOT SURE what to change there. The contents are as follows:

etc/Makeconf. Generated from Makeconf.in by configure.

${R_HOME}/etc/Makeconf

R was configured using the following call

(not including env. vars and site configuration)

configure '--prefix=/usr' '--with-cairo' '--with-jpeglib' '--with-readline' '--with-tcltk' '--with-system-bzlib' '--with-system-pcre' '--with-system-zlib' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--datadir=/usr/share/R/share' '--includedir=/usr/share/R/include' '--with-blas' '--with-lapack' '--enable-R-profiling' '--enable-R-shlib' '--enable-memory-profiling' '--without-recommended-packages' '--build' 'x86_64-linux-gnu' 'build_alias=x86_64-linux-gnu' 'R_PRINTCMD=/usr/bin/lpr' 'R_PAPERSIZE=letter' 'TAR=/bin/tar' 'R_BROWSER=xdg-open' 'LIBnn=lib' 'JAVA_HOME=/usr/lib/jvm/default-java' 'R_SHELL=/bin/bash' 'CC=gcc -std=gnu99' 'CFLAGS=-g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g' 'LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro' 'CPPFLAGS=' 'FC=gfortran' 'FCFLAGS=-g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong' 'CXX=g++' 'CXXFLAGS=-g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g'

This fails if it contains spaces, or if it is quoted

include $(R_SHARE_DIR)/make/vars.mk

AR = ar
BLAS_LIBS = -lblas
C_VISIBILITY = -fvisibility=hidden
CC = gcc -std=gnu99
CFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
CPICFLAGS = -fpic
CPPFLAGS =
CXX = g++ -std=gnu++11

Not used by anything in R, in particular not for the .cc.d rule

CXXCPP = (CXX) -E CXXFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g (LTO)
CXXPICFLAGS = -fpic
CXX98 = g++
CXX98FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g (LTO) CXX98PICFLAGS = -fpic CXX98STD = -std=gnu++98 CXX11 = g++ CXX11FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g (LTO)
CXX11PICFLAGS = -fpic
CXX11STD = -std=gnu++11
CXX14 = g++
CXX14FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g (LTO) CXX14PICFLAGS = -fpic CXX14STD = -std=gnu++14 CXX17 = g++ CXX17FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g (LTO)
CXX17PICFLAGS = -fpic
CXX17STD = -std=gnu++17
CXX_VISIBILITY = -fvisibility=hidden
DYLIB_EXT = .so
DYLIB_LD = (CC) DYLIB_LDFLAGS = -shared -fopenmp# (CFLAGS) (CPICFLAGS) DYLIB_LINK = (DYLIB_LD) (DYLIB_LDFLAGS) (LDFLAGS)
ECHO = echo
ECHO_C =
ECHO_N = -n
ECHO_T =
F_VISIBILITY = -fvisibility=hidden

FC is the compiler used for all Fortran as from R 3.6.0

FC = gfortran
FCFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong $(LTO)

additional libs needed when linking with $(FC), e.g. on some Oracle compilers

FCLIBS_XTRA =
FFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong $(LTO)
FLIBS = -lgfortran -lm -lquadmath
FPICFLAGS = -fpic
FOUNDATION_CPPFLAGS =
FOUNDATION_LIBS =
JAR = /usr/lib/jvm/default-java/bin/jar
JAVA = /usr/lib/jvm/default-java/bin/java
JAVAC = /usr/lib/jvm/default-java/bin/javac
JAVAH =

JAVA_HOME might be used in the next three.

They are for packages 'JavaGD' and 'rJava'

JAVA_HOME = /usr/lib/jvm/default-java
JAVA_CPPFLAGS = -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
JAVA_LIBS = -L$(JAVA_HOME)/lib/server -ljvm
JAVA_LD_LIBRARY_PATH = $(JAVA_HOME)/lib/server
LAPACK_LIBS = -llapack
LDFLAGS = -Wl,-Bsymbolic-functions -Wl,-z,relro

we only need this is if it is external, as otherwise link to R

LIBINTL=
LIBM = -lm
LIBR0 = -L"(R_HOME)/lib(R_ARCH)"
LIBR1 = -lR
LIBR = -L"(R_HOME)/lib(R_ARCH)" -lR
LIBS = -lpcre2-8 -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -licuuc -licui18n

needed by R CMD config

LIBnn = lib
LIBTOOL = (SHELL) "(R_HOME)/bin/libtool"
LTO =

needed to build applications linking to static libR

MAIN_LD = (CC) MAIN_LDFLAGS = -Wl,--export-dynamic -fopenmp MAIN_LINK = (MAIN_LD) (MAIN_LDFLAGS) (LDFLAGS)
MKINSTALLDIRS = "(R_HOME)/bin/mkinstalldirs" OBJC = OBJCFLAGS = (LTO)
OBJC_LIBS =
OBJCXX =
R_ARCH =
RANLIB = ranlib
SAFE_FFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-jbaK_j/r-base-3.6.3=. -fstack-protector-strong -msse2 -mfpmath=sse
SED = /bin/sed
SHELL = /bin/bash
SHLIB_CFLAGS =
SHLIB_CXXFLAGS =
SHLIB_CXXLD = (CXX) SHLIB_CXXLDFLAGS = -shared SHLIB_CXX98LD = (CXX98) (CXX98STD) SHLIB_CXX98LDFLAGS = -shared SHLIB_CXX11LD = (CXX11) (CXX11STD) SHLIB_CXX11LDFLAGS = -shared SHLIB_CXX14LD = (CXX14) (CXX14STD) SHLIB_CXX14LDFLAGS = -shared SHLIB_CXX17LD = (CXX17) (CXX17STD) SHLIB_CXX17LDFLAGS = -shared SHLIB_EXT = .so SHLIB_FFLAGS = SHLIB_LD = (CC)
SHLIB_LDFLAGS = -shared# (CFLAGS) (CPICFLAGS)
SHLIB_LIBADD =

We want to ensure libR is picked up from $(R_HOME)/lib

before e.g. /usr/local/lib if a version is already installed.

SHLIB_LINK = (SHLIB_LD) (SHLIB_LDFLAGS) (LIBR0) (LDFLAGS)
SHLIB_OPENMP_CFLAGS = -fopenmp
SHLIB_OPENMP_CXXFLAGS = -fopenmp
SHLIB_OPENMP_FFLAGS = -fopenmp
STRIP_STATIC_LIB = strip --strip-debug
STRIP_SHARED_LIB = strip --strip-unneeded
TCLTK_CPPFLAGS = -I/usr/include/tcl8.6 -I/usr/include/tcl8.6
TCLTK_LIBS = -L/usr/lib/x86_64-linux-gnu -ltcl8.6 -L/usr/lib/x86_64-linux-gnu -ltk8.6 -lX11 -lXss -lXext
YACC = bison -y

Legacy settings: no longer used by R as of 3.6.0

Setting FC often sets F77 (on Solaris make even if set)

so must follow FC in this file.

F77 = gfortran
FCPICFLAGS = -fpic
F77_VISIBILITY = -fvisibility=hidden
SHLIB_FCLD = $(FC)
SHLIB_FCLDFLAGS = -shared
SHLIB_OPENMP_FCFLAGS = -fopenmp

for linking to libR.a

STATIC_LIBR = # -Wl,--whole-archive "(R_HOME)/lib(R_ARCH)/libR.a" -Wl,--no-whole-archive (BLAS_LIBS) (FLIBS) (LIBINTL) -lreadline (LIBS)

These are recorded as macros for legacy use in packages

set on AIX, formerly for old glibc (-D__NO_MATH_INLINES)

R_XTRA_CFLAGS =

was formerly set on HP-UX

R_XTRA_CPPFLAGS = -I"$(R_INCLUDE_DIR)" -DNDEBUG

currently unset

R_XTRA_CXXFLAGS =

used for gfortran in R > 3.6.0

R_XTRA_FFLAGS = -fno-optimize-sibling-calls

SHLIB_CFLAGS SHLIB_CXXFLAGS SHLIB_FFLAGS are apparently currently unused

SHLIB_CXXFLAGS is undocumented, there is no SHLIB_FCFLAGS

ALL_CFLAGS = (PKG_CFLAGS) (CPICFLAGS) (SHLIB_CFLAGS) (CFLAGS)
ALL_CPPFLAGS = -I"(R_INCLUDE_DIR)" -DNDEBUG (PKG_CPPFLAGS) (CLINK_CPPFLAGS) (CPPFLAGS)
ALL_CXXFLAGS = (PKG_CXXFLAGS) (CXXPICFLAGS) (SHLIB_CXXFLAGS) (CXXFLAGS)
ALL_OBJCFLAGS = (PKG_OBJCFLAGS) (CPICFLAGS) (SHLIB_CFLAGS) (OBJCFLAGS)
ALL_OBJCXXFLAGS = (PKG_OBJCXXFLAGS) (CXXPICFLAGS) (SHLIB_CXXFLAGS) (OBJCXXFLAGS)
ALL_FFLAGS = -fno-optimize-sibling-calls (PKG_FFLAGS) (FPICFLAGS) (SHLIB_FFLAGS) (FFLAGS)

can be overridden by R CMD SHLIB

P_FCFLAGS = (PKG_FFLAGS) ALL_FCFLAGS = -fno-optimize-sibling-calls (P_FCFLAGS) (FPICFLAGS) (SHLIB_FFLAGS) $(FCFLAGS)

LIBR here as a couple of packages use this without SHLIB_LINK

ALL_LIBS = (PKG_LIBS) (SHLIB_LIBADD) (LIBR)# (LIBINTL)

.SUFFIXES:
.SUFFIXES: .c .cc .cpp .d .f .f90 .f95 .m .mm .M .o

.c.o:
(CC) (ALL_CPPFLAGS) (ALL_CFLAGS) -c < -o @ .c.d: @echo "making @ from <" @(CC) -MM (ALL_CPPFLAGS) < > @ .m.d: @echo > @
.cc.o:
(CXX) (ALL_CPPFLAGS) (ALL_CXXFLAGS) -c < -o @ .cpp.o: (CXX) (ALL_CPPFLAGS) (ALL_CXXFLAGS) -c < -o @
.cc.d:
@echo "making @ from <"
@(CXX) -M (ALL_CPPFLAGS) < > @
.cpp.d:
@echo "making @ from <"
@(CXX) -M (ALL_CPPFLAGS) < > @
.m.o:
(OBJC) (ALL_CPPFLAGS) (ALL_OBJCFLAGS) -c < -o @ .mm.o: (OBJCXX) (ALL_CPPFLAGS) (ALL_OBJCXXFLAGS) -c < -o @
.M.o:
(OBJCXX) (ALL_CPPFLAGS) (ALL_OBJCXXFLAGS) -c < -o @ .f.o: (FC) (ALL_FFLAGS) -c < -o $@

@FCFLAGS_f9x@ are flags needed to recognise the extensions

.f95.o:
(FC) (ALL_FCFLAGS) -c < -o @
.f90.o:
(FC) (ALL_FCFLAGS) -c < -o @

Try

sudo apt-get install r-cran-rcppeigen

Thanks for your reply. Tried the command on terminal nothing was updated & the problem still persists. I removed & installed rstan & StanHeaders packages from source & Rcpp installed & works now. However if I use rstan and then try to use Rcpp to compile .cpp files then the error
make: *** [/usr/lib/R/etc/Makeconf:177: api.o] Error 1
returns. Not sure why I can't use Rcpp & rstan in the same R session.

1 Like

Similar error messages have been reported associated with insufficient RAM, VM sessions and outdated system libraries for which

  1. Uninstallation
  2. Upgrade
sudo apt-get update
sudo apt-get upgrade
  1. reinstallation

resolved the issue. In an older case, stan headers were missing.

Can you post a simple example of the Rcpp script that works without but not with rstan loaded?

The following is just having one function but works perfectly with Rcpp.

#include <Rcpp.h>
using namespace Rcpp;
#include <math.h>       /* pow */

//[[Rcpp::export]]

double hessian_mat(NumericVector par,NumericVector obs){
  
  Environment pkg = Environment::namespace_env("numDeriv"); //load numDeriv
  //package containing the hessian function
  Function f = pkg["hessian"]; //obtain hessian function from Num
  Function d("det");
  Function g("ll");
  Function t("sqrt");
  NumericMatrix mat;
  double mat_det;
  mat = f(g,Named("x")=par,Named("y")=obs);
  mat_det = as<double>(d(mat));
  //mat_inverse = as<NumericMatrix>(mat);
  return as<double>(t(mat_det));
}

How long should the vectors be and I assume they are different? Also, what should I use for the par and obs arguments?

So you first need to write a function called ll in the same .cpp script which has the log likelihood of the distribution you want to obtain the hessian matrix for. The first argument has to be a vector of par(parameters) for that distribution. Then you can simulate some data say 100 & get the hessian.

I will write it for the normal & upload it soon.

1 Like

Could you also supply the R wrapper please? I'm not fully up to speed on Rcpp.

So here is the complete code. In R

ll = function(x,y){
  #x is the parameter vector
  # y is thevector of observations
  n = length(y)
  mu=x[1]
  sigma = x[2]
  dev = sum((y-mu)**2)
  
  
  expression = -(n/2)*(log(2*pi)+log(sigma^2))-(1/(2*sigma**2))*dev
  return(-1*expression)
}

Then the above Rcpp code. I modified it a bit to return the Hessian matrix to compare the results from R's Hessian function.

#include <Rcpp.h>
using namespace Rcpp;
#include <math.h>       /* pow */





//[[Rcpp::export]]

NumericMatrix hessian_mat(NumericVector par,NumericVector obs){
  
  Environment pkg = Environment::namespace_env("numDeriv"); //load numDeriv
  //package containing the hessian function
  Function f = pkg["hessian"]; //obtain hessian function from Num
  Function d("det");
  Function g("ll");
  Function t("sqrt");
  NumericMatrix mat;
  double mat_det;
  mat = f(g,Named("x")=par,Named("y")=obs);
  //mat_det = as<double>(d(mat));
  //mat_inverse = as<NumericMatrix>(mat);
  //return as<double>(t(mat_det));
  return mat;
}

Then in R:

 ob = rnorm(50) #generate data
hessian_mat(c(0,1),ob) #hessian_mat from Rcpp code above
hessian(ll,x=c(0,1),y=ob) #R's own hessian function from numDeriv package

I used 0 for the mean and 1(data generated from normal(0,1)) for sd but usually one would use the ll(short for log likelihood) function I wrote to obtain the mles and plug that in the hessian and hessian_mat function.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.