TMinuit gives "linker command failed with exit code 1 (use -v to see invocation)" when compiled with C++


_ROOT Version:_6.14/06
Platform: MAC OS MOJAVE
Compiler: g++


I am trying to run a code which does minimization using TMinuit in a .cpp file. Whenever I run the .C version of the same code in the root interpreter it works fine but whenever I compile it the usual C++ way of using g++ along with ROOT libraries I get the following error:

Undefined symbols for architecture x86_64:
  "TMinuit::SetFCN(void (*)(int&, double*, double&, double*, int))", referenced from:
      _main in min-c0bfd0.o
  "TMinuit::mnexcm(char const*, double*, int, int&)", referenced from:
      _main in min-c0bfd0.o
  "TMinuit::mnparm(int, TString, double, double, double, double, int&)", referenced from:
      _main in min-c0bfd0.o
  "TMinuit::mnstat(double&, double&, double&, int&, int&, int&)", referenced from:
      _main in min-c0bfd0.o
  "TMinuit::TMinuit(int)", referenced from:
      _main in min-c0bfd0.o
  "TMinuit::~TMinuit()", referenced from:
      _main in min-c0bfd0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is how I am compiling my code:

g++ -o min min.cpp quadratic.cpp -I$ROOTSYS/include -L$ROOTSYS/lib `root-config --cflags --glibs`

My original source file looks like:

#include <iostream>
#include <fstream>
#include <vector>
#include "quadratic.h"
#include "quadratic.cpp"

// #include <TMinuit.h>
#include <TFile.h>
#include <TMinuit.h>
#include <TApplication.h>
#include <TROOT.h>
using namespace std;

#define N_BINS 500
static QuadraticFunc poly;
static QuadraticFunc *polyPtr;

std::vector<double> bin_center;
std::vector<double> bin_content;
std::vector<double> bin_error;

// Function for calculating chi-square.
double poly_integral = 3 / 4.;
void chi2_fn(int &npar, double *gin, double &f, double *par, int iflag)
{
    double delta = 0;
    double chi_square = 0;
    double t;

    poly.params.a = par[0];
    poly.params.b = par[1];
    poly.params.c = par[2];

    for (int i = 5; i < (N_BINS - 5); ++i)
    {
        t = bin_center[i];
        delta = (bin_content[i] - poly_integral * (poly.params.a * t * t + poly.params.b * t + poly.params.c)) /
                bin_error[i];
        chi_square += delta * delta;
    }
    f = chi_square;
}

int main()
{
    double bin_center_hold;
    double bin_content_hold;
    double bin_error_hold;

    ifstream fin("quad_hist_features.dat");

    while (fin >> bin_center_hold >> bin_content_hold >> bin_error_hold)
    {
        bin_center.push_back(bin_center_hold);
        bin_content.push_back(bin_content_hold);
        bin_error.push_back(bin_error_hold);
    }

    double arglist[10];
    int ierflg = 0;

    TMinuit minuit(3);

    minuit.SetFCN(chi2_fn);

    arglist[0] = 2.30;

    minuit.mnparm(0, "a", -1.0, 0.1, 0, 0, ierflg);
    minuit.mnparm(1, "b", 2.0, 0.1, 0, 0, ierflg);
    minuit.mnparm(2, "c", 0.0, 0.1, 0, 0, ierflg);

    minuit.mnexcm("MIGRAD", arglist, 0, ierflg);

    minuit.mnexcm("MINOS", arglist, 0, ierflg);

    double fmin, fedm;
    double errdef;
    int nparv, nparx;
    int fstat;

    minuit.mnstat(fmin, fedm, errdef, nparv, nparx, fstat);

    cout << "\n\n";
    cout << "Results of MINUIT minimisation\n";
    cout << "-------------------------------------\n\n";

    cout << " Minimal function value:                 " << fmin << endl;
    cout << " Estimated difference to true minimum:   " << fedm << endl;
    cout << " Number of parameters:                   " << nparv << endl;
    cout << " Error definition (Fmin + Delta):        " << errdef << endl;
    if (fstat == 3)
    {
        cout << " Exact covariance matrix." << endl;
    }
    else
    {
        cout << " No/error with covariance matrix." << endl;
        cout << " Error code:    " << fstat << endl;
    }
}

This code is basically fitting a quadratic polynomial through a set of data points.
Also, .I built root with cmake. I tried compiling this code with cmake as well but still getting the same error

Add “-lMinuit” in the end of your linking command line, e.g. use:

`root-config --cxx --cflags` -o min min.cpp quadratic.cpp `root-config --glibs` -lMinuit

Thank you for the quick reply but I also tried compiling with these flags:

g++ -o min min.cpp quadratic.cpp -std=c++11 -I$ROOTSYS/include -L$ROOTSYS/lib -lCore -lHist -lMatrix -lMinuit -lGraf -lm -ldl

But still got the same linking errors.

As you suggested I tried:

`root-config --cxx --cflags` -o min min.cpp quadratic.cpp `root-config --glibs` -lMinuit

But got this error:

duplicate symbol __ZN13QuadraticFuncD2Ev in:
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/min-8ffb71.o
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/quadratic-74dc83.o
duplicate symbol __ZN13QuadraticFuncC2Ev in:
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/min-8ffb71.o
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/quadratic-74dc83.o
duplicate symbol __ZN13QuadraticFuncD1Ev in:
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/min-8ffb71.o
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/quadratic-74dc83.o
duplicate symbol __ZN13QuadraticFuncC1Ev in:
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/min-8ffb71.o
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/quadratic-74dc83.o
duplicate symbol __ZN13QuadraticFunc10polynomialEPKNS_5CoeffEPd in:
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/min-8ffb71.o
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/quadratic-74dc83.o
duplicate symbol __ZN13QuadraticFunc9ParamShowEPKNS_5CoeffE in:
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/min-8ffb71.o
    /var/folders/lw/s6w5mpg16q9cr7hk5jnhbl4r0000gn/T/quadratic-74dc83.o
ld: 6 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Well, “duplicate symbol” says that you define it twice so, remove #include "quadratic.cpp" from your “main.cpp”.

It worked. This is really embarrassing because I just left "quadratic.cpp" header in "main.cpp" when I was trying the root version "main.C" of the same code in root interpreter (it will throw errors if you don’t include the class implementation file) . I just forgot to remove it in my "main.cpp".

Thank you so much! Really appreciate it.:smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.