# Classdef

**URL:** <https://root-forum.cern.ch/t/classdef/22337>\
**Category:** ROOT\
**Created:** [December 8, 2016, 8:23pm UTC](https://root-forum.cern.ch/t/classdef/22337 "2016-12-08T20:23:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbjc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/858c86/32.png) [@tbjc](https://root-forum.cern.ch/u/tbjc)\
**Post date:** [December 8, 2016, 8:23pm UTC](https://root-forum.cern.ch/t/classdef/22337/1 "2016-12-08T20:23:57Z")

</div>

Hi I am trying to build my own class,. However, when I do

g++ -o hello -Iinclude/ src/\*.cxx _.cxx -lXMLParser -lHist -lRIO -lTree `root-config --cflags --glibs`  
/tmp/ccwnkxgJ.o: In function `test::test()': test.cxx:(.text+0x1c): undefined reference to`vtable for test’  
/tmp/ccwnkxgJ.o: In function `__static_initialization_and_destruction_0(int, int)': test.cxx:(.text+0xcf): undefined reference to`ROOT::GenerateInitInstance(test const_)’  
/tmp/cc0Gyfar.o: In function `test::~test()': main.cxx:(.text._ZN4testD2Ev[_ZN4testD5Ev]+0xf): undefined reference to`vtable for test’  
collect2: error: ld returned 1 exit status  
Makefile:2: recipe for target ‘all’ failed  
make: \*\*\* [all] Error 1

below are the files

/// main.cxx  
#include “TString.h”  
#include   
#include “test.h”

using namespace std;

int main()  
{  
test t;  
return 1;  
}

/// include/test.h

#ifndef TEST  
#define TEST  
#include “TObject.h”

class test: public TObject {  
public:  
test();  
private:  
ClassDef(test, 1)  
};

#endif

/// src/test.cxx  
#include “test.h”  
#include   
using namespace std;

ClassImp(test)

test::test()  
{  
cout\<\<“test:bobo”\<\<endl;

}

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [December 8, 2016, 8:44pm UTC](https://root-forum.cern.ch/t/classdef/22337/2 "2016-12-08T20:44:38Z")

</div>

Hi,

You need to generate, compile and link against a “dictionary” for you class.  
See [root.cern.ch/interacting-shared … s-rootcint](https://root.cern.ch/interacting-shared-libraries-rootcint)  
and/or [root.cern.ch/root/Using.html](https://root.cern.ch/root/Using.html)

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [December 9, 2016, 10:54am UTC](https://root-forum.cern.ch/t/classdef/22337/3 "2016-12-09T10:54:50Z")

</div>

Hi,

And, unless you know why (e.g. you need to use a TClonesArray), you shouldn’t inherit from TObject these days anymore.

Cheers, Axel.

---

<div class="post-metadata">

**Author:** ![tbjc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/858c86/32.png) [@tbjc](https://root-forum.cern.ch/u/tbjc)\
**Post date:** [December 9, 2016, 6:46pm UTC](https://root-forum.cern.ch/t/classdef/22337/4 "2016-12-09T18:46:48Z")

</div>

[quote=“Axel”]Hi,

And, unless you know why (e.g. you need to use a TClonesArray), you shouldn’t inherit from TObject these days anymore.

Cheers, Axel.[/quote]

Hi Axel,

Yes that is exactly what I need. Someone wrote a root class using the TClonesArray and I am trying to reuse his code. Unfortunately, the documentation is not clear to me. Basically I am trying to understand how can I link those classes with the ClassDef tags through my simple examples. Can you tell me the step by step procedure how should I do it or simply add changes to my cmake file? Below is my cmake file.

Thanks

cmake\_minimum\_required(VERSION 2. ![8)](https://root-forum.cern.ch/uploads/default/original/1X/fe5f42ae79e389360ae9d969db4dcc4bca03d384.gif "Cool")

project(tbjcAnalysis)

set(CMAKE\_MODULE\_PATH $ENV{ROOTSYS}/etc/cmake)

find\_package(ROOT)

include\_directories({ROOT\_INCLUDE\_DIR})# {CMAKE\_CURRENT\_SOURCE\_DIR}/inc)  
add\_definitions(${GCC\_COVERAGE\_COMPILE\_FLAGS})

include\_directories(${PROJECT\_SOURCE\_DIR}/include)

file(GLOB sources {PROJECT\_SOURCE\_DIR}/src/\*.cxx) add\_executable(example main.cxx {sources})  
target\_link\_libraries(example ${ROOT\_LIBRARIES})

---

<div class="post-metadata">

**Author:** ![mato](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mato/32/12317_2.png) [@mato](https://root-forum.cern.ch/u/mato)\
**Post date:** [December 12, 2016, 8:28am UTC](https://root-forum.cern.ch/t/classdef/22337/5 "2016-12-12T08:28:41Z")

</div>

As Philippe pointed out, you need to link against the generated dictionary of our class. Just changing slightly the CMakeLists.txt file like this should work.

```auto
cmake_minimum_required(VERSION 2.8)

project(tbjcAnalysis)

list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
find_package(ROOT)
include(${ROOT_USE_FILE})

include_directories(${ROOT_INCLUDE_DIRS})
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})

include_directories(${PROJECT_SOURCE_DIR}/include)
ROOT_GENERATE_DICTIONARY(G__test include/test.h)

file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cxx)
add_executable(example main.cxx G__test.cxx ${sources})
target_link_libraries(example ${ROOT_LIBRARIES})

```

Notice the following changes:

- Use the CMAKE\_PREFIX\_PATH to tell where to find the ROOTConfig.cmake file instead of using FindROOT.cmake (if ROOT was build with CMake).
- Use the file ${ROOT\_USE\_FILE} to define useful macros (i.e. ROOT\_GENERATE\_DICTIONARY)
- Generation of the dictionary G\_\_test.cxx and its use to build the executable example.

Cheers,  
Pere

---

<div class="post-metadata">

**Author:** ![zhixing1996](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zhixing1996/32/24581_2.png) [@zhixing1996](https://root-forum.cern.ch/u/zhixing1996)\
**Post date:** [March 9, 2021, 4:32am UTC](https://root-forum.cern.ch/t/classdef/22337/6 "2021-03-09T04:32:03Z")

</div>

Hi, Pere,

A minor comment: ROOT\_GENERATE\_DICTIONARY(G\_\_test include/test.h) → ROOT\_GENERATE\_DICTIONARY(G\_\_test test.h), and in main.cxx: add #include “iostream”.

Your comment is very useful!

Cheers,

Maoqiang

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 10, 2021, 7:51pm UTC](https://root-forum.cern.ch/t/classdef/22337/7 "2021-03-10T19:51:45Z")

</div>

Closed on March 10, 2021, 7:51pm UTC


