4

I try to create a R package (using Rcpp). Everything works fine. But now I wrote a c++ function, which I want to call in an other c++ file. So in /src there is:

  • function1 = linearInterpolation.cpp
  • function2 = getSlope.cpp

Let's take function1, which just do a linear interpolation between two points at a specific position.

#include <Rcpp.h>

using namespace Rcpp;

//' @name linearInterpolation
//' @title linearInterpolation
//' @description Twodimensional linearinterpolation for a specific point
//' @param xCoordinates Two x coordinates
//' @param yCoordinates Coresponding two y coordinates
//' @param atPosition The Point to which the interpolation shall be done
//' @return Returns the linear interpolated y-value for the specific point
//' @examples
//' linearInterpolation(c(1,2),c(1,4),3)
//'
//' @export
// [[Rcpp::export]]
double linearInterpolation(NumericVector xCoordinates, NumericVector yCoordinates, double atPosition) {

  // start + delta y / delta x_1 * delta x_2
  return yCoordinates[1] + getSlope(xCoordinates, yCoordinates) * (atPosition - xCoordinates[1]);

}

and the slope is calculated in a different function (file).

#include <Rcpp.h>

using namespace Rcpp;

//' @name getSlope
//' @title getSlope
//' @description Calculates the slopes between two points in 2Dimensions
//' @param xCoordinates Two x coordinates
//' @param yCoordinates Coresponding two y coordinates
//' @return Returns the slope
//' @examples
//' getSlope(c(1,2),c(1,4),3)
//'
//' @export
// [[Rcpp::export]]
double getSlope(NumericVector xCoordinates, NumericVector yCoordinates) {
  return (yCoordinates[1] - yCoordinates[0]) / (xCoordinates[1] - xCoordinates[0]);
}

I do not have any deeper knowledge in Rcpp or c++. I read the Vignette and Writing a package that uses Rcpp I think I also read the right parts but I didn't get it.

Why is the getSlope function not "visible" in the other function - as they are both in the same package. How can I use the getSlope in the other file?

Sorry but I am really stuck.

Thanks and best Regards

Nico

kn1g
  • 358
  • 3
  • 16
  • 1
    You have to [link](http://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work) these two files. It would be easier to put them in the same file. – Verena Haunschmid Jul 14 '15 at 12:54
  • Please tell us the error message you are getting. It is insufficient to simply say "function not visible". What does 'visible' mean? – Aaron McDaid Jul 14 '15 at 12:57
  • Hi Verena, I do not want to put them into the same file. I would end up in a big mess with multiple functions. But thanks I will read throuh your link. Hi Aaron, sure, sorry. The error I get is: _'getSlope' was not declared in this scope_ – kn1g Jul 14 '15 at 13:22

1 Answers1

3

Perhaps you should make another file, a header file .hpp, and put this in it:

#include <Rcpp.h>
using namespace Rcpp;
double getSlope(NumericVector xCoordinates, NumericVector yCoordinates);

or, better still,

#include <Rcpp.h>
double getSlope(Rcpp:: NumericVector xCoordinates, Rcpp:: NumericVector yCoordinates);

and put #include"myheader.hpp" at the top of both of your cpp files. This is in order to declare this function such that both cpp files can see it.


... as they are both in the same package

Just because two things are in the same R package, that does not mean they are in the same C++ translation unit (i.e. cpp file), and the translation unit is the important thing for two C++ functions to see each other. They must be in the same cpp file, or you must use a header file.

Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
  • Ok, this changes something... Now I get some more errors. in the .hpp file: - 2x Numeric Vector was not declared in this scope - exprpession list treated as compound expression in initializier - previous declaration of 'double getSLope' in the getSlope.cpp: - double getSlope redeclared as different kind of symbol Sorry, now I am completely lost... – kn1g Jul 14 '15 at 13:23
  • 1
    I forget something, and I've now updated the answer. Your header file should also include `#include ` as its first lines. This is in order that the header file 'understands what `NumericVector` is. Also, `using namespace Rcpp;` – Aaron McDaid Jul 14 '15 at 13:32