r-lib / cpp11

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

C++ ODR violated when using `testthat` #350

Closed jgaeb closed 5 months ago

jgaeb commented 5 months ago

The autogenerated cpp11.cpp file is leading to run_testthat_tests() being defined twice. In particular, in my rar package it's defined here in cpp11.cpp:

extern SEXP run_testthat_tests(void *);

and in the testthat.h header:

extern "C" SEXP run_testthat_tests(SEXP use_xml_sxp) {
  bool use_xml = LOGICAL(use_xml_sxp)[0];
  bool success = testthat::run_tests(use_xml);
  return ScalarLogical(success);
}

This is leading to a violation of the C++ One Definition Rule when I compile this code on Fedora. (And presumably other platforms as well.) I've received the advice to simply include the testthat.h header in the cpp11.cpp file, but since it's autogenerated, I think this is a bug. (I'm pretty new to R package development, so my apologies in advance if this is actually user error.)

jgaeb commented 5 months ago

Nevermind—this seems to be the same as this issue on the testthat repo: https://github.com/r-lib/testthat/issues/1230.

jgaeb commented 5 months ago

In case anyone finds this in the future because they're dealing with the same problem, the issue actually seems to be with tools::package_native_routine_registration_skeleton(), which generates the wrong signature for some reason in R 4.3.2. See this bug report, as well as this patch in Rcpp. This thread on the r-package-devel list which includes some workarounds.