Rcpp - 在包结构之外编译
Rcpp - Compiling outside the structure of a package
我有一个关于在包结构之外使用Rcpp的C++代码的问题。
为了澄清我的疑问,请考虑下面的C++代码(test.cpp
(:
// [[Rcpp::depends(RcppGSL)]]
#include <Rcpp.h>
#include <numeric>
#include <gsl/gsl_sf_bessel.h>
#include <RcppGSL.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
return x * 2;
}
// [[Rcpp::export]]
double my_bessel(double x){
return gsl_sf_bessel_J0 (x);
}
// [[Rcpp::export]]
int tamanho(NumericVector x){
int n = x.size();
return n;
}
// [[Rcpp::export]]
double soma2(NumericVector x){
double resultado = std::accumulate(x.begin(), x.end(), .0);
return resultado;
}
// [[Rcpp::export]]
Rcpp::NumericVector colNorm(const RcppGSL::Matrix & G) {
int k = G.ncol();
Rcpp::NumericVector n(k); // to store results
for (int j = 0; j < k; j++) {
RcppGSL::VectorView colview = gsl_matrix_const_column (G, j);
n[j] = gsl_blas_dnrm2(colview);
}
return n; // return vector
}
上面的代码在包的结构中起作用。众所周知,使用Rcpp::compileAttributes()
创建文件RcppExports.cpp
.因此,我将可以访问 R. 环境中的功能。
我的兴趣是在包框架之外使用使用 Rcpp实现的C++函数。为此,我使用g ++
编译器编译了C++代码,如下所示:
g++ -I"/usr/include/R/" -DNDEBUG -I"/home/pedro/R/x86_64-pc-linux-gnu-library/3.5/Rcpp/include" -I"/home/pedro/Dropbox/UFPB/Redes Neurais e Análise de Agrupamento/Rcpp" -I /home/pedro/R/x86_64-pc-linux-gnu-library/3.5/RcppGSL/include -D_FORTIFY_SOURCE=2 -fpic -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt -c test.cpp -o test.o -lgsl -lgslcblas -lm
g++ -shared -L/usr/lib64/R/lib -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -o produto.so produto.o -L/usr/lib64/R/lib -lR -lgsl -lgslcblas -lm
编译成功,未发出警告消息。这样,就生成了test.o
和test.so
文件。已经在 R 中使用.Call
接口,我做到了:
dyn.load("test.so")
my_function <- function(x){
.Call("soma2",x)
}
尝试使用my_function ()
函数时,会出现错误,指出soma2
不在加载表中。有没有办法在包框架之外创建RcppExports.cpp
文件?我想正确的方法是RcppExports.cpp
编译代码而不是test.cpp
.
提前谢谢。
如果您在软件包之外工作,您可以简单地使用Rcpp::sourceCpp(<file>)
.这将负责为您编译、链接和设计 R 包装器。通过您的文件,我得到:
> Rcpp::sourceCpp("test.cpp")
> soma2(1:5)
[1] 15
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 从函数中全局删除并重新实例化数组结构,而无需在编译时知道数组的大小
- C++编译大型结构时内存使用过多
- 如何在 C++11 编译时构建具有递增值的整数成员的结构序列
- C 嵌套结构上的 g++ 编译错误
- 无法在窗口上编译结构 - 缺少 ltdl.h
- 编译时检查以确保结构中的任何位置都没有填充
- Rcpp - 在包结构之外编译
- 当编译时已知引用占用结构中的空间时,是否错过了优化?
- 尝试初始化结构向量时出现编译错误
- ARM C++编译器不会编译具有可变常量成员的结构
- Jetson 工具链文件夹结构和交叉编译时的 libgomp 链接器错误
- std::结构复杂,使编译速度变慢
- 为什么我无法编译结构化绑定?
- 如何修复编译由结构组成的 std::p air 时出现的错误
- NVCC编译了特征库,并在运行时失败的结构中的MatrixxD大小
- 使用std :: map时,包含unique_ptr的结构中的默认破坏者会导致编译错误
- C++代码只能针对特定的 CPU 体系结构进行编译.有没有办法将其编译为所有架构
- 编译结构的时枚举映射
- 使用emscripten编译结构时出错