Reputation: 77
I'm very new to C++ and Rcpp. I have the following function, which converts a character vector containing string representations of large integers to integers, then converts these to hexadecimals and takes a substring of the resulting hex number. I can use this function with sourceCpp and it generates the expected result.
#include <Rcpp.h>
#include <iostream>
#include <ostream>
using namespace Rcpp;
// [[Rcpp::export]]
Rcpp::StringVector mcc(Rcpp::StringVector x) {
unsigned short int n = x.size();
Rcpp::StringVector output(n);
for(int i = 0; i < n; ++i) {
unsigned long int number = std::strtoul(x[i], NULL, 10);
std::stringstream sstream;
sstream << std::hex << number;
std::string result = sstream.str();
output[i] = result.substr(0,3);
sstream.str("");
}
return output;
}
However, when I try to compile the function to include it in a package I'm building, the compiler (clang++, on Mac) throws the following errors:
RcppExports.cpp:24:40: error: address of overloaded function 'mcc' does not match required type 'void *()'
{"mcc", (DL_FUNC) &mcc, 1},
^~~
RcppExports.cpp:20:17: note: candidate function has different number of parameters (expected 0 but has 1)
RcppExport SEXP mcc(SEXP);
^
RcppExports.cpp:9:20: note: candidate function has different number of parameters (expected 0 but has 1)
Rcpp::StringVector mcc(Rcpp::StringVector x);
My experience with C++ is virtually nonexisting, so I don't even understand what it's saying here. A google/Stackoverflow search for the error message did not return any helpful results.
I appreciate your help!
--- EDIT ----
This is the code generated by compileAttributes() (run twice)
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
#include <Rcpp.h>
using namespace Rcpp;
// mcc
Rcpp::StringVector mcc(Rcpp::StringVector x);
RcppExport SEXP _cropMobileData_mcc(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(mcc(x));
return rcpp_result_gen;
END_RCPP
}
RcppExport SEXP mcc(SEXP);
static const R_CallMethodDef CallEntries[] = {
{"_cropMobileData_mcc", (DL_FUNC) &_cropMobileData_mcc, 1},
{"mcc", (DL_FUNC) &mcc, 1},
{NULL, NULL, 0}
};
RcppExport void R_init_cropMobileData(DllInfo *dll) {
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}
Upvotes: 0
Views: 294
Reputation: 11
I got same error today. My solution is that replace the .Call method by call function by name directly and it magically worked.
Just share my experience with you.
Upvotes: 0
Reputation: 368231
Your code is perfectly fine. Rcpp is perfectly fine. R is perfectly fine.
But something on your system is not, and we cannot tell remotely what the issue is.
Complete log follows.
edd@brad:/tmp$ Rscript -e 'Rcpp::Rcpp.package.skeleton("jeroenclaes")'
Creating directories ...
Creating DESCRIPTION ...
Creating NAMESPACE ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './jeroenclaes/Read-and-delete-me'.
Adding Rcpp settings
>> added Imports: Rcpp
>> added LinkingTo: Rcpp
>> added useDynLib directive to NAMESPACE
>> added importFrom(Rcpp, evalCpp) directive to NAMESPACE
>> added example src file using Rcpp attributes
>> added Rd file for rcpp_hello_world
>> compiled Rcpp attributes
edd@brad:/tmp$ cd jeroenclaes/
edd@brad:/tmp/jeroenclaes$
edd@brad:/tmp/jeroenclaes$ ## edit step here copying SO post code
edd@brad:/tmp/jeroenclaes$
Note that compAttr.r
is just a trivial shell wrapper from my littler
package. No magic here. Note that I only ran it once. (The running twice issue arises sometimes when working with existing packages.)
edd@brad:/tmp/jeroenclaes$ compAttr.r
edd@brad:/tmp/jeroenclaes$
edd@brad:/tmp/jeroenclaes$ cd ..
edd@brad:/tmp$ R CMD build jeroenclaes
* checking for file ‘jeroenclaes/DESCRIPTION’ ... OK
* preparing ‘jeroenclaes’:
* checking DESCRIPTION meta-information ... OK
* cleaning src
* installing the package to process help pages
* saving partial Rd database
* cleaning src
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
* building ‘jeroenclaes_1.0.tar.gz’
edd@brad:/tmp$
edd@brad:/tmp$ R CMD check jeroenclaes_1.0.tar.gz
* using log directory ‘/tmp/jeroenclaes.Rcheck’
* using R version 3.4.1 (2017-06-30)
* using platform: x86_64-pc-linux-gnu (64-bit)
* using session charset: UTF-8
* checking for file ‘jeroenclaes/DESCRIPTION’ ... OK
* checking extension type ... Package
* this is package ‘jeroenclaes’ version ‘1.0’
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking for sufficient/correct file permissions ... OK
* checking whether package ‘jeroenclaes’ can be installed ... [12s/11s] OK
* checking installed package size ... OK
* checking package directory ... OK
* checking DESCRIPTION meta-information ... OK
* checking top-level files ... OK
* checking for left-over files ... OK
* checking index information ... OK
* checking package subdirectories ... OK
* checking R files for non-ASCII characters ... OK
* checking R files for syntax errors ... OK
* checking whether the package can be loaded ... OK
* checking whether the package can be loaded with stated dependencies ... OK
* checking whether the package can be unloaded cleanly ... OK
* checking whether the namespace can be loaded with stated dependencies ... OK
* checking whether the namespace can be unloaded cleanly ... OK
* checking loading without being on the library search path ... OK
* checking dependencies in R code ... OK
* checking S3 generic/method consistency ... OK
* checking replacement functions ... OK
* checking foreign function calls ... OK
* checking R code for possible problems ... OK
* checking Rd files ... OK
* checking Rd metadata ... OK
* checking Rd cross-references ... OK
* checking for missing documentation entries ... WARNING
Undocumented code objects:
‘mcc’
All user-level objects in a package should have documentation entries.
See chapter ‘Writing R documentation files’ in the ‘Writing R
Extensions’ manual.
* checking for code/documentation mismatches ... OK
* checking Rd \usage sections ... OK
* checking Rd contents ... OK
* checking for unstated dependencies in examples ... OK
* checking line endings in C/C++/Fortran sources/headers ... OK
* checking compiled code ... OK
* checking examples ... OK
* checking PDF version of manual ... OK
* DONE
Status: 1 WARNING
See
‘/tmp/jeroenclaes.Rcheck/00check.log’
for details.
edd@brad:/tmp$
Looks just fine to me.
edd@brad:/tmp$ R CMD INSTALL jeroenclaes_1.0.tar.gz
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘jeroenclaes’ ...
** libs
ccache g++ -I/usr/share/R/include -DNDEBUG -I"/usr/local/lib/R/site-library/Rcpp/include" -fpic -g -O3 -Wall -pipe -Wno-unused -pedantic -Werror -march=native -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 -Wno-unused -pedantic -Werror -march=native -c mcc.cpp -o mcc.o
ccache g++ -I/usr/share/R/include -DNDEBUG -I"/usr/local/lib/R/site-library/Rcpp/include" -fpic -g -O3 -Wall -pipe -Wno-unused -pedantic -Werror -march=native -c rcpp_hello_world.cpp -o rcpp_hello_world.o
ccache g++ -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o jeroenclaes.so RcppExports.o mcc.o rcpp_hello_world.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/jeroenclaes/libs
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (jeroenclaes)
edd@brad:/tmp$ R
R version 3.4.1 (2017-06-30) -- "Single Candle"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(jeroenclaes)
> mcc("The quick brown fox")
[1] "0"
>
Maybe it is macOS specific. I don't know -- maybe ask on r-sig-mac.
Upvotes: 3