Dear Root forum members,
I am hitting a problem with cmake compilation and ROOT libraries.
I have followed the ROOT guide to create a CMakeLists for my code and I managed to have a compiled running code. (Here the guide that I have followed: https://root.cern.ch/how/integrate-root-my-project-cmake)
Now, I want to add to my code some external libraries for Neural Network. This is pytorch.
They come with a make example that you can find here:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(custom_ops)
find_package(Torch REQUIRED)
add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 11)
In order to be compatible with RDataFrame I am targeting CXX_STANDARD 17:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(event)
# You need to tell CMake where to find the ROOT installation. This can be done in a number of ways:
# - ROOT built with classic configure/make use the provided $ROOTSYS/etc/cmake/FindROOT.cmake
# - ROOT built with CMake. Add in CMAKE_PREFIX_PATH the installation prefix for ROOT
set(ENV{TORCH} "lib/libtorch")
list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
list(APPEND CMAKE_PREFIX_PATH $ENV{TORCH})
foreach(path ${CMAKE_PREFIX_PATH})
message("Path: " ${path})
endforeach(path)
message("Torch Libraries: " ${TORCH_LIBRARIES})
#---Locate the ROOT package and defines a number of variables (e.g. ROOT_INCLUDE_DIRS)
find_package(ROOT REQUIRED COMPONENTS MathCore RIO Hist Tree Net)
find_package(Torch REQUIRED)
#---Define useful ROOT functions and macros (e.g. ROOT_GENERATE_DICTIONARY)
include(${ROOT_USE_FILE})
include_directories(${CMAKE_SOURCE_DIR} ${ROOT_INCLUDE_DIRS})
include_directories(./include/)
add_definitions(${ROOT_CXX_FLAGS})
ROOT_GENERATE_DICTIONARY(G__LoopSUSYThTh include/LoopSUSYThTh.h)
ROOT_GENERATE_DICTIONARY(G__LoopSUSYTN1TbC1 include/LoopSUSYTN1TbC1.h)
ROOT_GENERATE_DICTIONARY(G__LoopSUSYTZTZ include/LoopSUSYTZTZ.h)
ROOT_GENERATE_DICTIONARY(G__LoopSUSYSleptons include/LoopSUSYSleptons.h)
ROOT_GENERATE_DICTIONARY(G__LoopSUSYFrame include/LoopSUSYFrame.h)
ROOT_GENERATE_DICTIONARY(G__LoopSUSYTorch include/LoopSUSYTorch.h)
ROOT_GENERATE_DICTIONARY(G__GenericHelpers include/GenericHelpers.h)
ROOT_GENERATE_DICTIONARY(G__LeptonHelpers include/LeptonHelpers.h)
ROOT_GENERATE_DICTIONARY(G__LoopSUSYClaudio include/LoopSUSYClaudio.h)
#---Create a shared library with geneated dictionary
add_library(libLoopSUSYThTh SHARED src/LoopSUSYThTh.cxx)
add_library(libLoopSUSYTN1TbC1 SHARED src/LoopSUSYTN1TbC1.cxx)
add_library(libLoopSUSYTZTZ SHARED src/LoopSUSYTZTZ.cxx)
add_library(libLoopSUSYSleptons SHARED src/LoopSUSYSleptons.cxx)
add_library(libLoopSUSYFrame SHARED src/LoopSUSYFrame.cxx)
add_library(libLoopSUSYClaudio SHARED src/LoopSUSYClaudio.cxx)
add_library(libLoopSUSYTorch SHARED src/LoopSUSYTorch.cxx)
add_library(libGenericHelpers SHARED src/GenericHelpers.cxx)
add_library(libLeptonHelpers SHARED src/LeptonHelpers.cxx)
# THIS LINE IS CREATING THE ISSUE
#target_link_libraries(libLoopSUSYThTh "${TORCH_LIBRARIES}")
target_link_libraries(libLoopSUSYThTh ${ROOT_LIBRARIES})# ${TORCH_LIBRARIES})
target_link_libraries(libLoopSUSYTZTZ libLeptonHelpers libLoopSUSYTN1TbC1 libLoopSUSYFrame libLoopSUSYClaudio libLoopSUSYTorch libGenericHelpers ${ROOT_LIBRARIES})# ${TORCH_LIBRARIES})
#---Create a main program using the library
add_executable(ntupleMaker src/main.cxx)
target_link_libraries(ntupleMaker libLoopSUSYThTh libLoopSUSYTZTZ libLeptonHelpers libLoopSUSYTN1TbC1 libLoopSUSYSleptons libLoopSUSYTZTZ libLoopSUSYFrame libLoopSUSYClaudio libLoopSUSYTorch libGenericHelpers)
set_property(TARGET ntupleMaker libLoopSUSYThTh libLoopSUSYTZTZ libLeptonHelpers libLoopSUSYTN1TbC1 libLoopSUSYSleptons libLoopSUSYTZTZ libLoopSUSYFrame libLoopSUSYClaudio libLoopSUSYTorch libGenericHelpers PROPERTY CXX_STANDARD 17)
Now the issue shows up, if I add the line target_link_libraries(libLoopSUSYThTh “${TORCH_LIBRARIES}”) for linking my libtorch NN, I got the following error:
-- The C compiler identification is GNU 6.2.0
-- The CXX compiler identification is GNU 6.2.0
-- Check for working C compiler: /cvmfs/sft.cern.ch/lcg/releases/gcc/6.2.0-b9934/x86_64-centos7/bin/gcc
-- Check for working C compiler: /cvmfs/sft.cern.ch/lcg/releases/gcc/6.2.0-b9934/x86_64-centos7/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /cvmfs/sft.cern.ch/lcg/releases/gcc/6.2.0-b9934/x86_64-centos7/bin/g++
-- Check for working CXX compiler: /cvmfs/sft.cern.ch/lcg/releases/gcc/6.2.0-b9934/x86_64-centos7/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
Path: /cvmfs/sft-nightlies.cern.ch/lcg/nightlies/dev3python3/Thu/ROOT/HEAD/x86_64-centos7-gcc62-opt
Path: lib/libtorch
Torch Libraries:
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found torch: /terabig/amiucci/LoopSUSYFrame_rel21/LoopSUSYFrame/lib/libtorch/lib/libtorch.so
-- Configuring done
-- Generating done
-- Build files have been written to: /terabig/amiucci/LoopSUSYFrame_rel21/LoopSUSYFrame/build
Scanning dependencies of target libLoopSUSYFrame
[ 5%] Building CXX object CMakeFiles/libLoopSUSYFrame.dir/src/LoopSUSYFrame.cxx.o
[ 10%] Linking CXX shared library liblibLoopSUSYFrame.so
[ 10%] Built target libLoopSUSYFrame
Scanning dependencies of target libLeptonHelpers
[ 15%] Building CXX object CMakeFiles/libLeptonHelpers.dir/src/LeptonHelpers.cxx.o
/terabig/amiucci/LoopSUSYFrame_rel21/LoopSUSYFrame/src/LeptonHelpers.cxx: In function ‘std::pair<std::array<float, 4ul>, std::array<int, 3ul> > ClosestMllVars(const std::vector<short unsigned int, std::allocator<short unsigned int> >&, const std::vector<float, std::allocator<float> >&, const std::vector<float, std::allocator<float> >&, const std::vector<float, std::allocator<float> >&, const std::vector<float, std::allocator<float> >&, const std::vector<int, std::allocator<int> >&)’:
/terabig/amiucci/LoopSUSYFrame_rel21/LoopSUSYFrame/src/LeptonHelpers.cxx:107:46: warning: narrowing conversion of ‘(((double)closest_mll) / 1.0e+3)’ from ‘double’ to ‘float’ inside { } [-Wnarrowing]
std::array<float, 4> the_vars = {closest_mll/1000., closest_ptll/1000., closest_dphill, closest_phi};
~~~~~~~~~~~^~~~~~
/terabig/amiucci/LoopSUSYFrame_rel21/LoopSUSYFrame/src/LeptonHelpers.cxx:107:66: warning: narrowing conversion of ‘(((double)closest_ptll) / 1.0e+3)’ from ‘double’ to ‘float’ inside { } [-Wnarrowing]
std::array<float, 4> the_vars = {closest_mll/1000., closest_ptll/1000., closest_dphill, closest_phi};
~~~~~~~~~~~~^~~~~~
[ 20%] Linking CXX shared library liblibLeptonHelpers.so
[ 20%] Built target libLeptonHelpers
Scanning dependencies of target libLoopSUSYTorch
[ 25%] Building CXX object CMakeFiles/libLoopSUSYTorch.dir/src/LoopSUSYTorch.cxx.o
[ 30%] Linking CXX shared library liblibLoopSUSYTorch.so
[ 30%] Built target libLoopSUSYTorch
Scanning dependencies of target libLoopSUSYThTh
[ 35%] Building CXX object CMakeFiles/libLoopSUSYThTh.dir/src/LoopSUSYThTh.cxx.o
[ 40%] Linking CXX shared library liblibLoopSUSYThTh.so
[ 40%] Built target libLoopSUSYThTh
Scanning dependencies of target libLoopSUSYTN1TbC1
[ 45%] Building CXX object CMakeFiles/libLoopSUSYTN1TbC1.dir/src/LoopSUSYTN1TbC1.cxx.o
[ 50%] Linking CXX shared library liblibLoopSUSYTN1TbC1.so
[ 50%] Built target libLoopSUSYTN1TbC1
Scanning dependencies of target libGenericHelpers
[ 55%] Building CXX object CMakeFiles/libGenericHelpers.dir/src/GenericHelpers.cxx.o
[ 60%] Linking CXX shared library liblibGenericHelpers.so
[ 60%] Built target libGenericHelpers
Scanning dependencies of target libLoopSUSYClaudio
[ 65%] Building CXX object CMakeFiles/libLoopSUSYClaudio.dir/src/LoopSUSYClaudio.cxx.o
[ 70%] Linking CXX shared library liblibLoopSUSYClaudio.so
[ 70%] Built target libLoopSUSYClaudio
Scanning dependencies of target libLoopSUSYTZTZ
[ 75%] Building CXX object CMakeFiles/libLoopSUSYTZTZ.dir/src/LoopSUSYTZTZ.cxx.o
[ 80%] Linking CXX shared library liblibLoopSUSYTZTZ.so
[ 80%] Built target libLoopSUSYTZTZ
Scanning dependencies of target libLoopSUSYSleptons
[ 85%] Building CXX object CMakeFiles/libLoopSUSYSleptons.dir/src/LoopSUSYSleptons.cxx.o
[ 90%] Linking CXX shared library liblibLoopSUSYSleptons.so
[ 90%] Built target libLoopSUSYSleptons
Scanning dependencies of target ntupleMaker
[ 95%] Building CXX object CMakeFiles/ntupleMaker.dir/src/main.cxx.o
[100%] Linking CXX executable ntupleMaker
CMakeFiles/ntupleMaker.dir/src/main.cxx.o: In function `main':
main.cxx:(.text+0x1abf): undefined reference to `TString::TString(std::string const&)'
main.cxx:(.text+0x1df7): undefined reference to `TString::TString(std::string const&)'
main.cxx:(.text+0x2034): undefined reference to `TString::TString(std::string const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::GraphDrawing::CreateDefineNode(std::string const&, ROOT::Detail::RDF::RCustomColumnBase const*)'
liblibLoopSUSYThTh.so: undefined reference to `AddWeightBranches(std::vector<std::string, std::allocator<std::string> >&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::RActionBase::RActionBase(ROOT::Detail::RDF::RLoopManager*, std::vector<std::string, std::allocator<std::string> > const&, ROOT::Internal::RDF::RBookedCustomColumns&&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::TTreeReaderValueBase::GetElementTypeName(std::type_info const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::InterpreterCalc(std::string const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::FindUndefinedDSColumns(std::vector<std::string, std::allocator<std::string> > const&, std::vector<std::string, std::allocator<std::string> > const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::RColumnValue<float>::MakeProxy(TTreeReader*, std::string const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::RColumnValue<int>::MakeProxy(TTreeReader*, std::string const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::PrettyPrintAddr(void const*)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Detail::RDF::RLoopManager::GetBranchNames()'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::DemangleTypeIdName(std::type_info const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::CheckCustomColumn(std::experimental::fundamentals_v1::basic_string_view<char, std::char_traits<char> >, TTree*, std::vector<std::string, std::allocator<std::string> > const&, std::vector<std::string, std::allocator<std::string> > const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::GraphDrawing::CheckIfDefaultOrDSColumn(std::string const&, std::shared_ptr<ROOT::Detail::RDF::RCustomColumnBase> const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::GetBranchNames(TTree&, bool)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::BookDefineJit(std::experimental::fundamentals_v1::basic_string_view<char, std::char_traits<char> >, std::experimental::fundamentals_v1::basic_string_view<char, std::char_traits<char> >, ROOT::Detail::RDF::RLoopManager&, ROOT::RDF::RDataSource*, std::shared_ptr<ROOT::Detail::RDF::RJittedCustomColumn> const&, ROOT::Internal::RDF::RBookedCustomColumns const&, std::vector<std::string, std::allocator<std::string> > const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::GetValidatedColumnNames(ROOT::Detail::RDF::RLoopManager&, unsigned int, std::vector<std::string, std::allocator<std::string> > const&, std::vector<std::string, std::allocator<std::string> > const&, ROOT::RDF::RDataSource*)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::InterpreterDeclare(std::string const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::ColumnName2ColumnTypeName(std::string const&, unsigned int, TTree*, ROOT::RDF::RDataSource*, bool, bool, unsigned int)'
liblibLoopSUSYThTh.so: undefined reference to `GetLeptonBranches(bool, std::string const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::RColumnValue<std::vector<int, std::allocator<int> > >::MakeProxy(TTreeReader*, std::string const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::RColumnValue<unsigned int>::MakeProxy(TTreeReader*, std::string const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::TypeID2TypeName(std::type_info const&)'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Detail::RDF::RCustomColumnBase::GetName() const'
liblibLoopSUSYThTh.so: undefined reference to `ROOT::Internal::RDF::RColumnValue<std::vector<float, std::allocator<float> > >::MakeProxy(TTreeReader*, std::string const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [ntupleMaker] Errore 1
make[1]: *** [CMakeFiles/ntupleMaker.dir/all] Errore 2
make: *** [all] Errore 2
This is weird because I was not expecting the link of another library messing up the one for ROOT.
On my code, at the moment, there is nothing that uses the libtorch library, I am planning to add the relevant code in a next step.
Thank you very much for any inputs there,
Cheers,
Antonello