2014-04-04 19 views
1

我一直在試圖使用Rcpp包將我的一些c/C++代碼的功能擴展到R中。如何在R中包含和鏈接sourceCpp Rcpp

但我在與包含頭文件的問題和聯繫

我做了下面的例子,說明我的問題:

  1. 我有由.H的原始C程序文件和.c文件

headerfile:add.h

int add(int a,int b); 

的CFile:add.c

int add(int a,int b){ 
    return a+b; 
} 
  1. C++函數的 '接口',應與功能性連接我的電話[R在我的C文件

monkey.cpp

#include <cstdio> 
#include <Rcpp.h> 
#include "add.h" 
using namespace Rcpp; 

// [[Rcpp::export]] 
NumericVector monkey(std::string string1,std::string string2) { 

    //below not important 
    NumericVector rary(5); 
    for(int i=0;i<5;i++) 
    rary[i] = i; 
    return rary; 
} 
  1. 試圖使用「猴子」功能的Rcode

monkey.R

nam1 <- "nam1" 
nam2 <- "nam2" 

Rcpp::sourceCpp("monkey.cpp") 

monkey(nam1,nam2) 

Q1。是否可以使用sourceCpp函數爲gcc編譯指定-I標誌?

q2。如果我的接口cpp文件依賴於其他.o文件,是否可以使用sourceCpp函數與這些文件進行鏈接?

回答

1

快速的:

  • sourceCpp()爲小的測試應用程序;它可以使用一組提供擴展插件的擴展。

  • 所以,你可能需要編寫一個自定義插件頭文件,以及可能是矯枉過正,因爲......

  • R中更復雜的代碼組織通過封裝典型的做法。你應該考慮它。

Section 3.5 in the Rcpp Attributes vignette擁有所有選項。

+0

好超,謝謝你的幫助。 – user3194934