Linker Error: undefined reference to `TDatabasePDG::TDatabasePDG()'

When I compile an executable (see an example below) with

#!/bin/bash
g++ -std=c++17 $1 `root-config --cflags --glibs` -o PT_CHECKS

I get the following linker error:

/cvmfs/sft.cern.ch/lcg/releases/binutils/2.36.1-a9696/x86_64-centos8/bin/ld: /tmp/andreas/ccm3L0aA.o: in function `main':
example.cpp:(.text+0x1a): undefined reference to `TDatabasePDG::GetParticle(int) const'
/cvmfs/sft.cern.ch/lcg/releases/binutils/2.36.1-a9696/x86_64-centos8/bin/ld: /tmp/andreas/ccm3L0aA.o: in function `__static_initialization_and_destruction_0(int, int)':
example.cpp:(.text+0xa6): undefined reference to `TDatabasePDG::TDatabasePDG()'
/cvmfs/sft.cern.ch/lcg/releases/binutils/2.36.1-a9696/x86_64-centos8/bin/ld: example.cpp:(.text+0xb5): undefined reference to `TDatabasePDG::~TDatabasePDG()'
collect2: error: ld returned 1 exit status

which probably means that some library is not included in the --glibs argument (?). I have created a code to recreate the issue: e.g.:

#include <iostream>
#include <TDatabasePDG.h>
TDatabasePDG GDatabase;

int main(int ac, char *av[])
{
	auto new_particle = GDatabase.GetParticle(11); // Electron
	std::cout << new_particle->Mass() << std::endl;
	return 1;
}

Works otherwise flawlessly when I run it with root -x.


ROOT Version: 6.24/06
Platform: Centos08
Compiler: g++ (GCC) 11.1.0


Hello @andrew ,
you can see at the bottom of ROOT: TDatabasePDG Class Reference that this functionality is contained in libEG, which is likely not part of the default set of libraries listed by --libs and --glibs. If you can use the class from the root prompt, then the library is available, you probably just have to link it. Try using e.g. g++ -std=c++17 $1 -o PT_CHECKS $(root-config --cflags --glibs) -lEG.

Cheers,
Enrico

1 Like

Hi @eguiraud,

Works flawlessly and this is exactly what I was looking for! Thanks a lot!

Yours,

Andreas

root-config --evelibs