I tried to call c++ function from another .cpp file. I used .h header. See below what I did.
I have a f.h file:
#ifndef PACKAGENAME_ADD_H
#define PACKAGENAME_ADD_H
#include <Rcpp.h>
Rcpp::NumericVector f(Rcpp::NumericVector x) ;
#endif
f.cpp file:
#include <Rcpp.h>
using namespace Rcpp;
NumericVector f(NumericVector x) {
return x * 2;
}
g.cpp file:
#include <Rcpp.h>
#include <f.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector g(NumericVector x) {
return f(x);
}
The three files are in the same folder I got this error when I run g.cpp:
Rcpp::sourceCpp('~/g.cpp')
Error in dyn.load("/tmp/Rtmpdu4AWp/sourceCpp-x86_64-pc-linux-gnu-0.12.17/sourcecpp_260f5e1a9ebc/sourceCpp_9.so") : unable to load shared object '/tmp/Rtmpdu4AWp/sourceCpp-x86_64-pc-linux-gnu-0.12.17/sourcecpp_260f5e1a9ebc/sourceCpp_9.so': /tmp/Rtmpdu4AWp/sourceCpp-x86_64-pc-linux-gnu-0.12.17/sourcecpp_260f5e1a9ebc/sourceCpp_9.so: undefined symbol: _Z1fN4Rcpp6VectorILi14ENS_15PreserveStorageEEE
Can someone help me? I work on ubuntu 18.04 and I have R 3.4.4 version.
Rcpp) and create a package.sourceCpp()wasn't meant for a multiple files. Making R packages that call C++ via Rcpp is simple; look athelp("Rcpp::Rcpp.package.skeleton")or the Rcpp package vignette or this answer (disclaimer: mine) with an example of making a package from scratch with C++ & Fortran codeRcppR package. As Ralf Stubner mentioned, "in the context of Rcpp, compilation and linking is to some extend automated." Someone callingsourceCpp()from R is coming at this from an entirely different context than you're familiar with. At a certain point, the question marked as dupe and the things you mention could be helpful, but not without understanding how this mixes with R/Rcpp specific issues.