#include in scripts only works once?

Hi, I am having a really strange problem, hopefully there is an easy fix. I am using header files to define some constants. Using gROOT->Reset() clears the defines, but then ROOT still thinks the file is loaded. So the second time I run the script, the defines are not set.

Here is an example of what I mean. I have two files:

header.hxx :

#define _EXAMPLEDEFINE 12345

main.cxx :

{ gROOT->Reset(); #include "header.hxx" cout << _EXAMPLEDEFINE << "\n"; }

The first time I run this script it works fine:

root [0] .x main.cxx 12345

I run it a second time and it does this:

root [1] .x main.cxx
Note: File "header.hxx" already loaded
Error: Symbol _EXAMPLEDEFINE is not defined in current scope  main.cxx:4:
Error: << Illegal operator for pointer 3 main.cxx:4:
!!!Dictionary position not recovered because G__unloadfile() is used in a macro!!!
*** Interpreter error recovered ***

I can only run it again by exiting and reopening ROOT.
What can I do to tell ROOT it needs to reload the include file?

Hi,

ideally you wouldn’t use unnamed macros to begin with. Please use e.g. for a file called mycode.cxx a function

#include "header.hxx"
void mycode() {
   cout << _EXAMPLEDEFINE << "\n";
}

Note how the gROOT->Reset() is gone and how the #include moves outside the function - it’s proper C++. Does that solve the issue? It should, we use CINT this way every day :slight_smile:

Cheers, Axel.

The problem is aall variables and objects go out of scope and can’t be used after script is done executing. They do for unnamed scripts. Is there a way to keep them for named scripts?

It still seems like there should be a way to do this for unnamed scripts anyway. I would think gROOT->Reset() should also clear all header files out of memory and allow them to be loaded again.

[quote]The problem is aall variables and objects go out of scope and can’t be used after script is done executing. They do for unnamed scripts. Is there a way to keep them for named scripts?[/quote]You can declare some of the variables in the global scope ; this way this will no go out of scope at the end of the routine.

Cheers,
Philippe.