Hello J. and Wile_E_Coyote,
Indeed I am trying to use compiled code to get the results back from a macro, whether that macro is loaded with “LoadMacro” or “ProcessLine”, and fully compiled itself with “.L config.C++” style compilation.
The issue with the example for compiled code is that the compiler will not know anything about __config
and so refuse to compile. So I thought I could send a pointer over to the script, the script fills that pointer, which after execution would be useful to access the information. This seems to not work with compiled code.
Following your example, here is what I tried:
config.h:
#include "TROOT.h"
#include <iostream>
struct config_t {
int ivalue = 0;
double dvalue = 0.;
config_t(){
std::cout << "Created a config_t. \n";
}
void Print() {
std::cout << "ivalue = " << ivalue << "\n";
std::cout << "dvalue = " << dvalue << "\n";
}
};
config.C:
#include "config.h"
{
config_test.ivalue=10;
config_test.dvalue=1.234;
}
config_test.cpp:
#include "config.h"
int main(int argc, char **argv) {
std::cout << "Hello from config_test.\n";
config_t config_test;
config_test.Print();
gROOT->LoadMacro("config.C");
config_test.Print();
}
From the root prompt, this works fine:
root [0] #include "config.h"
root [1] config_t config_test;
Created a config_t.
root [2] config_test.Print()
ivalue = 0
dvalue = 0
root [3] gROOT->LoadMacro("config.C")
(int) 0
root [4] config_test.Print()
ivalue = 10
dvalue = 1.234
But compiling, and running it:
g++ config_test.cpp -o config_test $(root-config --cflags) $(root-config --libs)
config_test
Hello from config_test.
Created a config_t.
ivalue = 0
dvalue = 0
input_line_9:2:3: error: use of undeclared identifier 'config_test'
(config_test.ivalue = 10)
^
libc++abi: terminating with uncaught exception of type cling::CompilationException
Abort trap: 6
What I also tried is finding the object in the macro (or alternatively in the main code) with gROOT->FindObject("config_t")
, but I don’t seem to find the object, I get a null pointer.
Modified config.C:
#include "config.h"
{
auto config_ptr = (config_t *)gROOT->FindObject("config_t");
if(config_ptr){
config_ptr->ivalue = 11;
}else{
std::cout << "Pointer is null.\n";
}
}
Using it:
#include "config.h"
root [1] config_t config_test;
Created a config_t.
root [2] gROOT->LoadMacro("config.C")
Pointer is null.
(int) 0
Is there any way that I can pass pointers or information between the compiled code and the macro?
It just seems I am missing something.
Thanks again for the help.
Best,
Maurik