Issue compiling root 5.34/18 with g++ (Ubuntu/Linaro 4.6

I’m trying to compile a script and g++ is giving me a lot of “Undefined reference to `TTree::TTree(char const*, char const*, int)’” or similar when I try to make run (run being the name of the script).

results of make run.o
[myname]@[mypc]$ make run.o
g++ -c -g -Wall /home/[myname]/root_tmp/root/bin/root-config --cflags -c -o run.o run.c

and works fine, run.o is created
make run however fails, the full log is at the bottom of this post.

I have all the necessary includes for my script, so that cannot be the issue.

First, my makefile, because that seems more likely to be where things are wrong

CC=g++
CFLAGS=-c -g -Wall `/home/[myname]/root_tmp/root/bin/root-config --cflags`
LDFLAGS=-L`root-config --libdir` `/home/[myname]/root_tmp/root/bin/root-config --glibs`
SOURCES=run.cc
OBJECTS=$(SOURCES:.cc=.o)
EXECUTABLE=main

all: $(SOURCES) $(EXECUTABLE)

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

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

clean:
	rm ./*~ ./*.o

Now the entire output of trying to create an executable from the object:

[myname]@[mypc]:~/sas$ make run g++ `/home/[myname]/root_tmp/root/bin/root-config --glibs` run.o -o run run.o: In function `main': /home/[myname]/sas/run.c:21: undefined reference to `TTree::TTree(char const*, char const*, int)' /home/[myname]/sas/run.c:22: undefined reference to `TTree::ReadFile(char const*, char const*, char)' /home/[myname]/sas/run.c:26: undefined reference to `TCanvas::TCanvas(char const*, char const*, int)' /home/[myname]/sas/run.c:59: undefined reference to `TTree::GetEntry(long long, int)' /home/[myname]/sas/run.c:128: undefined reference to `TGraph::TGraph(int, double const*, double const*)' /home/[myname]/sas/run.c:132: undefined reference to `TF1::TF1(char const*, char const*, double, double)' /home/[myname]/sas/run.c:135: undefined reference to `TFormula::SetParameters(double, double, double, double, double, double, double, double, double, double, double)' /home/[myname]/sas/run.c:137: undefined reference to `TFormula::SetParNames(char const*, char const*, char const*, char const*, char const*, char const*, char const*, char const*, char const*, char const*, char const*)' /home/[myname]/sas/run.c:144: undefined reference to `TGraph::Fit(TF1*, char const*, char const*, double, double)' /home/[myname]/sas/run.c:144: undefined reference to `TFitResultPtr::~TFitResultPtr()' /home/[myname]/sas/run.c:151: undefined reference to `TGraph::Draw(char const*)' /home/[myname]/sas/run.c:152: undefined reference to `TGraph::SetTitle(char const*)' /home/[myname]/sas/run.c:153: undefined reference to `TGraph::GetXaxis() const' /home/[myname]/sas/run.c:154: undefined reference to `TGraph::GetYaxis() const' /home/[myname]/sas/run.c:158: undefined reference to `TPad::Print(char const*) const' /home/[myname]/sas/run.c:164: undefined reference to `TFormula::GetParameter(int) const' /home/[myname]/sas/run.c:165: undefined reference to `TFormula::GetParameter(int) const' /home/[myname]/sas/run.c:272: undefined reference to `TF1::TF1(char const*, char const*, double, double)' /home/[myname]/sas/run.c:278: undefined reference to `TF1::GetRange(double&, double&) const' /home/[myname]/sas/run.c:285: undefined reference to `TF1::Eval(double, double, double, double) const' /home/[myname]/sas/run.c:295: undefined reference to `TF1::TF1(char const*, char const*, double, double)' /home/[myname]/sas/run.c:300: undefined reference to `TF1::GetRange(double&, double&) const' /home/[myname]/sas/run.c:307: undefined reference to `TF1::Eval(double, double, double, double) const' /home/[myname]/sas/run.c:327: undefined reference to `TCanvas::Clear(char const*)' /home/[myname]/sas/run.c:328: undefined reference to `TPad::Print(char const*) const' /home/[myname]/sas/run.c:331: undefined reference to `TPad::Divide(int, int, float, float, int)' /home/[myname]/sas/run.c:334: undefined reference to `TCanvas::cd(int)' /home/[myname]/sas/run.c:335: undefined reference to `TGraph::TGraph(int, double const*, double const*)' /home/[myname]/sas/run.c:336: undefined reference to `TGraph::Draw(char const*)' /home/[myname]/sas/run.c:336: undefined reference to `TGraph::SetTitle(char const*)' /home/[myname]/sas/run.c:337: undefined reference to `TGraph::GetXaxis() const' /home/[myname]/sas/run.c:338: undefined reference to `TGraph::GetYaxis() const' /home/[myname]/sas/run.c:342: undefined reference to `TCanvas::cd(int)' /home/[myname]/sas/run.c:343: undefined reference to `TGraph::TGraph(int, double const*, double const*)' /home/[myname]/sas/run.c:344: undefined reference to `TGraph::Draw(char const*)' /home/[myname]/sas/run.c:344: undefined reference to `TGraph::SetTitle(char const*)' /home/[myname]/sas/run.c:345: undefined reference to `TGraph::GetXaxis() const' /home/[myname]/sas/run.c:346: undefined reference to `TGraph::GetYaxis() const' /home/[myname]/sas/run.c:349: undefined reference to `TCanvas::cd(int)' /home/[myname]/sas/run.c:350: undefined reference to `TGraph::TGraph(int, double const*, double const*)' /home/[myname]/sas/run.c:351: undefined reference to `TGraph::Draw(char const*)' /home/[myname]/sas/run.c:351: undefined reference to `TGraph::SetTitle(char const*)' /home/[myname]/sas/run.c:352: undefined reference to `TGraph::GetXaxis() const' /home/[myname]/sas/run.c:353: undefined reference to `TGraph::GetYaxis() const' /home/[myname]/sas/run.c:356: undefined reference to `TCanvas::cd(int)' /home/[myname]/sas/run.c:357: undefined reference to `TGraph::TGraph(int, double const*, double const*)' /home/[myname]/sas/run.c:358: undefined reference to `TGraph::Draw(char const*)' /home/[myname]/sas/run.c:358: undefined reference to `TGraph::SetTitle(char const*)' /home/[myname]/sas/run.c:359: undefined reference to `TGraph::GetXaxis() const' /home/[myname]/sas/run.c:360: undefined reference to `TGraph::GetYaxis() const' /home/[myname]/sas/run.c:362: undefined reference to `TPad::Print(char const*) const' /home/[myname]/sas/run.c:364: undefined reference to `TCanvas::Clear(char const*)' /home/[myname]/sas/run.c:365: undefined reference to `TPad::Divide(int, int, float, float, int)' /home/[myname]/sas/run.c:368: undefined reference to `TCanvas::cd(int)' /home/[myname]/sas/run.c:371: undefined reference to `TGraph::TGraph(int, double const*, double const*)' /home/[myname]/sas/run.c:372: undefined reference to `TGraph::Draw(char const*)' /home/[myname]/sas/run.c:372: undefined reference to `TGraph::SetTitle(char const*)' /home/[myname]/sas/run.c:373: undefined reference to `TGraph::GetXaxis() const' /home/[myname]/sas/run.c:374: undefined reference to `TGraph::GetYaxis() const' /home/[myname]/sas/run.c:375: undefined reference to `TGraph::GetYaxis() const' /home/[myname]/sas/run.c:378: undefined reference to `TCanvas::cd(int)' /home/[myname]/sas/run.c:380: undefined reference to `TGraph::TGraph(int, double const*, double const*)' /home/[myname]/sas/run.c:381: undefined reference to `TGraph::Draw(char const*)' /home/[myname]/sas/run.c:381: undefined reference to `TGraph::SetTitle(char const*)' /home/[myname]/sas/run.c:382: undefined reference to `TGraph::GetXaxis() const' /home/[myname]/sas/run.c:383: undefined reference to `TGraph::GetYaxis() const' /home/[myname]/sas/run.c:384: undefined reference to `TGraph::GetYaxis() const' /home/[myname]/sas/run.c:387: undefined reference to `TPad::Print(char const*) const' /home/[myname]/sas/run.c:390: undefined reference to `TPad::Print(char const*) const' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' run.o:/home/[myname]/sas/run.c:390: more undefined references to `TGraph::~TGraph()' follow run.o: In function `main': /home/[myname]/sas/run.c:390: undefined reference to `TF1::~TF1()' /home/[myname]/sas/run.c:390: undefined reference to `TF1::~TF1()' /home/[myname]/sas/run.c:390: undefined reference to `TF1::~TF1()' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' /home/[myname]/sas/run.c:390: undefined reference to `TCanvas::~TCanvas()' /home/[myname]/sas/run.c:390: undefined reference to `TTree::~TTree()' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' run.o:/home/[myname]/sas/run.c:390: more undefined references to `TGraph::~TGraph()' follow run.o: In function `main': /home/[myname]/sas/run.c:390: undefined reference to `TF1::~TF1()' /home/[myname]/sas/run.c:390: undefined reference to `TF1::~TF1()' /home/[myname]/sas/run.c:390: undefined reference to `TF1::~TF1()' /home/[myname]/sas/run.c:390: undefined reference to `TGraph::~TGraph()' /home/[myname]/sas/run.c:390: undefined reference to `TCanvas::~TCanvas()' /home/[myname]/sas/run.c:390: undefined reference to `TTree::~TTree()' run.o: In function `__static_initialization_and_destruction_0': /home/[myname]/root_tmp/root/include/TVersionCheck.h:34: undefined reference to `TVersionCheck::TVersionCheck(int)' run.o: In function `TCanvasImp::IsA() const': /home/[myname]/root_tmp/root/include/TCanvasImp.h:80: undefined reference to `TCanvasImp::Class()' run.o: In function `int TTree::SetBranchAddress<float>(char const*, float*, TBranch**)': /home/[myname]/root_tmp/root/include/TTree.h:504: undefined reference to `TClass::GetClass(std::type_info const&, bool, bool)' /home/[myname]/root_tmp/root/include/TTree.h:506: undefined reference to `TDataType::GetType(std::type_info const&)' run.o:(.rodata._ZTV10TCanvasImp[vtable for TCanvasImp]+0xf8): undefined reference to `TCanvasImp::ShowMembers(TMemberInspector&)' run.o:(.rodata._ZTV10TCanvasImp[vtable for TCanvasImp]+0x100): undefined reference to `TCanvasImp::Streamer(TBuffer&)' run.o: In function `~eline_t': /home/[myname]/sas/lookupgen.h:3: undefined reference to `TString::~TString()' collect2: ld returned 1 exit status make: *** [run] Error 1

if you type:

root-config --glibs

what do you get ?

-L/home/[myname]/root_tmp/root/lib -lGui -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic

So I doubled the -L. However removing my first -L didn’t fix the problem

Try with:

LDFLAGS=/home/[myname]/root_tmp/root/bin/root-config --ldflags --glibs

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

make run leads to the same result

go in the place where your ROOT is installed and “source” one of the “thisroot” script in the bin directory

already do that, manually doing it again makes no difference

Could you, please, try to manually execute: /home/[myname]/root_tmp/root/bin/root-config --ldflags --glibs g++ -o run run.o `/home/[myname]/root_tmp/root/bin/root-config --ldflags --glibs` and post the output of these two comands here.

that worked, not sure why, but it did

In my first post above, I told you to change the line:
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
into:
$(CC) -o $@ $(OBJECTS) $(LDFLAGS)

Which I did.

So try to “make clean” and then recompile it from scratch again.
Inspect the linking command line -> the order of all elements should be exactly the same as in the “manual linking” command line.

I’ve found something rather strange … in your makefile, I can see:
EXECUTABLE=main
but then you link the “run” executable (and there’s no rule to link it in your makefile so make takes some “default” rule which is not appropriate, I believe).
Try to change this line into:
EXECUTABLE=run

I’ve tried comparing the lines, and making the exectuable comes up (wrongly) with

after changing executable to run

g++ -o run `/home/[myname]/root_tmp/root/bin/root-config --ldflags --glibs` /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: ld returned 1 exit status make: *** [run] Error 1

I can see another problem … in you first post, I find:
g++ -c … run.c
but your makefile expects a “run.cc” file (well, try simply to rename “run.c” into “run.cc”).