Compilation error of model with external c++ function

Dear All,

we have a (quite) simple 2PL model that includes an external c++ function (a simple random number generator). The model compiles fine on a Windows machine, but does not on a Linux high performance cluster (Linux). To include the external c++ function, we followed the vignette at https://cran.r-project.org/web/packages/rstan/vignettes/external.html.

Here’s the error:
make: *** [file162f1fddb55.o] Error 1 ERROR(s) during compilation: source code errors or compiler configuration errors Compilation ERROR, function(s)/method(s) not created!

Here’s the .hpp-file which includes the c++ function:

#include "random"
#include "ctime"
#include "iostream"
#include "chrono"
static int random = 0;
int rnum(std::ostream* pstream__) {
random += 1;
std::mt19937_64 generator;
generator.seed(random);
std::uniform_int_distribution<uint32_t> dice(1,10000000);
int random = dice(generator);
return random;
}´ 

Session_Info:

`  R version 3.6.2 (2019-12-12)
   Platform: x86_64-pc-linux-gnu (64-bit)
   Running under: Scientific Linux 7.7 (Nitrogen)
   Matrix products: default
   BLAS:   /scratch/fuchs/agmisc/koenig/sense/spack/opt/spack/linux-scientific7-ivybridge/gcc-9.2.0/r- 
   3.6.2-qvgqnjjlnw526vueejmw6vg5w2ihudpq/rlib/R/lib/libRblas.so
   LAPACK: /scratch/fuchs/agmisc/koenig/sense/spack/opt/spack/linux-scientific7-ivybridge/gcc-9.2.0/r- 
   3.6.2-qvgqnjjlnw526vueejmw6vg5w2ihudpq/rlib/R/lib/libRlapack.so
   locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base
other attached packages:
[1] rstan_2.19.2       ggplot2_3.2.1      StanHeaders_2.19.0
loaded via a namespace (and not attached):
 [1] Rcpp_1.0.3         magrittr_1.5       tidyselect_1.0.0   munsell_0.5.0
 [5] colorspace_1.4-1   R6_2.4.1           rlang_0.4.4        fansi_0.4.1
 [9] dplyr_0.8.4        tools_3.6.2        parallel_3.6.2     pkgbuild_1.0.6
[13] grid_3.6.2         gtable_0.3.0       loo_2.2.0          cli_2.0.1
[17] withr_2.1.2        matrixStats_0.55.0 lazyeval_0.2.2     assertthat_0.2.1
[21] tibble_2.1.3       lifecycle_0.1.0    crayon_1.3.4       processx_3.4.1
[25] gridExtra_2.3      purrr_0.3.3        callr_3.4.1        ps_1.3.0
[29] inline_0.3.15      glue_1.3.1         compiler_3.6.2     pillar_1.4.3
[33] scales_1.1.0       prettyunits_1.1.1  stats4_3.6.2       pkgconfig_2.0.3

`

Contents of the Makevars-file:

CXX_STD = CXX14
CXX14FLAGS= -O3 -fPIC -std=c++1y
CXX14=/scratch/fuchs/agmisc/koenig/sense/spack/opt/spack/linux-scientific7-ivybridge/gcc- 
      9.2.0/gcc-9.2.0-tlk7au4rfsov33pewelgy4nf4dksdiq2/bin/g++
CXX=g++ -I/usr/include/openmpi3-x86_64 -pthread -Wl,-rpath -Wl,/usr/lib64/openmpi3/lib -Wl,--enable-new-dtags -L/usr/lib64/openmpi3/lib -lmpi_cxx -lmpi -std=c++1y
STAN_MPI=true
TBB_CXX_TYPE=gcc++

Please note that both

example(stan_model, package = "rstan", run.dontrun = TRUE)
example(cxxfunction, package = "inline", run.dontrun = TRUE)

work without error. Other models (without external c++ code) also work fine. So perhaps there’s an error in the c++ function? Since I am not well versed with c++ code I would be grateful for any hint towards potential errors in the code.

Let me know if you need further information!

Any help appreciated!

Best wishes
Christoph

We need to see the part of the error message that contains error: (with the colon) when you try to call stan or sampling with the verbose = TRUE argument specified.

Hi Ben!

Thanks for your reply.

Upon closer inspection of the verbose-output, there are several recurring error messages… as an example:

error: 'basic_ostream' in namespace 'model183c06c3cd433_model_namespace::std' does not name a template type
error: 'basic_istream' in namespace 'model183c06c3cd433_model_namespace::std' does not name a template type
error: 'log' is not a member of 'model183c06c3cd433_model_namespace::std'

Best wishes
Christoph