1

In R, using Rcpp to access C++ code, without putting all of the C++ code on a single file, how can I control the order which the files are used when compilation takes place.

Lets say I have 2 methods, methodPrimary and methodSecondary, I want to put each method on separate files methodPrimary.cpp and methodSecondary.cpp, but let say function methodPrimary uses function methodSecondary, as below:

methodSecodary.cpp

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int methodSecondary(int i){
  return(i);
}

methodPrimary.cpp

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int methodPrimary(int i){
  return 2*methodSecondary(i);
}

I get an error thrown, saying methodSecondary not declared in this scope, which is understandable, since in each of the two files, there is no reference to the other. respectively.

My initial presumption was that the Rcpp compiler would handle all of this along with package construction and use of the Collate field, seemingly not the case.

So my question is, what is the correct process to have all the files compiled/processed/declared in the correct order?

4

1 回答 1

2

编译顺序无关紧要。但是每个函数都必须在编译器接受它之前声明。

创建一个声明所有函数的头文件,并将其包含在每个C源文件中。

methods.h

extern int methodPrimary(int);
extern int methodSecondary(int);

在每个C源文件中,在使用函数之前:

#include "methods.h"

您可以使用多个头文件,以便methodSecondary.h仅声明该函数。

于 2013-05-11T02:37:44.397 回答