How to run c/c++ files on root

I have tried writing codes line by line but it’s too much work and I want to know how to write in a text editior and run it on root then as it is much easier, as in the header files required and such. For now I always run hsimple.C first and the write the code for graphs line by line which is quite tedious.

ROOT Version: V6.18.04
Platform: Windows

Hi @esh187,
you can run C++ code with ROOT with root -q functionname.cpp (ROOT will execute the function called functionname in file functionname.cpp). You can also open a ROOT prompt with root, and then load the contents of a program with .L myprogram.cpp, at which point you can just call any function that was defined in that file as normal.

For more complex usecases, my personal recommendation would actually be to use ROOT as any other C++ library and compile your programs with g++ -o program program.cpp $(root-config --libs --cflags) (you can check what root-config --libs --cflags does by running it in your prompt: it’s just a handy utility to provide all the compilation flags needed to compile a program that depends on ROOT).


1 Like

Search for “macro” in: ROOT Primer

I tried doing this but it doesnt work, only shows errors for some reason.
error: unknown type name ‘cout’
error: expected unqualified-id

This was my program:

#include <iostream.h>
#include <conio.h>
void main()
cout<< “Hello World” <<endl;

It;s simple enough and I used a C++ compiler to ensur there were no mistakes so I dont know.

Uhm, that shouldn’t be valid C++ you need either using namespace std; or std::cout and std::endl;

How exactly should the code be written then?

#include < iostream >
using namespace std;

int main()
int number;

cout << "Enter an integer: ";
cin >> number;

cout << "You entered " << number;    
return 0;


is what i wrote and saved it as hello.cpp it still wont work
error: cannot find function ‘hello()’; falling back to .L
is what i see

See e.g. here for the original version and its compilation errors, and here for the fixed version.

Regarding the second post, as I wrote above if you run cpp file through root, the filename should be the same as the function you want root to execute (see that post for more details, or the primer that Wile E. linked.

Hope this helps!

Thanks! I am now able to execute files that way but I cannot seem to work the Slits example given in
The input is taken but the graph is blank white box and nothing else.
I am confused as to why

I really can’t say without a reproducer, but my best guess would be this or this.