Load and run macro in ROOT6

Hi

I have root macro run_test.C:

void run_test() {

  TString macro_file="test.C+g";
  Info("run_test","Loading main macro='%s'... ",macro_file.Data());

  if (gROOT->LoadMacro(macro_file))
    Fatal("run_test","Can't load macro='%s'",macro_file.Data());

  test();

} // run_test      

which load&runs other macro test.C:

#include <TError.h>

int test() {
  Info("test","Hello, world!");
  return 0;
} // test 

It runs without problem in ROOT5:

[shitov@nu76-78 tmp]$ root -l
root [0] .x run_test.C
Info in <run_test>: Loading main macro='test.C+g'... 
Info in <TUnixSystem::ACLiC>: creating shared library /usr/local_soft/danss/itep_analysis_2017/tmp/./test_C.so
Info in <test>: Hello, world!
root [1] 

But in ROOT6 I have:

root [0] .x run_test.C
In file included from input_line_8:1:
/usr/local_soft/danss/itep_analysis_2017/tmp/run_test.C:9:3: error: use of undeclared identifier 'test'
  test();
  ^
root [1] 

Addition of function declaration in run_test.C:

int test();

doesn’t help a lot:

root [1] .x run_test.C
IncrementalExecutor::executeFunction: symbol '_Z4testv' unresolved while linking [cling interface function]!
You are probably missing the definition of test()
Maybe you need to load the corresponding shared library?

How can I run my code in ROOT6?

Thanks,
Yuri

You can use TROOT::Macro()

void run_test() {

  TString macro_file="test.C+g";
  Info("run_test","Loading main macro='%s'... ",macro_file.Data());

  gROOT->Macro(macro_file.Data());
}

Thanks, Olivier!

It also runs with parameters (needed in real example):

void run_test() {

  TString args,com,macro_file="test.C+g";
  args="(1,\"test\")";
  com=macro_file + args;

  Int_t rerr; 	// load&run error                                                                                                                                      
  Long_t merr;  // macro error                                                                                                                                         
  if ( (merr=gROOT->Macro(com,&rerr)) !=0 )
    Error("run_test","Failed com='%s' (macro err=%ld)",com.Data(),merr);
  if (rerr)
    Error("run_test","Load&run failed com='%s' (load&run rerr=%d)",com.Data(),rerr);

} // run_test     

#include <TError.h>
#include <TString.h>

int test(int i, TString s) {
  Info("test","Hello, world!");
  Info("test","  Input pars i=%d, s=%s!",i,s.Data());
  return 0;
} // test

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