Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:58:25+00:00 2026-06-17T09:58:25+00:00

Say I have two independent cpp codes in two different R packages: (please do

  • 0

Say I have two independent cpp codes in two different R packages:
(please do not take these examples literally, these are meant to be
a minimal version of my question).

#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;
}
extern "C"{
    void R_Fastmedian(int* n,float* X,int* fMet){
        MatrixXf xi=Map<VectorXf>(X,*n);        
        float Um=Fmedian(xi);
        *fMet=Um;
    }
}

then, in another file, i have:

#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;
    }
}

Now, i want to combine these two functions in a package. When i will compile
the second code using R CMD, i will get an error for Fmedian to the effect
that this function is already defined in the first file. What is the most
straightforward way to link these two files together?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:58:27+00:00Added an answer on June 17, 2026 at 9:58 am

    If I’m reading correctly, your Fmedian implementations are exactly the same in both files. But the compiler doesn’t actually know that, for it they may be different and cause ambiguity, hence the error. To fix it, you should unify both implementations into one.

    One way to do that would be this:

    // 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;
        }
    }
    

    And your second file remains the same.


    See also Externing functions in C++, How does the linker know where is the definition of an extern function?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two different cpp files. Both declare classes with the same name,
I have two models (say A and B) which are independent and have independent
Say I have two classes with different names but the exactly same structure. It
Say I have two independent classes: class Foo { int bar; } class Baz
Let say I have two different hashsets as shown below how can I check
I have two images with different IDs, lets say #pic1 and #pic2. I tried
Let's say I have two methods - getCurrentValue(int valueID) updateValues(int changedComponentID) These two methods
Let's say I have a two wheeled object, where each wheel has an independent
Say we have two activities, Activity1 and Activity2. In Activity1's onClick() method, we have
Say I have two Java apps that I wrote: Ping.jar and Pong.jar and they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.