R package with Rcpp - c++

I tried to add a small C++ function (called reduceString) into an R package of mine using Rcpp but I failed to configurate the package so that it compiles fine. The package can be found here.
devtools::install_github("RemiMattheyDoeret/SimBitWrapper")
Here is part of the error message
Error: package or namespace load failed for ‘SimBitWrapper’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so, 6): Symbol not found: __Z12reduceStringv
Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so
Expected in: flat namespace
in /Library/Frameworks/R.framework/Versions/3.5/Resources/library/SimBitWrapper/libs/SimBitWrapper.so
I have tried to modify again and again the NAMESPACE and DESCRIPTION files as well as src/RcppExports.cpp and R/RcppExports.r files but I just fail to fix the problem. Here are those files as well as the reduceString.cpp file
NAMESPACE
useDynLib(SimBitWrapper, .registration=TRUE)
exportPattern("^[^\\.]")
importFrom(Rcpp, evalCpp)
DESCRIPTION
Package: SimBitWrapper
Title: R wrapper for SimBit
Version: 5.1.0
Authors#R:
person(given = "Remi",
family = "Matthey-Doret",
role = c("aut", "cre"),
email = "remi.b.md#gmail.com",
comment = c(ORCID = "0000-0001-5614-5629"))
Description: The package is a wrapper for SimBit (SimBit is a simulation platform for evolutionary genetic studies). Please see the SimBit manual (at https://github.com/RemiMattheyDoret/SimBit) for description of how to use this SimBitWrapper package.
License: MIT
Encoding: UTF-8
LazyData: true
Depends: R (>= 2.15)
Imports: data.table, processx, Rcpp
LinkingTo: Rcpp
Suggests:
URL: https://github.com/RemiMattheyDoret
src/RcppExports.cpp
#include <Rcpp.h>
using namespace Rcpp;
// reduceString
std::string reduceString();
RcppExport SEXP _SimBitWrapper_reduceString() {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
rcpp_result_gen = Rcpp::wrap(reduceString());
return rcpp_result_gen;
END_RCPP
}
static const R_CallMethodDef CallEntries[] = {
{"_SimBitWrapper_reduceString", (DL_FUNC) &_SimBitWrapper_reduceString, 0},
{NULL, NULL, 0}
};
RcppExport void R_init_SimBitWrapper(DllInfo *dll) {
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}
R/RcppExports.r
reduceString <- function(x) {
.Call(`_SimBitWrapper_reduceString`,x)
}
reduceString.cpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
std::string reduceString(CharacterVector& x)
{
// I cut off this part that does not need to be printed here
}

Thanks for posting a link to a repo.
It worked for me as soon as I re-recreated RcppExports.{cpp,R} using my (current) version of Rcpp and a call to compileAttributes(). What version of Rcpp do you have?
The log below uses my wrappers from littler but that is immaterial. The R CMD ... commands would have worked the same way.
Log
edd#rob:/tmp/SimBitWrapper(master)$ build.r
* checking for file ‘./DESCRIPTION’ ... OK
* preparing ‘SimBitWrapper’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* building ‘SimBitWrapper_5.1.0.tar.gz’
edd#rob:/tmp/SimBitWrapper(master)$ install2.r -l /tmp/lib SimBitWrapper_5.1.0.tar.gz
* installing *source* package ‘SimBitWrapper’ ...
** using staged installation
** libs
ccache g++ -I"/usr/share/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -fpic -g -O3 -Wall -pipe -pedantic -c RcppExports.cpp -o RcppExports.o
ccache g++ -I"/usr/share/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -fpic -g -O3 -Wall -pipe -pedantic -c reduceString.cpp -o reduceString.o
reduceString.cpp: In function ‘std::string reduceString(Rcpp::CharacterVector&)’:
reduceString.cpp:10:27: warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘R_xlen_t’ {aka ‘long int’} [-Wsign-compare]
10 | for (size_t i = 0 ; i < x.size() ; ++i)
| ~~^~~~~~~~~~
ccache g++ -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o SimBitWrapper.so RcppExports.o reduceString.o -L/usr/lib/R/lib -lR
installing to /tmp/lib/00LOCK-SimBitWrapper/00new/SimBitWrapper/libs
** R
** byte-compile and prepare package for lazy loading
** help
No man pages found in package ‘SimBitWrapper’
*** installing help indices
** building package indices
** testing if installed package can be loaded from temporary location
** checking absolute paths in shared objects and dynamic libraries
** testing if installed package can be loaded from final location
** testing if installed package keeps a record of temporary installation path
* DONE (SimBitWrapper)
edd#rob:/tmp/SimBitWrapper(master)$
Diff
edd#rob:/tmp/SimBitWrapper(master)$ git diff
diff --git a/R/RcppExports.R b/R/RcppExports.R
index 19a82b4..62f890a 100644
--- a/R/RcppExports.R
+++ b/R/RcppExports.R
## -2,6 +2,6 ##
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
reduceString <- function(x) {
- .Call(`_SimBitWrapper_reduceString`,x)
+ .Call(`_SimBitWrapper_reduceString`, x)
}
diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp
index 90dda3b..0b91977 100644
--- a/src/RcppExports.cpp
+++ b/src/RcppExports.cpp
## -6,18 +6,19 ##
using namespace Rcpp;
// reduceString
-std::string reduceString();
-RcppExport SEXP _SimBitWrapper_reduceString() {
+std::string reduceString(CharacterVector& x);
+RcppExport SEXP _SimBitWrapper_reduceString(SEXP xSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
- rcpp_result_gen = Rcpp::wrap(reduceString());
+ Rcpp::traits::input_parameter< CharacterVector& >::type x(xSEXP);
+ rcpp_result_gen = Rcpp::wrap(reduceString(x));
return rcpp_result_gen;
END_RCPP
}
static const R_CallMethodDef CallEntries[] = {
- {"_SimBitWrapper_reduceString", (DL_FUNC) &_SimBitWrapper_reduceString, 0},
+ {"_SimBitWrapper_reduceString", (DL_FUNC) &_SimBitWrapper_reduceString, 1},
{NULL, NULL, 0}
};
edd#rob:/tmp/SimBitWrapper(master)$
PS: You have a second copy of your package in your repo. I just the top-level. Also, you committed the .DS_Store files from your mac which are of no use to R or a repo.

Related

can't update R package documentation: `System command 'R' failed` (#using statements don't work)

I have a RcppEigen project that I'm trying to update documentation for. This project only exports one c++ function into R.
Unfortunately, I get a problem when I run either devtools::document(pkg = "~/pfexamplesinr/") or roxygen2::roxygenize(roclets="rd", package.dir = "pfexamplesinr/") The error message is below.
It says something about System command 'R' failed. The entire build output is too large to paste (I go over the character limit). If I search for the first error, it's RcppExports.cpp:15:21: error: ‘Map’ was not declared in this scope. Is there some funny business about the order of #include directives and // [[Rcpp::depends(RcppEigen)]] statements?
Note: the code builds fine when I use Rcpp::sourceCpp('pfexamplesinr/src/likelihoods.cpp')
Edit: this problem goes away when I remove the #using statements, and prepend Eigen:: whenever necessary.
#include <RcppEigen.h>
#include "svol_sisr_hilb.h"
#include "resamplers.h"
// [[Rcpp::depends(RcppEigen)]]
// choose number of particles, and number of bits for inverse Hilbert curve map
#define NP 500
#define NB 5
#define debug_mode false
using Eigen::Map;
using Eigen::MatrixXd;
using Eigen::VectorXd;
using hilb_sys_resamp_T = pf::resamplers::sys_hilb_resampler<NP,1,NB,double>;
using svol_pfilter = svol_sisr_hilb<NP,NB, hilb_sys_resamp_T, double, debug_mode>;
// helpful notes:
// 1.
// parameters passed to svol_pfilter() ctor are in the following order: phi, beta, sigma
// 2.
// uProposal will be dimension (time X (particles + 1))
// first NP columns will be used for state sampling
// last column will be used for resampling at each time point
// 3.
// choosing NP or NB too large will result in stackoverflow
// number of particles is set in two places: in the #define directive and also used in your R script
// [[Rcpp::export]]
double svolApproxLL(const Map<VectorXd> y, const Map<VectorXd> thetaProposal, const Map<MatrixXd> uProposal) {
// construct particle filter object
svol_pfilter pf(thetaProposal(0), thetaProposal(1), thetaProposal(2)); // order: phi, beta, sigma
// iterate over the data
double log_like(0.0);
Eigen::Matrix<double,1,1> yt;
std::array<Eigen::Matrix<double,1,1>, NP> uStateTransition;
Eigen::Matrix<double,1,1> uResample;
for(int time = 0; time < y.rows(); ++time){
// change types of inputs
yt(0) = y(time);
for(unsigned particle = 0; particle < NP; ++particle) {
uStateTransition[particle] = uProposal.block(time,particle,1,1);
}
uResample(0) = uProposal(time,NP);
// std::cout << yt.transpose() << "\n";
// for(unsigned int i = 0; i < NP; ++i)
// std::cout << uStateTransition[i] << ", ";
// std::cout << "\n----------\n";
// update particle filter and log-likelihood
pf.filter(yt, uStateTransition, uResample);
log_like += pf.getLogCondLike();
}
//return es.eigenvalues();
return log_like;
}
// You can include R code blocks in C++ files processed with sourceCpp
// (useful for testing and development). The R code will be automatically
// run after the compilation.
/*** R
numTime <- 3
numParts <- 500 # make sure this agrees with NP
u <- matrix(rnorm(numTime*(numParts+1)), ncol = numParts+1)
params <- c(.9, 1, .1) # -1 < phi < 1, beta, sigma > 0
hist(replicate(100, svolApproxLL(rnorm(numTime), params, u)))
*/
Error in (function (command = NULL, args = character(), error_on_status = TRUE, :
System command 'R' failed, exit status: 1, stdout + stderr (last 10 lines):
E> /home/taylor/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/Eigen/src/Core/MatrixBase.h:48:34: required from ‘class Eigen::MatrixBase<Eigen::Matrix<double, -1, 1> >’
E> /home/taylor/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/Eigen/src/Core/PlainObjectBase.h:98:7: required from ‘class Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1> >’
E> /home/taylor/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/Eigen/src/Core/Matrix.h:178:7: required from ‘class Eigen::Matrix<double, -1, 1>’
E> /home/taylor/R/x86_64-pc-linux-gnu-library/4.1/Rcpp/include/Rcpp/InputParameter.h:77:11: required from ‘class Rcpp::ConstReferenceInputParameter<Eigen::Matrix<double, -1, 1> >’
E> RcppExports.cpp:43:74: required from here
E> /home/taylor/R/x86_64-pc-linux-gnu-library/4.1/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:5
Thanks for providing the URL to the repro. This error, as it happens not infrequently, appears to be one deeper down 'hidden' by devtools::document().
When I clone your repo onto a machine with 'current everything' from CRAN running Ubuntu 21.04 and doing my usual two-step of
compAttr.r # shell wrapper for Rcpp::compileAttributes
roxy.r -f # shell wrapper for roxygenize()
(using these helper scripts from my littler package),
I end in tears because your code uses C++17 statements without specifying C++17 as a compilation standard.
In other words, a vanilla bug at your end, independent of Rcpp or other tools.
edd#rob:~/git/stackoverflow/68985867/pfexamplesinr(master)$ compAttr.r
edd#rob:~/git/stackoverflow/68985867/pfexamplesinr(master)$ roxy.r -f
ℹ Loading pfexamplesinr
Exports from /home/edd/git/stackoverflow/68985867/pfexamplesinr/src/likelihoods.cpp:
double svolApproxLL(Eigen::Map<Eigen::VectorXd> y, Eigen::Map<Eigen::VectorXd> thetaProposal, Eigen::Map<Eigen::MatrixXd> uProposal)
Exports from /home/edd/git/stackoverflow/68985867/pfexamplesinr/src/rcppeigen_hello_world.cpp:
Eigen::MatrixXd rcppeigen_hello_world()
Eigen::MatrixXd rcppeigen_outerproduct(const Eigen::VectorXd& x)
double rcppeigen_innerproduct(const Eigen::VectorXd& x)
Rcpp::List rcppeigen_bothproducts(const Eigen::VectorXd& x)
Exports from /home/edd/git/stackoverflow/68985867/pfexamplesinr/src/resamplers.h:
Exports from /home/edd/git/stackoverflow/68985867/pfexamplesinr/src/rv_eval.h:
Exports from /home/edd/git/stackoverflow/68985867/pfexamplesinr/src/rv_samp.h:
/home/edd/git/stackoverflow/68985867/pfexamplesinr/src/RcppExports.cpp updated.
/home/edd/git/stackoverflow/68985867/pfexamplesinr/R/RcppExports.R updated.
Re-compiling pfexamplesinr
─ installing *source* package ‘pfexamplesinr’ ...
** using staged installation
** using staged installation
** libs
ccache g++ -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/RcppEigen/include' -fpic -g -O3 -Wall -pipe -pedantic -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-class-memaccess -c RcppExports.cpp -o RcppExports.o
ccache g++ -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/RcppEigen/include' -fpic -g -O3 -Wall -pipe -pedantic -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-class-memaccess -c likelihoods.cpp -o likelihoods.o
In file included from svol_sisr_hilb.h:8,
from likelihoods.cpp:1:
sisr_filter.h: In member function ‘void pf::filters::SISRFilter<nparts, dimx, dimy, resamp_t, float_t, debug>::filter(const osv&, const std::vector<std::function<const Eigen::Matrix<float_t, -1, -1>(const Eigen::Matrix<float_t, dimx, 1>&)> >&)’:
sisr_filter.h:229:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
229 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:262:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
262 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:288:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
288 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:320:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
320 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h: In member function ‘void pf::filters::SISRFilterCRN<nparts, dimx, dimy, dimu, dimur, resamp_t, float_t, debug>::filter(const osv&, const arrayUs&, const usvr&, const std::vector<std::function<const Eigen::Matrix<float_t, -1, -1>(const Eigen::Matrix<float_t, dimx, 1>&)> >&)’:
sisr_filter.h:559:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
559 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:592:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
592 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:618:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
618 | if constexpr(debug)
| ^~~~~~~~~
sisr_filter.h:650:16: warning: ‘if constexpr’ only available with ‘-std=c++17’ or ‘-std=gnu++17’
650 | if constexpr(debug)
| ^~~~~~~~~
In file included from likelihoods.cpp:2:
resamplers.h: In member function ‘void pf::resamplers::sys_hilb_resampler<nparts, dimx, num_hilb_bits, float_t>::resampLogWts(pf::resamplers::sys_hilb_resampler<nparts, dimx, num_hilb_bits, float_t>::arrayVec&, pf::resamplers::sys_hilb_resampler<nparts, dimx, num_hilb_bits, float_t>::arrayFloat&, const usvr&) [with long unsigned int nparts = 500; long unsigned int dimx = 1; long unsigned int num_hilb_bits = 5; float_t = double]’:
resamplers.h:1089:36: warning: ‘idx’ may be used uninitialized in this function [-Wmaybe-uninitialized]
1089 | tmpPartics[i] = sortedParts[idx];
| ~~~~~~~~~~~^
ccache g++ -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG -I'/usr/local/lib/R/site-library/Rcpp/include' -I'/usr/local/lib/R/site-library/RcppEigen/include' -fpic -g -O3 -Wall -pipe -pedantic -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-class-memaccess -c rcppeigen_hello_world.cpp -o rcppeigen_hello_world.o
ccache g++ -std=gnu++14 -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -flto=auto -Wl,-z,relro -o pfexamplesinr.so RcppExports.o likelihoods.o rcppeigen_hello_world.o -L/usr/lib/R/lib -lR
installing to /tmp/devtools_install_2dc74d5a52ad61/00LOCK-pfexamplesinr/00new/pfexamplesinr/libs
** checking absolute paths in shared objects and dynamic libraries
─ DONE (pfexamplesinr)
edd#rob:~/git/stackoverflow/68985867/pfexamplesinr(master)$

Linking error of R package with Rcpp: "undefined symbol: LAPACKE_dgels"

I am creating an R package 'lapacker' to provide the C interface for internal LAPACK library provided and used by R (with double precision and double complex only) using the R API header file "R_ext/Lapack.h". The source code:
https://github.com/ypan1988/lapacker/
And the project structure:
/lapacker
/inst
/include
/lapacke.h
/someother header files
/R
/zzz.R
/src
/lapacke_dgetrf.c
/lapacke_dgetrf_work.c
/loads of other utility functions provided by LAPACKE
/rcpp_hello.cpp
DESCRIPTION
NAMESPACE
Inside the project, I tried a test function in rcpp_hello.cpp file (Note this example is coming from https://www.netlib.org/lapack/lapacke.html#_calling_code_dgels_code):
//'#export
// [[Rcpp::export]]
void example_lapacke_dgels()
{
double a[5][3] = {{1,1,1},{2,3,4},{3,5,2},{4,2,5},{5,4,3}};
double b[5][2] = {{-10,-3},{12,14},{14,12},{16,16},{18,16}};
lapack_int info,m,n,lda,ldb,nrhs;
int i,j;
m = 5;
n = 3;
nrhs = 2;
lda = 3;
ldb = 2;
info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*a,lda,*b,ldb);
for(i=0;i<n;i++)
{
for(j=0;j<nrhs;j++)
{
printf("%lf ",b[i][j]);
}
printf("\n");
}
}
The whole package can compile properly with no errors, and in R it gives the right answer(indicating symbol LAPACKE_dgels can be found):
> example_lapacke_dgels()
2.000000 1.000000
1.000000 1.000000
1.000000 2.000000
However, when I create a separate C++ file, say demo3.cpp with exactly the same function,
#include <Rcpp.h>
#include <lapacke.h>
// [[Rcpp::depends(lapacker)]]
// [[Rcpp::export]]
void lapacke_dgels_test()
{
double a[5][3] = {{1,1,1},{2,3,4},{3,5,2},{4,2,5},{5,4,3}};
double b[5][2] = {{-10,-3},{12,14},{14,12},{16,16},{18,16}};
lapack_int info,m,n,lda,ldb,nrhs;
int i,j;
m = 5;
n = 3;
nrhs = 2;
lda = 3;
ldb = 2;
info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*a,lda,*b,ldb);
for(i=0;i<n;i++)
{
for(j=0;j<nrhs;j++)
{
printf("%lf ",b[i][j]);
}
printf("\n");
}
}
it no longer compiles properly (actually I tried under both macOS and ubuntu, same linking problem), and gives linking error messages (Cannot find symbol LAPACKE_dgels):
> Rcpp::sourceCpp("~/Desktop/demo3.cpp", showOutput = TRUE)
/usr/lib/R/bin/R CMD SHLIB -o 'sourceCpp_6.so' 'demo3.cpp'
g++ -I/usr/share/R/include -DNDEBUG -I"/home/yipan/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include" -I"/home/yipan/R/x86_64-pc-linux-gnu-library/3.4/lapacker/include" -I"/home/yipan/Desktop" -fpic -g -O2 -fdebug-prefix-map=/build/r-base-AitvI6/r-base-3.4.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c demo3.cpp -o demo3.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o sourceCpp_6.so demo3.o -L/usr/lib/R/lib -lR
Error in dyn.load("/tmp/RtmpUsASwK/sourceCpp-x86_64-pc-linux-gnu-1.0.0/sourcecpp_159e6145591d/sourceCpp_6.so") :
unable to load shared object '/tmp/RtmpUsASwK/sourceCpp-x86_64-pc-linux-gnu-1.0.0/sourcecpp_159e6145591d/sourceCpp_6.so':
/tmp/RtmpUsASwK/sourceCpp-x86_64-pc-linux-gnu-1.0.0/sourcecpp_159e6145591d/sourceCpp_6.so: undefined symbol: LAPACKE_dgels
I have also checked the lapacker.so under /R/x86_64-pc-linux-gnu-library/3.4/lapacker/libs and found:
000000000000c6b0 g DF .text 00000000000001bf Base LAPACKE_dgels
Do I miss something to get the demo3.cpp compile correctly? Thanks very much for your patience and time!
You are facing a difficult problem here. The Symbol you are trying to resolve LAPACKE_dgels is part of lapacker.so, build during package installation. Problem is, that the libraries for R packages are not meant for linking. Instead, they are loaded by R dynamically at run-time. Basically, I see four possibilities:
Convert lapacke into a header only library and install that in inst/include (c.f. RcppArmadillo).
Link with a system installation of lapacke (easy on Linux ...)
Register all functions with R and use the methods provided by R to link to them (c.f. WRE and nloptr).
Compile a library meant for linking and install it with the R package. You will still need a plugin for that to work, since you have to add -L<path/to/lib> -l<libname> .... to PKG_LIBS.
I am sure there are examples on CRAN that use method 4, but none comes to mind right now. However, as a "code kata" I have converted a recent test package of mine to use this structure, c.f. https://github.com/rstub/levmaR/tree/static.
(Original incomplete answer.)
In src/Makevars you have
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
You need an analogue setting when compiling a cpp file via Rcpp attributes. The best way achieve this is by using an Rcpp plugin, c.f. RcppArmadillo's solution (adjustments are untested!):
inlineCxxPlugin <- function(...) {
plugin <-
Rcpp::Rcpp.plugin.maker(
include.before = "#include <lapacke.h>",
libs = "$(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)",
package = "lapacker"
)
settings <- plugin()
settings$env$PKG_CPPFLAGS <- "-I../inst/include"
settings
}
BTW, why do you want to interface with LAPACK directly, when RcppArmadillo does so already?

Unable to compile simple Rcpp function using c++11

Problem background
After upgrading to macos-mojave I don't seem to be able to compile a simple Rcpp function that would use // [[Rcpp::plugins(cpp11)]]. I've tried:
Editing Makevars following this answer
Running xcode-select --install following linked discussion on compiling C under macos after upgrade.
Changing includes, string / string.h leads to the same error
Code
The function, I've drafted to generate the error:
#include <Rcpp.h>
#include <string.h> // std::string, std::stod
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
NumericVector convertToDouble(Rcpp::StringVector x) {
// Numeirc vector to store results
NumericVector res;
// Double for converted values
double converted_double;
for(Rcpp::StringVector::iterator it = x.begin(); it != x.end(); ++it) {
// Get [] for vector element
int index = std::distance(x.begin(), it);
// Add results to vector
converted_double = std::stod(x[index]);
res.push_back(converted_double);
}
return res;
}
// Source / test
/*** R
Rcpp::sourceCpp(
file = "sample_stod.cpp",
embeddedR = FALSE,
verbose = TRUE,
rebuild = TRUE,
cleanupCacheDir = TRUE,
showOutput = TRUE)
convertToDouble(c("2.3", "34.25a", "abc32def.43", "12", "1.5"))
*/
Errors
>> Rcpp::sourceCpp(
+ file = "sample_stod.cpp",
+ embeddedR = FALSE,
+ verbose = TRUE,
+ rebuild = TRUE,
+ cleanupCacheDir = TRUE,
+ showOutput = TRUE)
Generated extern "C" functions
--------------------------------------------------------
#include <Rcpp.h>
// convertToDouble
NumericVector convertToDouble(Rcpp::StringVector x);
RcppExport SEXP sourceCpp_1_convertToDouble(SEXP xSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::StringVector >::type x(xSEXP);
rcpp_result_gen = Rcpp::wrap(convertToDouble(x));
return rcpp_result_gen;
END_RCPP
}
Generated R functions
-------------------------------------------------------
`.sourceCpp_1_DLLInfo` <- dyn.load('/private/var/folders/7x/kwc1y_l96t55_rwlv35mg8xh0000gn/T/Rtmp2H7VYU/sourceCpp-x86_64-apple-darwin15.6.0-0.12.19/sourcecpp_295d2f3a47a3/sourceCpp_2.so')
convertToDouble <- Rcpp:::sourceCppFunction(function(x) {}, FALSE, `.sourceCpp_1_DLLInfo`, 'sourceCpp_1_convertToDouble')
rm(`.sourceCpp_1_DLLInfo`)
Building shared library
--------------------------------------------------------
DIR: /private/var/folders/7x/kwc1y_l96t55_rwlv35mg8xh0000gn/T/Rtmp2H7VYU/sourceCpp-x86_64-apple-darwin15.6.0-0.12.19/sourcecpp_295d2f3a47a3
/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB -o 'sourceCpp_2.so' --preclean 'sample_stod.cpp'
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I"/Users/huski/Documents/R Projects/RcppConversion" -I/usr/local/include -fPIC -Wall -g -O2 -c sample_stod.cpp -o sample_stod.o
sample_stod.cpp:17:28: error: no matching function for call to 'stod'
converted_double = std::stod(x[index]);
^~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3910:30: note: candidate function not viable: no known conversion from 'Rcpp::Vector<16, PreserveStorage>::Proxy' (aka 'string_proxy<16, PreserveStorage>') to 'const std::__1::string' (aka 'const basic_string<char, char_traits<char>, allocator<char> >') for 1st argument
_LIBCPP_FUNC_VIS double stod (const string& __str, size_t* __idx = 0);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:3930:30: note: candidate function not viable: no known conversion from 'Rcpp::Vector<16, PreserveStorage>::Proxy' (aka 'string_proxy<16, PreserveStorage>') to 'const std::__1::wstring' (aka 'const basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> >') for 1st argument
_LIBCPP_FUNC_VIS double stod (const wstring& __str, size_t* __idx = 0);
^
1 error generated.
make: *** [sample_stod.o] Error 1
Error in Rcpp::sourceCpp(file = "sample_stod.cpp", embeddedR = FALSE, :
Error 1 occurred building shared library.
>>
~/.R/Makevars
# Force use of compilers maintained by Homebrew
# Clang and clang++
CC=/usr/local/opt/llvm/bin/clang
CXX=/usr/local/opt/llvm/bin/clang++
CXX1X=clang-omp++
Tried passing CC \ CXX \ CXX1X variables to use compilers installed via Homebrew but does generate a different error message. All compilation attempts fail on
sample_stod.cpp:17:28: error: no matching function for call to 'stod'
Update: can't find stod
g++ --version
g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.11.45.2)
Target: x86_64-apple-darwin18.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Current ~/.R/Makevars
# Force use of compilers maintained by Homebrew
# Clang and clang++
# CC=/usr/local/opt/llvm/bin/clang
# CXX=/usr/local/opt/llvm/bin/clang++
# CXX1X=clang-omp++
# Fortran
# FC=/usr/local/opt/gcc/bin/gfortran
# F77=/usr/local/opt/gcc/bin/gfortran
# CC=/usr/local/clang4/bin/clang
# CXX=/usr/local/clang4/bin/clang++
# LDFLAGS=-L/usr/local/clang4/lib
# CPPFLAGS="-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
CPPFLAGS="-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
So, outside of the initial issue with respect to the compiler shenanigans on macOS (covered here), you have to help out the compiler when converting to a std::string from a single value in Rcpp::StringVector in this case.
#include <Rcpp.h>
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
Rcpp::NumericVector convertToDouble(Rcpp::StringVector x) {
// Numeirc vector to store results
std::vector<double> res;
// Double for converted values
double converted_double;
for(Rcpp::StringVector::iterator it = x.begin(); it != x.end(); ++it) {
// Get [] for vector element
int index = std::distance(x.begin(), it);
// Help the conversion to string
std::string temp = Rcpp::as<std::string>(x[index]);
// Convert
converted_double = std::stod(temp);
// Add to a std vector... Do not use with Rcpp types
res.push_back(converted_double);
}
// Convert and return the Rcpp type as desired.
return Rcpp::wrap(res);
}
Then, we can run:
convertToDouble(c("2.3", "34.25a", "abc32def.43", "12", "1.5"))
# Error in convertToDouble(c("2.3", "34.25a", "abc32def.43", "12", "1.5")) : stod: no conversion
This errors at: abc32def.43
You may need to do additional cleaning of the string prior to trying a conversion...

When using dyn.load in R I do not get any error messages, but my .dll file does not load

I am following an example from the 'Writing R extensions manual' version 3.3.1. Specifically, I am working with c++ code, which I have saved in file out.cpp, that is on page 115 of that manual shown below:
#include <R.h>
#include <Rinternals.h>
SEXP out(SEXP x, SEXP y)
{
int nx = length(x), ny = length(y);
SEXP ans = PROTECT(allocMatrix(REALSXP, nx, ny));
double *rx = REAL(x), *ry = REAL(y), *rans = REAL(ans);
for(int i = 0; i < nx; i++) {
double tmp = rx[i];
for(int j = 0; j < ny; j++)
rans[i + nx*j] = tmp * ry[j];
}
UNPROTECT(1);
return ans;
}
I have a windows computer and use cygwin command line. There I typed R CMD SHLIB out.cpp and get the following result:
cygwin warning:
MS-DOS style path detected: C:/R-3.2.3/etc/x64/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/R-3.2.3/etc/x64/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
g++ -m64 -I"C:/R-3.2.3/include" -DNDEBUG -I"d:/RCompile/r- compiling/local/local323/include" -O2 -Wall -mtune=core2 -c out.cpp -o out.o
g++ -m64 -shared -s -static-libgcc -o out.dll tmp.def out.o -Ld:/RCompile/r-compiling/local/local323/lib/x64 -Ld:/RCompile/r-compiling/local/local323/lib -LC:/R-3.2.3/bin/x64 -lR
I think there is nothing wrong with this, right? Then when I go into Rstudio I type
dyn.load(paste("C:/Rextensions/out",
.Platform$dynlib.ext, sep = ""),local=FALSE)
which gives me no error message whatsoever. But when I run
is.loaded('out')
I get 'FALSE', so I am never able to upload out.dll. Could you help me with this please?
P.S.: I know how to run functions like this one using Rcpp, so please don't tell me to do that instead. What I am trying to do is understand really well the Writing Extensions manual so I hope I can go through all of their examples in that document.
Thank you.

custom node.js addon can be built but not able to include

I am writing a custom node.js addon by C++ mixed with C program.
The addon.cc consists of something like
#define BUILDING_NODE_EXTENSION
#include <node.h>
#include <node_buffer.h>
using namespace v8;
using namespace node;
/* other logic and function... */
Handle<Value> RunCallback(const Arguments& args) {
HandleScope scope;
Local<Value> buffer1 = args[0];
size_t size = Buffer::Length(buffer1->ToObject());
char* bufferdata = Buffer::Data(buffer1->ToObject());
/* some logic written in C style ... */
Local<Function> cb = Local<Function>::Cast(args[1]);
const unsigned argc = 1;
Local<Value> argv[argc] = { Local<Value>::New(String::New(outputdata, outputSize)) };
cb->Call(Context::GetCurrent()->Global(), argc, argv);
return scope.Close(Undefined());
}
void Init(Handle<Object> target) {
target->Set(String::NewSymbol("runCallback"), FunctionTemplate::New(RunCallback)->GetFunction());
}
NODE_MODULE(addon, Init)
It also includes other .cc files, so the wscript is like this:
srcdir = '.'
blddir = 'build'
VERSION = '0.0.1'
def set_options(opt):
opt.tool_options('compiler_cxx')
def configure(conf):
conf.check_tool('compiler_cxx')
conf.check_tool('node_addon')
def build(bld):
obj = bld.new_task_gen('cxx', 'shlib', 'node_addon')
obj.target = 'addon'
obj.source = ['addon.cc', 'otherFiles.cc']
When I run node-waf configure, it shows:
Checking for program g++ or c++ : /usr/bin/g++
Checking for program cpp : /usr/bin/cpp
Checking for program ar : /usr/bin/ar
Checking for program ranlib : /usr/bin/ranlib
Checking for g++ : ok
Checking for node path : not found
Checking for node prefix : ok /usr/local
'configure' finished successfully (0.169s)
When I run node-waf build, it shows:
Waf: Entering directory `/path/build'
[ 1/25] cxx: addon.cc -> build/Release/addon_1.o
... list of file ...
build/Release/list of file -> build/Release/addon.node
Waf: Leaving directory `/path/build'
'build' finished successfully (0.544s)
But when I try the following in node REPL, it shows:
var addon = require("./build/Release/addon");
Error: Unable to load shared library /path/build/Release/addon.node
at Object..node (module.js:472:11)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Module.require (module.js:354:17)
at require (module.js:370:17)
at repl:1:13
at REPLServer.eval (repl.js:80:21)
at repl.js:190:20
at REPLServer.eval (repl.js:87:5)
at Interface.<anonymous> (repl.js:182:12)
It is very strange. I have checked that the file should match with the system architecture:
$ file build/Release/addon.node
build/Release/addon.node: Mach-O 64-bit bundle x86_64
$ file `which node`
/usr/local/bin/node: Mach-O 64-bit executable x86_64
By looking at nm, it shows the following:
nm ./build/Release/addon.node
0000000000011880 s GCC_except_table30
0000000000001160 t _Init
U __Unwind_Resume_or_Rethrow
U ___bzero
U ___gxx_personality_v0
0000000000013220 D _addon_module
U _free
0000000000013600 D _lsfmeanTbl
0000000000013420 D _memLfTbl
U _memcpy
U _memmove
U _puts
U _realloc
0000000000011950 s _ssqEn_win.2272
000000000001341c D _stMemLTbl
00000000000132e0 D _state_frgqTbl
00000000000132c0 D _state_sq3Tbl
U dyld_stub_binder
(... many are omitted ...)
What will be the possible reasons? Is that I cannot just combine C files with C++ files to compile? Should I remove all malloc/realloc/free? Or any other possible reasons?
Wrap the definition of Init() and the NODE_MODULE macro in extern "C" to avoid name C++ mangling of the exported symbols:
extern "C" {
void Init(Handle<Object> target) {
target->Set(String::NewSymbol("runCallback"), FunctionTemplate::New(RunCallback)->GetFunction());
}
NODE_MODULE(addon, Init)
}
By using node.js which version >= 0.7.6. the problem is solved. Also, it has a lot more debug message.