Undefined reference to errors

Hello there,

I am new to ROOT and I feel like I have asked this question a couple of times already, in different disguises. I will try to be as specific as possible. I downloaded the stand alone version of Minuit2 from github and built it according to the directions given there.

I am trying to compile the following piece of code

#include "Minuit2/FunctionMinimum.h"
#include "Minuit2/MnUserParameterState.h"
#include "Minuit2/MnPrint.h"
#include "Minuit2/MnMigrad.h"
#include "Minuit2/FCNBase.h"

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include <sstream>

using namespace std;

using namespace ROOT::Minuit2;

class MinFcn : public FCNBase {


  const std::vector< std::vector<double> > & _data;


  MinFcn(const std::vector< std::vector<double> > & data) : _data(data) {}

  ~MinFcn() {}

  double operator() (const std::vector<double> & params) const {

    double X2 = 0.0;
    for (int i=0;i<_data[0].size();i++) {
      double x = _data[0][i];
      //double y = _data[1][i];
      double f = params[0]*x+params[1];
      X2 += (_data[1][i]-f)*(_data[1][i]-f);
    //cout << "X2 = " << X2 << endl;
    return X2;
    //set to 1 for chi-sq and 0.5 for log-likelihood
  double Up() const {return 1.0;}


int main() {
  vector< vector<double> > data(2);

  MinFcn fcn(data);
  vector<double> params(2,1.0);
  vector<double> err(2,0.1);
  params[0] = 1.0;
  params[1] = 1.0;
  MnMigrad migrad(fcn, params, err);
  FunctionMinimum minParams = migrad();
  cout << " chi^2 = " << minParams.Fval() << endl;
  cout << " at " << minParams.UserState().Value(0) << " " << minParams.UserState().Value(1) << endl;

  return 0;

I called my file HelloWorld.cpp and I am trying to compile it with the following command:

g++ -I/Minuit2/inc -L/Minuit2/lib -lMinuit2 -o HelloWorld HelloWorld.cpp -std=c++11

and I get the following errors:

/tmp/ccn8WY50.o: In function `main':
HelloWorld.cpp:(.text+0x47f): undefined reference to `ROOT::Minuit2::MnUserParameterState::Value(unsigned int) const'
HelloWorld.cpp:(.text+0x4a3): undefined reference to `ROOT::Minuit2::MnUserParameterState::Value(unsigned int) const'
/tmp/ccn8WY50.o: In function `ROOT::Minuit2::BasicFunctionMinimum::UserState() const':
HelloWorld.cpp:(.text._ZNK4ROOT7Minuit220BasicFunctionMinimum9UserStateEv[_ZNK4ROOT7Minuit220BasicFunctionMinimum9UserStateEv]+0x9c): undefined reference to `ROOT::Minuit2::MnUserParameterState::MnUserParameterState(ROOT::Minuit2::MinimumState const&, double, ROOT::Minuit2::MnUserTransformation const&)'
/tmp/ccn8WY50.o: In function `ROOT::Minuit2::MnApplication::~MnApplication()':
HelloWorld.cpp:(.text._ZN4ROOT7Minuit213MnApplicationD2Ev[_ZN4ROOT7Minuit213MnApplicationD5Ev]+0xd): undefined reference to `vtable for ROOT::Minuit2::MnApplication'
/tmp/ccn8WY50.o: In function `ROOT::Minuit2::ModularFunctionMinimizer::~ModularFunctionMinimizer()':
HelloWorld.cpp:(.text._ZN4ROOT7Minuit224ModularFunctionMinimizerD2Ev[_ZN4ROOT7Minuit224ModularFunctionMinimizerD5Ev]+0xd): undefined reference to `vtable for ROOT::Minuit2::ModularFunctionMinimizer'

Can anybody tell me what I am doing wrong?


g++ -std=c++11 -I/Minuit2/inc HelloWorld.cpp -o HelloWorld -L/Minuit2/lib -lMinuit2
1 Like

Sir, you are a savior! That fixed the problem. Could you explain why the order of the commands matters here?

Thank you so much.