Classdef

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 tovtable for test’
/tmp/ccwnkxgJ.o: In function __static_initialization_and_destruction_0(int, int)': test.cxx:(.text+0xcf): undefined reference toROOT::GenerateInitInstance(test const
)’
/tmp/cc0Gyfar.o: In function test::~test()': main.cxx:(.text._ZN4testD2Ev[_ZN4testD5Ev]+0xf): undefined reference tovtable 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;

}

Hi,

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

Cheers,
Philippe.

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=“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)

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})

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.

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

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