Rcpp 是否启用 C++ 数据类型(如 int、std::string 等)作为输入或输出参数?

Does Rcpp enables C++ datatypes such as int, std::string etc. as input or output parameters?

我正在尝试如下(一个粗略的例子):

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")

我想我找到了问题的答案。我经历了 http://dirk.eddelbuettel.com/code/rcpp/Rcpp-introduction.pdf。在这个 pdf 中,有一些例子展示了我们应该如何使用 SEXP 从 C/C++ 到 R 的输入或输出。我已经尝试过相同的方法,现在工作正常,没有崩溃。所以我的 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);
}