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 に答える