Including TH1F library in cmake project

Dear All,

I am trying to include the th1f library in my cmake project, trying to adapt this root.cern.ch/how/integrate-root … ject-cmake, but I get a strange error:

[code]-- Configuring done
CMake Error at CMakeLists.txt:73 (add_library):
Cannot find source file:

TH1F.cxx

Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx

CMake Error: CMake can not determine linker language for target: TH1F
CMake Error: Cannot determine link language for target “TH1F”.
– Generating done
[/code]

the relevant sections of the CMakeList.txt file are:

list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
[...]
find_package(ROOT REQUIRED COMPONENTS RIO Net Hist)

and

[code]
enable_testing()
set(PROJECT_TEST_NAME ${PROJECT_NAME_STR}_test)

include(${ROOT_USE_FILE})

link_libraries(${CMAKE_THREAD_LIBS_INIT} ${TIFF_LIBRARY})

file(GLOB TEST_SRC_FILES ${PROJECT_SOURCE_DIR}/test/.cpp)
file(GLOB ALL_SRC_FILES ${CPP_SOURCE_FILES}/
.cpp)

add_executable(${PROJECT_TEST_NAME} ${TEST_SRC_FILES} ${ALL_SRC_FILES})

add_dependencies(${PROJECT_TEST_NAME} googletest)

include_directories(${GTEST_INCLUDE_DIRS}
${COMMON_INCLUDES}
${COMMON_LOCAL}/include
${EIGEN3_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
)

link_directories(${COMMON_LIB})

ROOT_GENERATE_DICTIONARY(G__TH1F TH1F.h LINKDEF TH1FLinkDef.h)

#—Create a shared library with geneated dictionary

add_library(TH1F SHARED TH1F.cxx G__TH1F.cxx)
target_link_libraries(TH1F ${ROOT_LIBRARIES})

target_link_libraries(${PROJECT_TEST_NAME} tiff jpeg jbig xz z ${GTEST_LIBS_DIR}/libgtest.a ${GTEST_LIBS_DIR}/libgtest_main.a ${CMAKE_THREAD_LIBS_INIT} TH1F
)

add_test(test1 ${PROJECT_TEST_NAME})

###[/code]

can you help me spotting the bug?

Is there some documentation for
ROOT_GENERATE_DICTIONARY and which option to pass to find_package(ROOT REQUIRED … ) ?

Thanks a lot!

TH1F.cxx is part of the Hist library, which is part of ROOT.

add_library(TH1F SHARED TH1F.cxx G__TH1F.cxx)
target_link_libraries(TH1F ${ROOT_LIBRARIES})

The add_library() command tells CMake to make a [shared] library called TH1F using the source files: TH1F.cxx, G__TH1F.cxx, which are expected in the location as the CMakeLists.txt file in the source directory or binary directory as it is the case for the generated G__TH1F.cxx. Are you sure that you have a file called TH1F.cxx in your sources?

Pere

I am bit confused. I was expecting TH1F.cxx to be generated or included from the ROOT install directory using the ROOT_GENERATE_DICTIONARY macro. I installed root using the cmake procedure on my machine.

I have a line in my code that does include TH1F.h header file, how can I use it in a cmake project? Am I misunderstanding the Full example (Event project) in the root.cern.ch/how/integrate-root … ject-cmake documentation?

What is generated with ROOT_GENERATE_DICTIONARY(G__TH1F TH1F.h LINKDEF TH1FLinkDef.h) is the file called G__THF1.cxx (the first argument dictates the file name). Still I do not understand what to try to do creating a dictionary of a class that is already included in the ROOT libraries.

Typically if you define your own classes XXX and YYY in XXX.h and YYY.h and their implementation in XXX.cxx and YYY.cxx and you want to create a library including the dictionary you will do like this:

ROOT_GENERATE_DICTIONARY(MyDict XXX.h YYY.h LINKDEF MyDictLinkDef.h)
add_library(MyLib SHARED XXX.cxx YYY.cxx MyDict.cxx)
target_link_libraries(MyLib ${ROOT_LIBRARIES})

You are right in not understanding, because I was wrongly assuming that I needed the ROOT_GENERATE_DICTIONARY macro to link the libraries. I removed all the useless stuff and just added ${ROOT_LIBRARIES} to target_link_libraries. It works perfectly. Thanks!