如果我的c++代码使用C++11 std::regex,而我到目前为止还没有找到解决方法,那么当我在Windows上运行使用Rcpp构建的函数时,我会遇到.Call问题。
与之前关于类似问题的问题不同,我既没有构建也没有链接问题。使用C++11插件可以很好地构建和链接Rcpp包,使包在我的平台上变得有用。当不使用std::regex时,constexpr和C++11特定的函数(如std::stoi )不会导致问题。
使用Windows boost库时,我遇到了链接问题,即使在指定PKG_LIBS="-L/path/ to /boost/libs -lboost_regex“时也是如此,所以我更喜欢使用std::regex。
相同的包使用vanilla std::regex或boost::regex在linux上构建、安装和运行得很好。
不幸的是,我在优秀的Rcpp图库示例中找不到解决方案。
Windows平台是:
R version 3.2.3 (2015-12-10)
x86_64-w64-mingw32/x64 (64-bit) 在以下环境下运行:
Windows >= 8 x64 (build 9200)
Rcpp_0.12.3
Rtools 3.3.0.1959 running g++ 4.9.3 (x86_64-posix-seh,
built by MinGW-W64 project), normally C++11-compatible.
PKG_CXXFLAGS="-std=c++11"除了g++ (5.3版)之外,linux平台与之类似。
下面是用于复制的简化代码块。
#include <Rcpp.h>
#if defined(__linux__) && ! defined(FORCE_STL_BUILD)
#include <boost/regex.hpp>
#define reglib boost
#else
#include <regex>
#define reglib std
#endif
#include <string>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
constexpr int a[3]= {2, 10, 15};
// [[Rcpp::export]]
int my_test(int prop, const std::string& index)
{
#ifndef NO_REG
static const reglib::regex test {"H.*A", reglib::regex::icase};
#endif
int index_int = std::stoi(index) + a[1] + prop;
return index_int;
}当使用-DNO_REG构建时,此代码运行正常。否则,调用test::my_test(1, "1000")将返回:
`Error in .Call("test_my_test", PACKAGE = "test", prop, index) :
"test_my_test" not available for .Call() for package "test"` 编辑:
Rcpp::source("cppfile")R控制台:
Rcpp::Rcpp.package.skeleton("test", attributes=TRUE, example_code=FALSE, cpp_files="test.cpp")
Rcpp::compileAttributes("test") CMD控制台:
REM paths to R/bin/x64 and Rtools/bin, Rtools/mingw_64/bin added to PATH
set PKG_CXXFLAGS=-std=c++11
R CMD build test
R CMD INSTALL test_1.0.tar.gz 其他编辑:
一旦在C++代码中声明了正则表达式,就会出现.Call问题。使用它或不使用它(就像在std::regex_match中一样)都不会有任何变化。
发布于 2016-02-22 11:55:49
你能再试着解开这个吗?你在这里混合了很多东西。
先用更新的g++ 4.9.3编译器试试从R开始的“just”C++,看看这是否能让你像你希望的那样使用Boost。你的用例是本地的和非标准的,所以你必须解决这个问题。我们通常只推荐使用不带链接的BH。
我在这里实际上看不到Rcpp问题。您只是将(工作的、经过测试的、可信的) Rcpp设置推到了一个尚未使用过的角落。因此,您可能需要自己解决一些问题。
还要注意的是,用于R的g++ 4.9.3还没有真正发布。
https://stackoverflow.com/questions/35545329
复制相似问题