在 Windows 上运行使用 Rcpp 构建的函数时,我遇到了 .Call 问题,如果我的 c++ 代码使用 C++11std::regex
并且到目前为止我还没有找到出路。
与之前关于类似问题的问题不同,我既没有构建问题也没有链接问题。Rcpp 包使用 C++11 插件构建和链接良好,在我的平台上制作可用的包。constexpr
和 C++11 特定的函数,如不使用std::stoi
时不会出现问题std::regex
。
使用 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"`
编辑:
1. 问题集中在 std::regex 上。Boost 问题只是偶然的评论。
2.问题只在打包后出现,不使用Rcpp::source("cppfile")
3.打包代码:
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 中)不会改变。