Makefile trouble

Dear Root users,

I used TTree and TFile in my files, but I always had trouble to compile them. I simplifed my files to several lines which reproduce the errors when I tried to compile my original files. Could you please tell me what is wrong with my file? I am running my files at a Linux Red Hat Enterprise 3 with Intel cpu.

I attached my file and my Makefile is following:
CC = /usr/bin/g++
cc = /usr/bin/gcc

ROOTSYS = /usr/local/root

ROOTINC = (ROOTSYS)/include ROOTLIB = (ROOTSYS)/lib

tryfile: tryfile.o
{CC} -o tryfile tryfile.o -L{ROOTLIB}

tryfile.o: tryfile.cpp
(CC) -I(ROOTINC) -c tryfile.cpp

The errors I got:
/usr/bin/g++ -I/usr/local/root/include -c tryfile.cpp
/usr/bin/g++ -o tryfile tryfile.o -L/usr/local/root/lib
tryfile.o(.text+0x2d): In function tree_init()': : undefined reference toTTree::TTree(char const*, char const*, int)’
tryfile.o(.text+0x58): In function tree_init()': : undefined reference toTObject::operator delete(void*)’
tryfile.o(.text+0x7d): In function tree_init()': : undefined reference toTObject::operator delete(void*)’
tryfile.o(.text+0x13e): In function main': : undefined reference toTFile::TFile(char const*, char const*, char const*, int)’
tryfile.o(.text+0x17e): In function main': : undefined reference toTObject::operator delete(void*)’
tryfile.o(.text+0x1b2): In function main': : undefined reference toTObject::operator delete(void*)’
tryfile.o(.gnu.linkonce.t._ZN7TObjectnwEj+0xd): In function TObject::operator new(unsigned int)': : undefined reference toTStorage::ObjectAlloc(unsigned int)’
collect2: ld returned 1 exit status
make: *** [tryfile] Error 1

Thank you very much!

Best,
Jing
tryfile.cpp (1.04 KB)

Hi,

[quote=“jing”]
${CC} -o tryfile tryfile.o -L${ROOTLIB}

/usr/bin/g++ -o tryfile tryfile.o -L/usr/local/root/lib
tryfile.o(.text+0x2d): In function tree_init()': : undefined reference toTTree::TTree(char const*, char const*, int)’[/quote] g++ doesn’t find TTree’s symbols, because you’re not linking against the root libs. Some comments:

  • you seem to have not too much experience with Makefiles. You can either change that, or use Aclic (".L MyMacro.C+"). The latter is the recommended way :slight_smile:
  • there is an example Makefile in $ROOTSYS/test
  • instead of passing all the needed config options by hand, you can simply call

${CC} -o tryfile tryfile.o `root-config --ldflags` `root-config --glibs`which will pick up all the flags and libs needed to link against root’s (most common) libs.

Axel.