Compiling Root script with g++

Hello,

I am trying to compile a Root script externally with g++ so as to make a stand alone executable. As an initial step I am trying to just compile (main.cc):

#include "TTree.h"
int main()
{
return 0;
}

However I get the following error that I can’t figure out:
g++ -c -g -Wall root-config --cflags main.cc -o main.o
g++ root-config --glibs main.o -o main
main.o: In function __static_initialization_and_destruction_0': /usr/local/include/root/TVersionCheck.h:34: undefined reference toTVersionCheck::TVersionCheck(int)'
collect2: ld returned 1 exit status
make: *** [main] Error 1

I am using the following makefile:

CC=g++
CFLAGS=-c -g -Wall root-config --cflags
LDFLAGS=root-config --glibs
SOURCES=main.cc
OBJECTS=$(SOURCES:.cc=.o)
EXECUTABLE=main
main : main.cc

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cc.o:
$(CC) $(CFLAGS) $< -o $@

clean:
rm ./~ ./.o

Thank you in advance,

This should work.

Do you by chance have another ROOT installation early in you LD_LIBRARY_PATH, i.e. before the one installed to /usr/local?

LDFLAGS=root-config --ldflags
LDLIBS=root-config --glibs
$(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(LDLIBS)

I don’t think I have any previous installs. This is what is in the LD_LIBRARY_PATH
$ echo $LD_LIBRARY_PATH
/home/sam/geant4.9.5.p01-install/lib:/home/sam/geant4.9.5.p01-install/lib:/usr/local/lib/root

When I try to compile with out make:
g++ root-config --ldflags -o main main.cc root-config --glibs

main.cc:2:19: fatal error: TTree.h: No such file or directory
compilation terminated.

CXX=root-config --cxx
CXXFLAGS=root-config --cflags
LDFLAGS=root-config --ldflags
LDLIBS=root-config --glibs

… either …

$(EXECUTABLE): $(SOURCES)
$(CXX) $(CXXFLAGS) -W -Wall -o $@ $^ $(LDLIBS)

… or …

$(EXECUTABLE): $(OBJECTS)
$(CXX) $(LDFLAGS) -o $@ $^ $(LDLIBS)

.cc.o:
$(CXX) $(CXXFLAGS) -W -Wall -c $<

That fixed it

Thank you