Включает ли R cpp типы данных C ++, такие как int, std :: string et c. в качестве входных или выходных параметров? - PullRequest
0 голосов
/ 14 февраля 2020

Я пытался что-то вроде ниже (грубый пример):

test. cpp:

#include <Rcpp.h> 
#include <string>

// [[Rcpp::export]]
RcppExport int R_load_lib(SEXP R_strDllPath);

int R_load_lib(SEXP R_strDllPath)
{
   int nStatus; 
   std::string strDllPath = Rcpp::as<std::string>(R_strDllPath);
   nStatus = LoadLibrary(strDllPath.c_str());

  Rcpp::Rcout << "LoadLib status is " << nStatus << "\n";//This get printed and then crash happens

   return nStatus;
}

Шаги компиляции для кода C ++ (С использованием cygwin):

g++ -static-libgcc -static-libstdc++  -L$(R_HOME)/bin/x64 -lR
-L$(R_HOME)/library/Rcpp/libs/x64 -lRcpp -fPIC -shared test.o -o test.dll

test.R:

dyn.load("test.dll")
status<-.Call("R_load_lib", "D:/R_test/sample.dll")

1 Ответ

0 голосов
/ 17 февраля 2020

Мне кажется, я нашел ответ на свой вопрос. Я прошел через http://dirk.eddelbuettel.com/code/rcpp/Rcpp-introduction.pdf. В этом pdf есть несколько примеров, которые показывают, как мы должны использовать SEXP для входа или выхода из C / C ++ в R. Я попробовал то же самое, и теперь он работает нормально без cra sh. Так что мой код C ++ теперь выглядит так:

#include <Rcpp.h> 
#include <string>

// [[Rcpp::export]]
RcppExport SEXP R_load_lib(SEXP R_strDllPath);

SEXP R_load_lib(SEXP R_strDllPath)
{
   int nStatus; 
   std::string strDllPath = Rcpp::as<std::string>(R_strDllPath);
   nStatus = LoadLibrary(strDllPath.c_str());

  Rcpp::Rcout << "LoadLib status is " << nStatus << "\n";//This get printed and then crash happens

   return Rcpp::NumericVector(nStatus);
}
...