[R] Rcpp, dyn.load and C++ problems

Martin Møller Skarbiniks Pedersen traxplayer at gmail.com
Sun Dec 3 20:00:13 CET 2017


On 3 December 2017 at 05:23, Eric Berger <ericjberger at gmail.com> wrote:

> .Call("compute_values_cpp")
> Also, if you were passing arguments to the C++ function you would need to
> declare the function differently.
> Do a search on "Rcpp calling C++ functions from R"
>
>
Hi,
  It is still not working.
  $ ./compile.sh
g++  -I/usr/include/R/ -DNDEBUG   -D_FORTIFY_SOURCE=2
-I/home/tusk/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include -fpic
-march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt
-c logistic_map.cpp -o logistic_map.o
g++ -shared -L/usr/lib64/R/lib
-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -o logistic_map.so
logistic_map.o -L/usr/lib64/R/lib -lR

$ readelf -Ws logistic_map.so  | grep -i comp
    89: 0000000000002a50  2356 FUNC    GLOBAL DEFAULT    9
_Z18compute_values_cppidid
   141: 0000000000002a50  2356 FUNC    GLOBAL DEFAULT    9
_Z18compute_values_cppidid

$ R
R> dyn.load("logistic_map.so")
R> .Call("compute_values_cpp")
Error in .Call("compute_values_cpp") :
  C symbol name "compute_values_cpp" not in load table

Hmm.



> HTH,
> Eric
>
>
> On Sun, Dec 3, 2017 at 3:06 AM, Martin Møller Skarbiniks Pedersen <
> traxplayer at gmail.com> wrote:
>
>> Hi,
>>
>>   I have written a small C++ function and compile it.
>>   However in R I can't see the function I have defined in C++.
>>   I have read some web-pages about Rcpp and C++ but it is a bit confusion
>> for me.
>>
>> Anyway,
>>   This is the C++-code:
>>
>> #include <Rcpp.h>
>> using namespace Rcpp;
>>
>> // [[Rcpp::export]]
>> List compute_values_cpp(int totalPoints = 1e5, double angle_increment =
>> 0.01, int radius = 400, double grow = 3.64) {
>>   double xn = 0.5;
>>   double angle = 0.1;
>>   double xn_plus_one, yn_plus_one;
>>   NumericVector x(totalPoints);
>>   NumericVector y(totalPoints);
>>
>>   for (int i=0; i<totalPoints; i++) {
>>     xn_plus_one = xn*cos(angle)*radius;
>>     yn_plus_one = xn*sin(angle)*radius;
>>     angle += angle_increment;
>>     xn = grow*xn*(1-xn);
>>     x[i] = xn_plus_one;
>>     y[i] = yn_plus_one;
>>   }
>>   return List::create(Rcpp::Named("x") = x, Rcpp::Named("y") = y);
>> }
>>
>> And I compile it like this:
>> PKG_CXXFLAGS=$(Rscript -e 'Rcpp:::CxxFlags()') \
>> PKG_LIBS=$(Rscript -e 'Rcpp:::LdFlags()')  \
>> R CMD SHLIB logistic_map.cpp
>> without problems and I get a logistic_map.so file as expected.
>>
>> However in R:
>> R> dyn.load("logistic_map.so")
>> R> compute_values_cpp()
>> Error in compute_values_cpp() :
>>   could not find function "compute_values_cpp"
>>
>> Please advise,
>>   What piece of the puzzle is missing?
>>
>> Regards
>> Martin M. S. Pedersen
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posti
>> ng-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>


-- 
Til uvedkommende, der læser med: Der er ingen grund til at læse min
mail. Jeg har intet at gøre med FARC, al-Jihad, al-Qaida, Hamas, Hizb
al-Mujahidin eller ETA. Jeg har aldrig gjort Zakat, går ikke ind for
Istishad, har ikke lavet en bilbombe eller kernevåben og jeg ved
dårligt nok, hvad Al Manar og бомба betyder.  Men tak for den udviste
interesse.

Leve Ligemageriet!
Styrk pøbelvældet!
Bevar misundelsesafgifterne og cafepengene!
Hurra for ældrebyrden!

	[[alternative HTML version deleted]]



More information about the R-help mailing list