How to add library in CMakeLists.txt for Geant4

Hello, everyone!

I am looking for neutron interaction with nanodiamonds. I install the NCrystal library and I have to configure the CMakeLists.txt file for using this library. Could anyone help me, please?

Best regards,
Eyvaz

Provided that the NCrystal package (GitHub - mctools/ncrystal: NCrystal : a library for thermal neutron transport in crystals ?) provides a CMake configuration file, then it should just be a case of adding

find_package(NCrystal REQUIRED)

add_executable(MyExe ...)
target_link_libraries(MyExe PRIVATE <imported target provided by NCrystal>)

e.g. if the target provided by NCrystal follows cmake conventions for imported target naming, this last line will be something like:

target_link_libraries(MyExe PRIVATE NCrystal::NCrystal)

but consult its documentation for this.

What are the errors here? Do You use the NCrystal library for Geant4?

It shouldn’t be necessary (and in fact is bad practice) to put the absolute path to the library in target_link_library. As documented in the NCrystal README, it provides CMake imported targets for which the appropriate invocation would be:

target_link_libraries(ddxTask PRIVATE G4NCrystal::G4NCrystal)

Thank you for your reply. I tried, but it gives an error during run cmake …

“Target “ddxTask” links to target “G4NCrystal::G4NCrystal” but the target
was not found. Perhaps a find_package() call is missing for an IMPORTED
target, or an ALIAS target is missing?”

The problem is here:

if(WITH_GEANT4_UIVIS)
  find_package(Geant4 REQUIRED ui_all vis_all)
else()
  find_package(Geant4 REQUIRED)
  find_package(G4NCrystal REQUIRED)
endif()

G4NCrystal is only found in one branch of the conditional. Since you need it irrespective, just move the find_package out of the conditional.

Thank you for your reply. Should I add link or path to lib? I don’t understand(

Best regards,
Eyvaz

When running CMake, it needs to be told about places to look for additional packages. To do this for G4NCrystal, it looks like from your first post that it’s installed under /home/eyvaz/Geant4/plugin/ncrystal-install. That being the case, it should be sufficient to run cmake for your project as:

cmake -DCMAKE_PREFIX_PATH="/home/eyvaz/Geant4/plugin/ncrystal-install" <otherargs>

See the CMake documentation on find_package for the full details on that command.

Thank You so much! Problem solved)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.