Error when compiling a root cpp script

Hello all,

I opened visual studio’s command prompt, and from there my .root file.
There I try to compile my script:

root [2] .L something.cpp+

and I get this kind of error:

Info in <TWinNTSystem::ACLiC>: creating shared library D:\root_v5.34.36\macros\something_cpp.dll
2124431_cint.cxx
something_cpp_ACLiC_dict.cxx
D:\root_v5.34.36\macros\something.cpp(3) : error C2065: 'cout' : undeclared identifier
Error in <ACLiC>: Compilation failed!

I’ve been looking around for the answer and the best I found is to maybe run vcvarsall.bat before entering my .root file, but when I try to run vcvarsall.bat nothing happens (I think it should at least print some kind of massage, right?)

Thanks.

Hi,

  1. Make sure you have #inclued <Riostream.h>
  2. Either use std::cout, or add using namespace std;

Cheers, Bertrand.

Thank you bellenot, it worked.

However this was only a test script for the compiler.
In my real script this is the error I get:

Info in <TWinNTSystem::ACLiC>: creating shared library D:\root_v5.34.36\macros\var_cpp.dll
22832_cint.cxx
var_cpp_ACLiC_dict.cxx
D:\root_v5.34.36\macros\var.cpp(5) : error C2440: 'default argument' : cannot convert from 'const char [12]' to 'TString'
        Source or target has incomplete type
D:\root_v5.34.36\macros\var.cpp(10) : error C2065: 'TreeS' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(10) : error C2065: 'TreeB' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(10) : error C3861: 'Events2Data': identifier not found
D:\root_v5.34.36\macros\var.cpp(11) : error C2065: 'TFile' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(11) : error C2065: 'f' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(11) : error C2653: 'TFile' : is not a class or namespace name
D:\root_v5.34.36\macros\var.cpp(11) : error C3861: 'Open': identifier not found
D:\root_v5.34.36\macros\var.cpp(16) : error C2065: 'gROOT' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(16) : error C2227: left of '->ProcessLine' must point to class/struct/union/generic type
        type is 'unknown-type'
D:\root_v5.34.36\macros\var.cpp(21) : error C2065: 'TTree' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(21) : error C2065: 'TreeS' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(21) : error C2065: 'TreeB' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(22) : error C2448: 'Ntuple2Events' : function-style initializer appears to be a function definit
D:\root_v5.34.36\macros\var.cpp(37) : error C2065: 'TTree' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(37) : error C2065: 'TreeS' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(37) : error C2065: 'TreeB' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(38) : error C2448: 'Events2Data' : function-style initializer appears to be a function definitio
D:\root_v5.34.36\macros\var.cpp(54) : error C2065: 'TTree' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(54) : error C2065: 'tree' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(54) : error C2065: 'dataTree' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(54) : error C2062: type 'int' unexpected
D:\root_v5.34.36\macros\var.cpp(55) : error C2143: syntax error : missing ';' before '{'
D:\root_v5.34.36\macros\var.cpp(55) : error C2447: '{' : missing function header (old-style formal list?)
D:\root_v5.34.36\macros\var.cpp(431) : error C2100: illegal indirection
D:\root_v5.34.36\macros\var.cpp(435) : error C2100: illegal indirection
D:\root_v5.34.36\macros\var.cpp(440) : error C2100: illegal indirection
D:\root_v5.34.36\macros\var.cpp(441) : error C2100: illegal indirection
D:\root_v5.34.36\macros\var.cpp(448) : error C2100: illegal indirection
D:\root_v5.34.36\macros\var.cpp(452) : error C2065: 'TTree' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(452) : error C2065: 'tree' : undeclared identifier
D:\root_v5.34.36\macros\var.cpp(452) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error in <ACLiC>: Compilation failed!

Could you maybe guide me with what #include/namespace else I should add?

I would say those ones:

#include "TSystem.h"
#include "TROOT.h"
#include "TFile.h"
#include "TTree.h"

And maybe more, you’ll see

And BTW, if you look at the ROOT Reference Documentation, for example for the TFile class, you’ll see which header file to include:
Definition at line 46 of file TFile.h.

Cheers, Bertrand.

Hi bellenot,

After correcting some errors on my script (which I honestly don’t know how root just let slide), the compiling worked just fine.

After the compiler finished I was left with 6 additional files, with the extensions:
.d
.def
.dll
.exp
.lib
.rootmap

How am I to now run the compiled script?

After use the .L command the methods inside the script can simply be executed.

root [0] .L something.C+
root [1] something()

Alternatively one can load and execute the script simultaneously:

root [0] .x something.C+

This requires that there is a method with the same name as the file.

https://root.cern.ch/working-macros

It works!

Thank you everyone!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.