C++ 在同一个R包中组合C代码
假设我在两个不同的C++ 在同一个R包中组合C代码,c++,r,C++,R,假设我在两个不同的R包中有两个独立的cpp代码: (请不要从字面上理解这些示例,这些示例旨在 我的问题的最低版本) #包括 #包括 使用名称空间std; 使用名称空间特征; 使用Eigen::VectorXf; 使用本征::矢量; 使用Eigen::RowVectorXf; 浮动Fmedian(矢量x和x){ 常量int n=x.行(); 常数int半=(n+1)/2-1; 浮动med; std::n_元素(x.data(),x.data()+一半,x.data()+x.size()); 如果(
R
包中有两个独立的cpp代码:
(请不要从字面上理解这些示例,这些示例旨在
我的问题的最低版本)
#包括
#包括
使用名称空间std;
使用名称空间特征;
使用Eigen::VectorXf;
使用本征::矢量;
使用Eigen::RowVectorXf;
浮动Fmedian(矢量x和x){
常量int n=x.行();
常数int半=(n+1)/2-1;
浮动med;
std::n_元素(x.data(),x.data()+一半,x.data()+x.size());
如果((n%2)=1){
med=x(一半);
}否则{
浮动tmp0=x(一半);
float tmp1=x.segment(半+1,半-1).minCoeff();
med=0.5*(tmp0+tmp1);
}
返回医学院;
}
外部“C”{
无效R_快速中值(整数*n,浮点*X,整数*fMet){
矩阵xi=Map(X,*n);
浮点数Um=Fmedian(xi);
*fMet=Um;
}
}
然后,在另一个文件中,我有:
#include <algorithm>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
using Eigen::VectorXf;
using Eigen::VectorXi;
using Eigen::RowVectorXf;
float Fmedian(VectorXf& x){
const int n=x.rows();
const int half=(n+1)/2-1;
float med;
std::nth_element(x.data(),x.data()+half,x.data()+x.size());
if((n%2)==1){
med=x(half);
} else {
float tmp0=x(half);
float tmp1=x.segment(half+1,half-1).minCoeff();
med=0.5*(tmp0+tmp1);
}
return med;
}
float Fmad(VectorXf& x,float& med){
const int n=x.rows();
const int half=(n+1)/2-1;
float mad;
x-=med;
x=x.cwiseAbs();
std::nth_element(x.data(),x.data()+half,x.data()+x.size());
if((n%2)==1){
mad=x(half);
} else {
float tmp0=x(half);
float tmp1=x.segment(half+1,half-1).minCoeff();
mad=0.5*(tmp0+tmp1);
}
return(mad*1.4826);
}
extern "C"{
void R_Fastmad(int* n,float* X,int* fMet){
MatrixXf xi=Map<VectorXf>(X,*n);
float U1=Fmedian(xi);
float U2=Fmad(xi,U1);
*fMet=U2;
}
}
#包括
#包括
使用名称空间std;
使用名称空间特征;
使用Eigen::VectorXf;
使用本征::矢量;
使用Eigen::RowVectorXf;
浮动Fmedian(矢量x和x){
常量int n=x.行();
常数int半=(n+1)/2-1;
浮动med;
std::n_元素(x.data(),x.data()+一半,x.data()+x.size());
如果((n%2)=1){
med=x(一半);
}否则{
浮动tmp0=x(一半);
float tmp1=x.segment(半+1,半-1).minCoeff();
med=0.5*(tmp0+tmp1);
}
返回医学院;
}
浮动Fmad(矢量XF&x、浮动&med){
常量int n=x.行();
常数int半=(n+1)/2-1;
疯狂地漂浮;
x-=med;
x=x.cwiseAbs();
std::n_元素(x.data(),x.data()+一半,x.data()+x.size());
如果((n%2)=1){
mad=x(一半);
}否则{
浮动tmp0=x(一半);
float tmp1=x.segment(半+1,半-1).minCoeff();
mad=0.5*(tmp0+tmp1);
}
返回(mad*1.4826);
}
外部“C”{
void R_Fastmad(int*n,float*X,int*fMet){
矩阵xi=Map(X,*n);
浮点数U1=Fmedian(xi);
浮点数U2=Fmad(xi,U1);
*fMet=U2;
}
}
现在,我想将这两个函数合并到一个包中。我什么时候编译
第二个代码使用R CMD
,我将得到一个错误,用于Fmedian
该函数已在第一个文件中定义。最重要的是什么
将这两个文件链接在一起的简单方法?如果我阅读正确,您的
Fmedian
实现在这两个文件中完全相同。但是编译器实际上并不知道这一点,因为它们可能是不同的,并导致歧义,从而导致错误。要解决这个问题,您应该将两个实现统一为一个
一种方法是:
// the first file
#include <algorithm>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
using Eigen::VectorXf;
using Eigen::VectorXi;
using Eigen::RowVectorXf;
// Forward-declare Fmedian as an external function.
// This means that its definition will be found
// in some other file (C++ translation unit) during compilation.
extern float Fmedian(VectorXf& x);
extern "C" {
void R_Fastmedian(int* n,float* X,int* fMet){
MatrixXf xi=Map<VectorXf>(X,*n);
float Um=Fmedian(xi);
*fMet=Um;
}
}
//第一个文件
#包括,
// the first file
#include <algorithm>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
using Eigen::VectorXf;
using Eigen::VectorXi;
using Eigen::RowVectorXf;
// Forward-declare Fmedian as an external function.
// This means that its definition will be found
// in some other file (C++ translation unit) during compilation.
extern float Fmedian(VectorXf& x);
extern "C" {
void R_Fastmedian(int* n,float* X,int* fMet){
MatrixXf xi=Map<VectorXf>(X,*n);
float Um=Fmedian(xi);
*fMet=Um;
}
}