[英]Rcpp - Compiling outside the structure of a package
I'm having a question about using a C ++ code using Rcpp outside of a package structure. 我有一个关于在包结构外部使用Rcpp的C ++代码的问题。
To clarify my doubt, consider the C ++ code ( test.cpp
) below: 为了澄清我的疑问,请考虑以下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
}
The above code works when it is inside the structure of a package. 上面的代码在包结构内部时有效。 As we know, using the
Rcpp::compileAttributes()
the file is created RcppExports.cpp
. 众所周知,使用
Rcpp::compileAttributes()
创建文件RcppExports.cpp
。 So I will have access to the functions in the R. environment. 因此,我将可以访问R.环境中的功能。
My interest is to use the C++ function implemented using the Rcpp outside the framework of a package. 我的兴趣是在程序包框架之外使用通过Rcpp实现的C ++函数。 For this I compiled the C ++ code using the
g ++
compiler as follows: 为此,我使用
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
The compilation occurred successfully and no warning message was issued. 编译成功进行,没有发出警告消息。 In this way, the
test.o
and test.so
files were generated. 这样,生成了
test.o
和test.so
文件。 Already in R, using the .Call
interface, I did: 在R中,已经使用
.Call
接口实现了:
dyn.load("test.so")
my_function <- function(x){
.Call("soma2",x)
}
When trying to use the my_function ()
function, an error occurs stating that soma2
is not in the load table. 尝试使用
my_function ()
函数时,发生错误,指出soma2
不在加载表中。 Is there any way to create the RcppExports.cpp
file outside the framework of a package? 有什么方法可以在程序包框架之外创建
RcppExports.cpp
文件? I guess the correct one would be to have compiled the code RcppExports.cpp
and not test.cpp
. 我猜正确的方法是编译代码
RcppExports.cpp
而不是test.cpp
。
Thanks in advance. 提前致谢。
If you are working outside of a package you can simply use Rcpp::sourceCpp(<file>)
. 如果您在软件包之外工作,则可以使用
Rcpp::sourceCpp(<file>)
。 This will take care of compilating, linking and prociding an R wrapper for you. 这将为您编译,链接和处理R包装器。 With your file I get:
有了您的文件,我得到:
> Rcpp::sourceCpp("test.cpp")
> soma2(1:5)
[1] 15
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.