Linking TSpectrum

Hi all,
While compiling my code one error is coming following this.

undefined reference to `TSpectrum::TSpectrum(int, double)’
collect2: error: ld returned 1 exit status

I saw from previous post that this is a problem of linking library lSpectrum.
Here I am compiling the code using cmake …/. and then doing make.

How do i can link this library here ?

Thank you.

Maybe adding Spectrum in your target_link_libraries ?

something along the following lines

add_executable(yourApp yourApp.cxx)
target_link_libraries(yourApp PRIVATE Spectrum)
1 Like

How to do that ?

Thanks

See the reply from Eddy above (modify your CMakeLists.txt)

I can not understand in which CMakeLists.cxx i have to do the change because there are many.

How many projects do you have?

I found it. I am writing the CMakeLists.cxx . What should I write exactly ?

add_executable(single single.cpp ${sources} ${headers})
target_link_libraries(single PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(fast-lenses reco_fast.cpp ${sources} ${headers})
target_link_libraries(fast-lenses PUBLIC Struct ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(full-lenses reco_full.cpp ${sources} ${headers})
target_link_libraries(full-lenses PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(convert src/convert.C ${sources} ${headers})
target_link_libraries(convert PUBLIC Struct ${ROOT_LIBRARIES} ${edepsim_io})

#add_executable(vertex manual_vertex.cpp ${sources} ${headers})
#target_link_libraries(vertex PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(geo src/geometry.C ${sources} ${headers})
target_link_libraries(geo PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(lecce_save src/lecce_save.C ${sources} ${headers})
target_link_libraries(lecce_save PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(visible src/visible_tracks.C ${sources} ${headers})
target_link_libraries(visible PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(ele_ana ele_ana.cpp ${sources} ${headers})
target_link_libraries(ele_ana PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})


add_executable(spill_study spill_study.cpp ${sources} ${headers})
target_link_libraries(spill_study PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(Read_spill tools/Read_spill.cpp ${sources} ${headers})
target_link_libraries(Read_spill PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

target_link_libraries(???????)

Thank you.

And the error that is coming ,

usr/bin/ld: CMakeFiles/Read_spill.dir/tools/Read_spill.cpp.o: in function main': Read_spill.cpp:(.text+0x1870): undefined reference to TSpectrum::TSpectrum(int, double)’
collect2: error: ld returned 1 exit status

Like this:

target_link_libraries(single PUBLIC ${ROOT_LIBRARIES} Spectrum ${edepsim_io})

for all the target requiring Spectrum

Thank you very much for the solution but another new error is coming.

/usr/bin/ld: cannot find -lSpectrum

Is any library missing ?

Check if there is a libSpectrum.so in the ${ROOTSYS}/lib directory

yes, it is present

Can you attach (or copy the whole content of) your CMakeLists.txt, so we can see what’s wrong, instead of trying to guess?

# CMakeLists.txt for the "lar-lenses" package.
# If ROOT is not installed in a default system location you need to tell CMake where to find it.
# Sourcing `thisroot.sh` already sets the required environment variables.
# Otherwise, you'll have to tell the build system where to look for ROOT,
# e.g. by passing `-DROOT_DIR="/path/to/root/installation` at cmake configuration time.

cmake_minimum_required(VERSION 2.8)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
  cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()

project(lar-lenses LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_PLATFORM_INDEPENDENT_CODE ON)

#---Check "ROOT" environment variable is set
#message("THING:" $ENV{THING})
if(NOT DEFINED ENV{ROOTSYS})
    message(FATAL_ERROR "ROOTSYS environment variable not defined")
endif()

#---Locate the ROOT package and defines a number of variables (e.g. ROOT_INCLUDE_DIRS)
find_package(ROOT REQUIRED COMPONENTS Geom Physics Matrix MathCore MathMore RIO Net Tree TreePlayer)
find_library(ROOT_TREEPLAYER_LIBRARY TreePlayer HINTS ${ROOT_LIBRARY_DIR} REQUIRED)
find_library(ROOT_MATHMORE_LIBRARY MathMore HINTS ${ROOT_LIBRARY_DIR} REQUIRED)

#---Include edepsim-io libraries
find_library(edepsim_io NAMES edepsim_io PATHS ENV LD_LIBRARY_PATH)

if(NOT edepsim_io)
  message(FATAL_ERROR "Failed to find the edepsim_io library.")
endif()

#---Include ROOT / edepsim-io / local project
include_directories(${ROOT_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/include $ENV{EDEPSIM_IO_DIR})

file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cpp)
file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.h)

# Creates a libStruct shared library with a generated dictionary.
ROOT_GENERATE_DICTIONARY(G__Struct struct.h LINKDEF include/Linkdef.h)
add_library(Struct SHARED G__Struct.cxx)
target_link_libraries(Struct PUBLIC ROOT::RIO ROOT::Net)


#----------------------------------------------------------------------------
# Add the executables

add_executable(single single.cpp ${sources} ${headers})
target_link_libraries(single PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(fast-lenses reco_fast.cpp ${sources} ${headers})
target_link_libraries(fast-lenses PUBLIC Struct ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(full-lenses reco_full.cpp ${sources} ${headers})
target_link_libraries(full-lenses PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(convert src/convert.C ${sources} ${headers})
target_link_libraries(convert PUBLIC Struct ${ROOT_LIBRARIES} ${edepsim_io})

#add_executable(vertex manual_vertex.cpp ${sources} ${headers})
#target_link_libraries(vertex PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(geo src/geometry.C ${sources} ${headers})
target_link_libraries(geo PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(lecce_save src/lecce_save.C ${sources} ${headers})
target_link_libraries(lecce_save PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(visible src/visible_tracks.C ${sources} ${headers})
target_link_libraries(visible PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(ele_ana ele_ana.cpp ${sources} ${headers})
target_link_libraries(ele_ana PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})


add_executable(spill_study spill_study.cpp ${sources} ${headers})
target_link_libraries(spill_study PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(Read_spill tools/Read_spill.cpp ${sources} ${headers})
target_link_libraries(Read_spill PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

target_link_libraries( single PUBLIC ${ROOT_LIBRARIES} Spectrum ${edepsim_io})

#--------------------------------------------------------------------------
# Copy all scripts/stuff to the build directory

set(OptMen_SCRIPTS setup_CNAF run_fast.sh run_full.sh run_single.sh run_visible.sh run_lecce_save.sh submit_fast.sub submit_full.sub)

foreach(_script ${OptMen_SCRIPTS})
  configure_file(
    ${PROJECT_SOURCE_DIR}/${_script}
    ${PROJECT_BINARY_DIR}/${_script}
    COPYONLY
    )
endforeach()

You add this line:

link_directories(${ROOT_LIBRARY_DIR})

after

include_directories(${ROOT_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/include $ENV{EDEPSIM_IO_DIR})

Or you can try to replace Spectrum by ROOT::Spectrum:

target_link_libraries( single PUBLIC ${ROOT_LIBRARIES} ROOT::Spectrum ${edepsim_io})

Thank you . But the error is still there.

When i am doing this,

add_executable(spill_study spill_study.cpp ${sources} ${headers})
target_link_libraries(spill_study PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(Read_spill tools/Read_spill.cpp ${sources} ${headers})
target_link_libraries(Read_spill PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

target_link_libraries( single PUBLIC ${ROOT_LIBRARIES} ROOT::Spectrum ${edepsim_io})

the following error is coming:

/usr/bin/ld: CMakeFiles/Read_spill.dir/tools/Read_spill.cpp.o: in function `main':
Read_spill.cpp:(.text+0x1870): undefined reference to `TSpectrum::TSpectrum(int, double)'

And if I do this ,

include_directories(${ROOT_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/include $ENV{EDEPSIM_IO_DIR})
link_directories(${ROOT_LIBRARY_DIR})
target_link_libraries( single PUBLIC ${ROOT_LIBRARIES} Spectrum ${edepsim_io})

The following error is coming,

/usr/bin/ld: cannot find -lSpectrum

Thank you again for your help.

And if you add Spectrum in find_package ?

find_package(ROOT REQUIRED COMPONENTS Geom Physics Matrix MathCore MathMore RIO Net Spectrum Tree TreePlayer)

Note that you can most probably simplify your CMakeLists.txt

it is giving same problem…if I am compiling any code using g++ then also same problem arises but if i use -lSpectrum there the problem resolves.

Can you try with this:

# CMakeLists.txt for the "lar-lenses" package.
# If ROOT is not installed in a default system location you need to tell CMake where to find it.
# Sourcing `thisroot.sh` already sets the required environment variables.
# Otherwise, you'll have to tell the build system where to look for ROOT,
# e.g. by passing `-DROOT_DIR="/path/to/root/installation` at cmake configuration time.

cmake_minimum_required(VERSION 2.8)
if(${CMAKE_VERSION} VERSION_LESS 3.12)
  cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()

project(lar-lenses LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_PLATFORM_INDEPENDENT_CODE ON)

#---Check "ROOT" environment variable is set
#message("THING:" $ENV{THING})
if(NOT DEFINED ENV{ROOTSYS})
    message(FATAL_ERROR "ROOTSYS environment variable not defined")
endif()

#---Locate the ROOT package and defines a number of variables (e.g. ROOT_INCLUDE_DIRS)
find_package(ROOT REQUIRED)

#---Include edepsim-io libraries
find_library(edepsim_io NAMES edepsim_io PATHS ENV LD_LIBRARY_PATH)

if(NOT edepsim_io)
  message(FATAL_ERROR "Failed to find the edepsim_io library.")
endif()

#---Include ROOT / edepsim-io / local project
include_directories(${ROOT_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/include $ENV{EDEPSIM_IO_DIR})
link_directories(${ROOT_LIBRARY_DIR})

file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cpp)
file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.h)

# Creates a libStruct shared library with a generated dictionary.
ROOT_GENERATE_DICTIONARY(G__Struct struct.h LINKDEF include/Linkdef.h)
add_library(Struct SHARED G__Struct.cxx)
target_link_libraries(Struct PUBLIC ROOT::RIO ROOT::Net)


#----------------------------------------------------------------------------
# Add the executables

add_executable(single single.cpp ${sources} ${headers})
target_link_libraries(single PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(fast-lenses reco_fast.cpp ${sources} ${headers})
target_link_libraries(fast-lenses PUBLIC Struct ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(full-lenses reco_full.cpp ${sources} ${headers})
target_link_libraries(full-lenses PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(convert src/convert.C ${sources} ${headers})
target_link_libraries(convert PUBLIC Struct ${ROOT_LIBRARIES} ${edepsim_io})

#add_executable(vertex manual_vertex.cpp ${sources} ${headers})
#target_link_libraries(vertex PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(geo src/geometry.C ${sources} ${headers})
target_link_libraries(geo PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(lecce_save src/lecce_save.C ${sources} ${headers})
target_link_libraries(lecce_save PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(visible src/visible_tracks.C ${sources} ${headers})
target_link_libraries(visible PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(ele_ana ele_ana.cpp ${sources} ${headers})
target_link_libraries(ele_ana PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})


add_executable(spill_study spill_study.cpp ${sources} ${headers})
target_link_libraries(spill_study PUBLIC ${ROOT_LIBRARIES} ${edepsim_io})

add_executable(Read_spill tools/Read_spill.cpp ${sources} ${headers})
target_link_libraries(Read_spill PUBLIC ${ROOT_LIBRARIES} ROOT::Spectrum ${edepsim_io})

#--------------------------------------------------------------------------
# Copy all scripts/stuff to the build directory

set(OptMen_SCRIPTS setup_CNAF run_fast.sh run_full.sh run_single.sh run_visible.sh run_lecce_save.sh submit_fast.sub submit_full.sub)

foreach(_script ${OptMen_SCRIPTS})
  configure_file(
    ${PROJECT_SOURCE_DIR}/${_script}
    ${PROJECT_BINARY_DIR}/${_script}
    COPYONLY
    )
endforeach()