Error to include root header files in C++


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.22
Platform: ubuntu 20
Compiler: g++

When I run this code in C++:
#include
#include “/home/subhrod/root/include/TF1.h”
using namespace std;
int main(void)
{
int n;
TF1 *f1 = new TF1(“f1”,“sin(x)”, -5, 5);
f1-> SetLineColor(kBlue+1);
f1->SetTitle(“My graph; x”);
f1->Draw();
cout<<"hello world "<<n<<endl;
return 0;
}

It throws an error:

In file included from /home/subhrod/root/include/Rtypes.h:23,
from /home/subhrod/root/include/TObject.h:17,
from /home/subhrod/root/include/TNamed.h:25,
from /home/subhrod/root/include/TFormula.h:16,
from /home/subhrod/root/include/TF1.h:27,
from root1.cc:2:
/home/subhrod/root/include/RtypesCore.h:23:10: fatal error: ROOT/RConfig.hxx: No such file or directory
23 | #include <ROOT/RConfig.hxx>
| ^~~~~~~~~~~~~~~~~~
compilation terminated.

Please help me out with that.

Hi,
how are you compiling the code? Are you adding ROOT-specific compilation flags (those listed when running root-config --cflags --libs in a terminal)?

Cheers,
Enrico

I did sir,
$ root-config --cflags --libs
-pthread -std=c++11 -m64 -I/home/subhrod/root/include -L/home/subhrod/root/lib -lCore -lImt -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lROOTVecOps -lTree -lTreePlayer -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lMultiProc -lROOTDataFrame -pthread -lm -ldl -rdynamic

it returns like this but the problem still there.

Hi,
what’s the exact command you are using to compile the code?

Sir, I used c++ compiler.
~/workspace$ g++ filename.cpp

The file is in ~/workspace

You need g++ filename.cpp $(root-config --cflags --libs), those flags tell the compiler where to find ROOT headers and libraries.

Thank you sir, it worked perfectly. but can I set some parameter so that I don’t need to put the flags everythime??

Typically you’d write a Makefile or (for larger projects) setup cmake so that you just have to type make or equivalent.

when I run this file by:
$ ./a.out

It thows this:
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
Error in TROOT::WriteTObject: The current directory (RootApp) is not associated with a file. The object (f1) has not been written.
hello world

But I could not find the file or it does not pop up any window showing the graph.
If you can tell me how to do those things.
Sorry sir this is pre-eliminary things, but if you tell me that will be really helpful as I am newbie
Thank you, sir.

See this post for why the graph does not show (you need to create a TApplication object and call app.Run() before the end of the main function so that the program doesn’t just exit when it reaches the end of main.

In general you might want to check out the ROOT primer, in particular the section about Interpretation and compilation of ROOT programs.

I hope this helps!
Enrico