Undefined reference when using Raspbian compile root based program

Dear experts

I tried to compile some program on my Raspberry pi which is using Raspbian system. And I installed root from source.

I set the r++ as:

alias r++="`root-config --cxx --cflags --libs` -lMinuit2"

then I run r++ something.cxx -o something

I got these errors:

/usr/bin/ld: /tmp/ccQiC1IL.o: in function `main':
binary2root.cpp:(.text+0x720): undefined reference to `TFile::TFile(char const*, char const*, char const*, int)'
/usr/bin/ld: binary2root.cpp:(.text+0x76c): undefined reference to `TTree::TTree(char const*, char const*, int, TDirectory*)'
/usr/bin/ld: binary2root.cpp:(.text+0x840): undefined reference to `Form(char const*, ...)'
/usr/bin/ld: binary2root.cpp:(.text+0x898): undefined reference to `Form(char const*, ...)'
/usr/bin/ld: binary2root.cpp:(.text+0x8ec): undefined reference to `Form(char const*, ...)'
/usr/bin/ld: binary2root.cpp:(.text+0x940): undefined reference to `Form(char const*, ...)'
/usr/bin/ld: binary2root.cpp:(.text+0x998): undefined reference to `Form(char const*, ...)'
/usr/bin/ld: /tmp/ccQiC1IL.o:binary2root.cpp:(.text+0x9f0): more undefined references to `Form(char const*, ...)' follow
/usr/bin/ld: /tmp/ccQiC1IL.o: in function `main':
binary2root.cpp:(.text+0xd74): undefined reference to `TObject::operator delete(void*)'
/usr/bin/ld: binary2root.cpp:(.text+0xd90): undefined reference to `TObject::operator delete(void*)'
/usr/bin/ld: /tmp/ccQiC1IL.o: in function `__static_initialization_and_destruction_0(int, int)':
binary2root.cpp:(.text+0xe10): undefined reference to `TVersionCheck::TVersionCheck(int)'
/usr/bin/ld: /tmp/ccQiC1IL.o: in function `TObject::operator new(unsigned long)':
binary2root.cpp:(.text._ZN7TObjectnwEm[_ZN7TObjectnwEm]+0x10): undefined reference to `TStorage::ObjectAlloc(unsigned long)'
/usr/bin/ld: /tmp/ccQiC1IL.o: in function `ROOT::Internal::TDirectoryAtomicAdapter::TDirectoryAtomicAdapter()':
binary2root.cpp:(.text._ZN4ROOT8Internal23TDirectoryAtomicAdapterC2Ev[_ZN4ROOT8Internal23TDirectoryAtomicAdapterC5Ev]+0xc): undefined reference to `TDirectory::GetSharedLocalCurrentDirectory()'
collect2: error: ld returned 1 exit status

Seems that libs are not loaded correctly, how can I solve this? Thanks in advance.


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.30/04
Platform: Raspbian
Compiler: root-config


Hi Siyuan,

I am sorry to see you experience this behaviour.
What is the expanded content of your r++ command? The errors the linker prompts are crystal clear…

Cheers,
Danilo

Thank you for your reply! the r++ command is an alias as:

"`root-config --cxx --cflags --libs` -lMinuit2"

and its full expanded version is:

c++ -pthread -std=c++17 -fsigned-char -I/home/ssun/Projects/root/install/include -L/home/ssun/Projects/root/install/lib -lCore -lImt -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lROOTVecOps -lTree -lTreePlayer -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lMultiProc -lROOTDataFrame -Wl,-rpath,/home/ssun/Projects/root/install/lib -pthread -lm -ldl -rdynamic -lMinuit2

Hi Siyuan,

Thanks: all the flags seem to be there, as expected.
Could you try to add -L /home/ssun/Projects/root/install/lib to point the linker to the directory where the libraries can be found?

Best,
D

I don’t understand what do you mean…it was already in my r++ command, do you mean you want me to try to add -L /home/ssun/Projects/root/install/lib outside r++ command which means it will be called twice?

By the way, I tried but get the same errors as before…

I see the path now, sorry.
This could be related to your setup. Have you tried to build a very simple executable that just creates a histogram and does something with it to avoid the compiler optimizes it away (e.g. print its title)?
We could start debugging from something very simple.

I used a very simple code:

#include <TFile.h>
int main(){
    TFile* f = new TFile("test.root");
    return 0;
}

then compile it with r++ command above, then gives me the same error:

/usr/bin/ld: /tmp/ccBIkmYY.o: in function `main':
test.cxx:(.text+0x40): undefined reference to `TFile::TFile(char const*, char const*, char const*, int)'
/usr/bin/ld: test.cxx:(.text+0x60): undefined reference to `TObject::operator delete(void*)'
/usr/bin/ld: /tmp/ccBIkmYY.o: in function `__static_initialization_and_destruction_0(int, int)':
test.cxx:(.text+0xb8): undefined reference to `TVersionCheck::TVersionCheck(int)'
/usr/bin/ld: /tmp/ccBIkmYY.o: in function `TObject::operator new(unsigned long)':
test.cxx:(.text._ZN7TObjectnwEm[_ZN7TObjectnwEm]+0x10): undefined reference to `TStorage::ObjectAlloc(unsigned long)'
collect2: error: ld returned 1 exit status

please notice that when I use root -l to enter the interactive mode I can create TFile without error, and the error above indicate that include files are successfully loaded only libs are not.

any advice?

Well, I also reached my EOL but, even though ROOT doesn’t have my “subscription”, here it is.
Replace your “alias” with this “function” (and re-login):

function r++ { $(root-config --cxx --cflags) $@ $(root-config --libs) -lMinuit2 ; }

Thank you for the reply! Do you mean that I should add this in my .bashrc? If so I tried and get exactly the same error above…

I think this is caused by incorrectly loading the libs, is there a way to check if root-config is loading libs from the correct path and correct name of files?

Uh…any idea?

This is solved by adding such function to .bashrc:

function r++ { $(root-config --cxx) $@ $(root-config --cflags --libs) -lMinuit2 ; }

seems that libs must be loaded after source code been compiled, but it was not necessary at any other machine such as MacOS and CentOS…
So I think only Ubuntu or Debian based system will have such problem?