Load different Libraries (VTK Libraries) to the Root

Hello Rooters,

I need to include separate libraries to the root. This is the Library ("wget https://www.vtk.org/files/release/9.0/VTK-9.0.1.tar.gz"). So I tried to install it using the following steps.

"cd /VTK/"
"mkdir build"
"cd build"
"cmake .."

Installation worked well.
I need to load that VTK library to the root. I know that we can use the following command.

root > gSystem->Load("/where/are/you/lib/lib.so")
root > .x test.cpp

In my build directory I found lib64, There are so many libraries. I can load just one library, but how can load all libraries? Here are few libraries in the lib64

libvtkCommonColor-9.0.so                        libvtkFiltersModeling-9.0.so.9.0.0    libvtkloguru-9.0.so.1
libvtkCommonColor-9.0.so.1                      libvtkFiltersSources-9.0.so           libvtkloguru-9.0.so.9.0.0
libvtkCommonColor-9.0.so.9.0.0                  libvtkFiltersSources-9.0.so.1         libvtklz4-9.0.so
libvtkCommonComputationalGeometry-9.0.so        libvtkFiltersSources-9.0.so.9.0.0     libvtklz4-9.0.so.1
libvtkCommonComputationalGeometry-9.0.so.1      libvtkFiltersStatistics-9.0.so        libvtklz4-9.0.so.9.0.0
libvtkCommonComputationalGeometry-9.0.so.9.0.0  libvtkFiltersStatistics-9.0.so.1      libvtklzma-9.0.so
libvtkCommonCore-9.0.so                         libvtkFiltersStatistics-9.0.so.9.0.0  libvtklzma-9.0.so.1
libvtkCommonCore-9.0.so.1                       libvtkFiltersTexture-9.0.so           libvtklzma-9.0.so.9.0.0
libvtkCommonCore-9.0.so.9.0.0                   libvtkFiltersTexture-9.0.so.1         libvtkmetaio-9.0.so
libvtkCommonDataModel-9.0.so                    libvtkFiltersTexture-9.0.so.9.0.0     libvtkmetaio-9.0.so.1
libvtkCommonDataModel-9.0.so.1                  libvtkfreetype-9.0.so                 libvtkmetaio-9.0.so.9.0.0
libvtkCommonDataModel-9.0.so.9.0.0              libvtkfreetype-9.0.so.1               libvtkParallelCore-9.0.so
libvtkCommonExecutionModel-9.0.so               libvtkfreetype-9.0.so.9.0.0           libvtkParallelCore-9.0.so.1
libvtkCommonExecutionModel-9.0.so.1             libvtkImagingColor-9.0.so             libvtkParallelCore-9.0.so.9.0.0
libvtkCommonExecutionModel-9.0.so.9.0.0         libvtkImagingColor-9.0.so.1           libvtkParallelDIY-9.0.so
libvtkCommonMath-9.0.so                         libvtkImagingColor-9.0.so.9.0.0       libvtkParallelDIY-9.0.so.1
libvtkCommonMath-9.0.so.1                       libvtkImagingCore-9.0.so              libvtkParallelDIY-9.0.so.9.0.0
libvtkCommonMath-9.0.so.9.0.0                   libvtkImagingCore-9.0.so.1            libvtkpng-9.0.so
libvtkCommonMisc-9.0.so                         libvtkImagingCore-9.0.so.9.0.0        libvtkpng-9.0.so.1
libvtkCommonMisc-9.0.so.1                       libvtkImagingGeneral-9.0.so           libvtkpng-9.0.so.9.0.0

Any help would be greatly appreciated!!


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

Create a simple “rootlogon.C” file (in your current working directory):

{ // rootlogon.C
  // load all "libvtk*.so" shared libraries (no need to append ".so" in names)
  // note: if they depend on one another, the order matters; e.g., try to
  //       load them in the same order as they had been built: "ls -1rt libvtk*.so"
  // ...

You probably do not need to load all “libvtk*.so” libraries one by one. There are probably only several “top-level” libraries that you need to load “manually”. When they were being built, each of these “top-level” libraries was linked against “lower-level” libraries on which it depends. So, when you load such a “top-level” library, the system will automatically load all required “lower-level” libraries (that it knows about, of course).
Note however that, due to a known cling::DynamicLibraryManager deficiency, you need to add the “/where/are/your/VTK/libs” directory into the “LD_LIBRARY_PATH” environment variable (before starting root), or you need to run (just once):
patchelf --set-rpath "\$ORIGIN" /where/are/your/VTK/libs/libvtk*.so

With the “rootlogon.C” file (in your current working directory) given below, one should be able to run all “VTK-9.0.x/Examples/Tutorial/Step*/Cxx/Cone*.cxx” in ROOT 6, e.g., as “interpreted” C++ macros:

root -q -e ".L Cone.cxx" -e "main();"


[...]$ root
root [0] .L Cone.cxx
root [1] main();
root [2] .q
{ // rootlogon.C ...
  { // VTK-9.0.x ...
    // it is assumed that the VTK-9.0.x has been built using something like this:
    //   mkdir /path/to/build/dir/VTK-9.0.x; cd /path/to/build/dir/VTK-9.0.x
    //   cmake -DCMAKE_INSTALL_PREFIX=${HOME}/work/VTK/VTK-9.0 /path/to/source/dir/VTK-9.0.x
    //   make install # build and then install everything in "CMAKE_INSTALL_PREFIX"
    //   patchelf --set-rpath "\$ORIGIN" ${HOME}/work/VTK/VTK-9.0/lib*/libvtk*.so # set "RUNPATH"
    TString prefix = "${HOME}/work/VTK/VTK-9.0"; // same as "CMAKE_INSTALL_PREFIX"
    TString lib = "lib"; // "lib" or "lib64" (usually depends on the architecture)
    TString version = "-9.0"; // e.g., "" or "-9.0" (note: it's without the "patch version" here)
    if (!prefix.EndsWith("/")) prefix += "/";
    gSystem->SetDynamicPath(gSystem->ExpandPathName(prefix + lib + ":") + TString(gSystem->GetDynamicPath()));
    gInterpreter->AddIncludePath(gSystem->ExpandPathName(prefix + "include/vtk" + version));
#if 0 /* 0 or 1 */
    // these environment variables may be needed if one plans to execute shell commands from inside of a ROOT session
    gSystem->Setenv("LD_LIBRARY_PATH", gSystem->ExpandPathName(prefix + lib + ":") + TString(gSystem->Getenv("LD_LIBRARY_PATH")));
    gSystem->Setenv("PATH", gSystem->ExpandPathName(prefix + "bin:") + TString(gSystem->Getenv("PATH")));
    TString python = "python2.7"; // e.g., "python2.7" or "python3.8"
    // TPRegexp(".*PYTHON_VERSION_MAJOR=([0-9]*).+PYTHON_VERSION_MINOR=([0-9]*).*").Substitute(python = gROOT->GetConfigOptions(), "python$1.$2");
    gSystem->Setenv("PYTHONPATH", gSystem->ExpandPathName(prefix + lib + "/" + python + "/site-packages:") + TString(gSystem->Getenv("PYTHONPATH")));
#endif /* 0 or 1 */
    // these VTK components are needed to run all "VTK-9.0.x/Examples/Tutorial/Step*/Cxx/Cone*.cxx"
    std::vector<TString> components
      { "CommonCore",
        "InteractionWidgets", // needed by "Step6/Cxx/Cone6.cxx" only
        "RenderingOpenGL2" };
    // load the corresponding shared libraries
    for (unsigned long i = 0; i < components.size(); i++) {
      TString l = gSystem->ExpandPathName("libvtk" + components[i] + version);
      int v = gSystem->Load(l);
      if (v < 0) std::cout << "Warning: cannot load " << l << " : " << v << std::endl;
    // initialize VTK components (i.e., these which need it)
    if (!(gInterpreter->IsLoaded("vtkAutoInit.h")))
      gInterpreter->ProcessLine("#include \"vtkAutoInit.h\"");
    for (unsigned long i = 0; i < components.size(); i++) {
      TString m = "vtk" + components[i];
      gInterpreter->Declare("VTK_AUTOINIT_DECLARE(" + m + ");");
      if (gSystem->DynFindSymbol(nullptr, gInterpreter->GetMangledName(0, m + "_AutoInit_Construct", "")))
        gInterpreter->Declare("VTK_MODULE_INIT(" + m + ");");
  } // ... VTK-9.0.x
} // ... rootlogon.C

BTW. There exists also an “accompanying” discussion on the VTK forum: “Using VTK shared libraries in (CERN) ROOT”

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