Problem with ClassDef() compiling standalone code

Dear all,

sorry for (probably) stupid question but I don’t know how to solve the problem.

I am using ROOT 6.18.04 on osx High Sierra; the problem is: when I compile my simple test code with: g++ prova3.cpp root-config --cflags -- libs -o prova3

I get the following error:

**prova3.cpp:23:5:**  **error:**  **static data member 'fgIsA' not allowed in local class**

 **'Event'**

 **ClassDef(Event,1)**

 **^**

**/sw/share/root/include/Rtypes.h:327:4:**  **note:**  **expanded from macro 'ClassDef'**

 **_ClassDefOutline_(name,id,virtual,)   \**

 **^**

**/sw/share/root/include/Rtypes.h:303:29:**  **note:**  **expanded from macro**

 **'_ClassDefOutline_'**

 **static atomic_TClass_ptr fgIsA; \**

 **^**

**1 error generated.**

My code is:

#include "TROOT.h"

#include "TFile.h"

#include "TTree.h"

#include <fstream>

#include <iostream>

#include "TObject.h"

using namespace std;

int main() {

  class example_1 {  

  public:

    //Int_t pippo;

    Int_t croc1[32], croc2, croc3, croc4;

  };

  class Event : public TObject {

  public:

    example_1 ex;   

    ClassDef(Event,1)

  };

  TFile fout("test_11.root", "recreate");

  TTree *tree = new TTree("tree","tree");

  Event *evento = new Event;

  tree->Branch("event",&evento);

  Int_t n_eventi = 10000;

  for (Int_t i = 0; i < n_eventi; i++) {

    if(i % 1000 == 0) cout << "Processing event " << i << "... " << endl;

    evento->ex.croc1[0] = 22;

    evento->ex.croc1[1] = -23;

    tree->Fill();

  }

  tree->Write();

}

Can anyone help me ?
Thanks in advance
F.

Ciao Francesco!
That compiler error says that your class Event can’t have a static data member fgIsA if it’s defined inside main (it’s the ClassDef macro that puts fgIsA into your class). It should be enough to take the class definitions out of the main function to fix it.

Cheers,
Enrico

Ciao Enrico,

thank for your support; I did two files: prova3.h and prova3.cpp. In prova3.h there is:

#include "TObject.h"
class Det {  
 public:
  Int_t croc1[32], croc2, croc3, croc4;
};

class Event : public TObject {
 public:
  Det ex;   
  ClassDef(Event,1)
};

While in prova.cpp there is:

#include "TROOT.h"
#include "TFile.h"
#include "TTree.h"
#include <fstream>
#include <iostream>
#include <TInterpreter.h>
#include "TObject.h"
#include "prova3.h"
using namespace std;

int main() { 
  TFile fout("test_11.root", "recreate");
  TTree *tree = new TTree("tree","tree");
  Event *evento = new Event;
  
  tree->Branch("event",&evento);
  
  Int_t n_eventi = 10000;
  for (Int_t i = 0; i < n_eventi; i++) {
    if(i % 1000 == 0) cout << "Processing event " << i << "... " << endl;
    evento->ex.croc1[0] = 22;
    evento->ex.croc1[1] = -23;
    tree->Fill();
  }
  tree->Write();
}

Now compiling always with: g++ prova3.cpp $(root-config --cflags --libs) -o prova3
I get the following error:

Undefined symbols for architecture x86_64:
"Event::Class()", referenced from:
TClass* ROOT::Internal::GetClassHelper<Event>(bool, bool, std::__1::integral_constant<bool, true>) in prova3-45dd9c.o
"vtable for Event", referenced from:
Event::Event() in prova3-45dd9c.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Cheers
F.

Hi,
ok, a new error message is progress :smiley:

Since you are doing I/O with these classes, are you generating dictionaries for them?

Cheers,
Enrico

Ciao Enrico,

Yes, I generated the dictionaries with the following instruction:

rootcling -v -f test.cxx test.cpp [test.cc](http://test.cc) LinkDef.h

Where as usual in .cpp there’s the code and in .cc there’s the function’s definition.

After this I compile the code with the following instruction:

g++ test.cxx `root-config --cflags —libs` -o test

Following these instructions the compilation works but when I run the program the variable in the tree is empty.

I can’t figure out why, (if you can explain it I would be grateful) but if also the Class Def is declared as:

Class Def : public TObject {

…

}

everything seems working correctly and the variables in the tree are correctly filled.

Cheers

Francesco.

P.S.: It’s real: "a new error message is progress”; we say: “sbagliando sbagliando s’impara” :-))

Assuming you meant class Det and not Class Def…I’m not sure why data gets written in one case and doesn’t in the other. Maybe @pcanal can shed some light on this. You don’t get any runtime warning?

we say: “sbagliando sbagliando s’impara”

lo ben so! :slight_smile:

Yes, of course, Class Det …
No, no warning during rootcling and g++; only when run ROOT and open output file get two warning:

root [0] TFile f

(TFile &) Name: Title:

root [1] f.Open(“out_file.root”)

Warning in TClass::Init: no dictionary for class Event is available

Warning in TClass::Init: no dictionary for class Det is available

(TFile *) 0x7fbc9121a3a0

root [2]

Bye

Francesco

To ‘remove’ those warning you need:

g++ test.cxx `root-config --cflags —libs` -o libtest.so --shared -fPIC
root
root [0] .L libtest.so
root [1] f = TFile::Open(“out_file.root”)

without the shared library being created and loaded … the dictionary is not available to the root executable.

Cheers,
Philippe.

1 Like

Ciao Philippe,

thanks for your reply, but now I’m confused….

The steps for compiling and running my programs are (supposing I have a test.cpp, test.h, test.cc e LinlDef.h files):

  1. generate the dictionary using rootcling:

rootcling -v -f test.cxx test.cpp test.cc LinkDef.h

  1. compile the program:

g++ test.cxx root-config --cflags —libs -o test (test is the name of the executable generated by g++ with -o option)

  1. run the program that works on data:

./test

3+) After this I have a my root file: “out_file.root” with variables correctly filled.

  1. Use your command line ……. to generate libtest.so that after load in root to erase warnings

g++ test.cxx root-config --cflags —libs -o libtest.so --shared -fPIC

Cheers

Francesco.

What is confusing? (i.e. I am confused :slight_smile: on where you are stuck )

Ok.

looking again everything it’s clear. :-))

Thanks again.

Cheers

Francesco.

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