r-lib / cpp11

cpp11 helps you to interact with R objects using C++ code.
https://cpp11.r-lib.org/
Other
199 stars 46 forks source link

Compilation trouble with doubles_matrix in version 0.4.0 on Linux #242

Closed wlandau closed 2 years ago

wlandau commented 2 years ago

I love cpp11, and I have been using it to develop an internal proprietary package. When I upgraded to version 0.4.0 today, I started having trouble compiling code that uses doubles_matrix objects. Here is a reprex using the Gibbs sampler from vignettes/cpp11.Rmd.

tmp <- tempfile(fileext = ".cpp")
source <- "
#include \"cpp11/matrix.hpp\"
#include \"cpp11/doubles.hpp\"
#include \"Rmath.h\"
using namespace cpp11;
namespace writable = cpp11::writable;

[[cpp11::register]] cpp11::doubles_matrix gibbs_cpp(int N, int thin) {
  writable::doubles_matrix mat(N, 2);
  double x = 0, y = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < thin; j++) {
      x = Rf_rgamma(3., 1. / double(y * y + 4));
      y = Rf_rnorm(1. / (x + 1.), 1. / sqrt(2. * (x + 1.)));
    }
    mat(i, 0) = x;
    mat(i, 1) = y;
  }
  return mat;
}
"
    writeLines(source, tmp)
    cpp11::cpp_source(tmp)
#> /tmp/Rtmpl1pvRW/file5357db82e91.cpp:8:21: error: invalid use of template-name ‘cpp11::doubles_matrix’ without an argument list
#>  [[cpp11::register]] cpp11::doubles_matrix gibbs_cpp(int N, int thin) {
#>                      ^~~~~
#> In file included from /tmp/Rtmpl1pvRW/file5357db82e91.cpp:2:
#> /CENSORED/R/x86_64-pc-linux-gnu-library/4.0/cpp11/include/cpp11/matrix.hpp:207:59: note: ‘template<class S> using doubles_matrix = cpp11::matrix<cpp11::r_vector<double>, double, S>’ declared here
#>  using doubles_matrix = matrix<r_vector<double>, double, S>;
#>                                                            ^
#> make: *** [/opt/R/R-4.0.3/lib64/R/etc/Makeconf:182: /tmp/Rtmpl1pvRW/file5357db82e91.o] Error 1
#> Error: Compilation failed.

Created on 2021-09-22 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.0.3 (2020-10-10) #> os Red Hat Enterprise Linux #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/Indiana/Indianapolis #> date 2021-09-22 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> brio 1.1.2 2021-04-23 [1] CRAN (R 4.0.3) #> callr 3.7.0 2021-04-20 [1] CRAN (R 4.0.3) #> cli 3.0.1 2021-07-17 [1] CRAN (R 4.0.3) #> cpp11 0.4.0 2021-09-22 [1] CRAN (R 4.0.3) #> crayon 1.4.1 2021-02-08 [1] CRAN (R 4.0.3) #> decor 1.0.0 2020-06-30 [2] CRAN (R 4.0.3) #> desc 1.3.0 2021-03-05 [1] CRAN (R 4.0.3) #> devtools 2.3.2 2020-09-18 [2] CRAN (R 4.0.3) #> digest 0.6.27 2020-10-24 [2] CRAN (R 4.0.3) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.0.3) #> evaluate 0.14 2019-05-28 [2] CRAN (R 4.0.3) #> fansi 0.5.0 2021-05-25 [1] CRAN (R 4.0.3) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.0.3) #> fs 1.5.0 2020-07-31 [2] CRAN (R 4.0.3) #> glue 1.4.2 2020-08-27 [2] CRAN (R 4.0.3) #> highr 0.9 2021-04-16 [1] CRAN (R 4.0.3) #> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.0.3) #> knitr 1.34 2021-09-09 [1] CRAN (R 4.0.3) #> lifecycle 1.0.0 2021-02-15 [1] CRAN (R 4.0.3) #> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.3) #> memoise 1.1.0 2017-04-21 [2] CRAN (R 4.0.3) #> pillar 1.6.2 2021-07-29 [1] CRAN (R 4.0.3) #> pkgbuild 1.2.0 2020-12-15 [1] CRAN (R 4.0.3) #> pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.0.3) #> pkgload 1.1.0 2020-05-29 [2] CRAN (R 4.0.3) #> prettyunits 1.1.1 2020-01-24 [2] CRAN (R 4.0.3) #> processx 3.5.2 2021-04-30 [1] CRAN (R 4.0.3) #> ps 1.6.0 2021-02-28 [1] CRAN (R 4.0.3) #> purrr 0.3.4 2020-04-17 [2] CRAN (R 4.0.3) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.0.3) #> remotes 2.2.0 2020-07-21 [2] CRAN (R 4.0.3) #> rlang 0.4.11 2021-04-30 [1] CRAN (R 4.0.3) #> rmarkdown 2.10 2021-08-06 [1] CRAN (R 4.0.3) #> rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.0.3) #> sessioninfo 1.1.1 2018-11-05 [2] CRAN (R 4.0.3) #> stringi 1.7.4 2021-08-25 [1] CRAN (R 4.0.3) #> stringr 1.4.0 2019-02-10 [2] CRAN (R 4.0.3) #> testthat 3.0.0 2020-10-31 [2] CRAN (R 4.0.3) #> tibble 3.1.4 2021-08-25 [1] CRAN (R 4.0.3) #> usethis 2.0.1 2021-02-10 [1] CRAN (R 4.0.3) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.0.3) #> vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.0.3) #> withr 2.4.2 2021-04-18 [1] CRAN (R 4.0.3) #> xfun 0.25 2021-08-06 [1] CRAN (R 4.0.3) #> yaml 2.2.1 2020-02-01 [2] CRAN (R 4.0.3) #> #> [1] /CENSORED/R/x86_64-pc-linux-gnu-library/4.0 #> [2] /CENSORED/R/R-4.0.3/lib64/R/library ```
jimhester commented 2 years ago

The doubles_matrix class is now a template, so you need to use cpp11::doubles_matrix<>, or specify cpp11::doubles_matrix<by_column> or by_row depending on whether be able to use column major or row major indexing.

wlandau commented 2 years ago

Thanks Jim, that solved the issue for me. Any other breaking changes in the foreseeable future?