Cannot wrapping module for python

Hi all,
as I early mentioned on title, I’m trouble with the wrapping of custom classes to python. I had search for long time but i didnt found nothing to solve my case.
Below all steps to reproduce the error:

git clone --depth 1 --branch v9.4.1 https://gitlab.kitware.com/vtk/vtk.git

cd vtk/Examples/Modules/Wrapping

python -m venv venv

source venv/bin/activate

pip install vtk

mkdir build && cd build

cmake .. \
    -DVTK_DIR=../venv/lib/python3.12/site-packages/vtkmodules/cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DVTK_WRAP_PYTHON=ON

cmake --build .

then I’ve copied “wrapping” folder from build/lib/python3/dist-packages/wrapping to venv/lib/python3.12/site-packages/
and copied “libvtkWrappable.so” from build/lib/ to venv/lib

after that

python
Python 3.12.7 (main, Feb 12 2025, 12:16:38) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wrapping.vtkWrappable
Errore di segmentazione (core dump creato)

I can’t say what goes wrong and I dont have found a solid guide to build correctly this wrapper.
The main goal is to subclass the vtkLegendScaleActor (in c++) in order to import on a python script beacuse I need to modify the logic on BuildRepresentation method, but for now I’m focusing to get a example build.
Can you help me to solve this? Thanks in advance

Your VTK build is probably subtly incompatible with the wheel VTK. You might have better luck grabbing the relevant wheel SDK bundle (named vtk-wheel-sdk-* on this page) and building projects against it.

Hi Ben, thanks for your reply.
Finally I have build the wrapper and works when imported on a custom venv. Now I’m facing another problem that concerns vtkCommonCore compatibiliy. My lib links to my system’s vtk lib, and I think need to build wrapper linking to venv vtk libs.
The error:

    from customodules.renderingAnnotation import LegendScaleActor
ImportError: Initialization failed for renderingAnnotation, not compatible with vtkmodules.vtkCommonCore

This appens when the vtk lib goes installed trough pip, but when I copy the entire vtk lib generated on my system (using the same version 9.4.1) this problem goes away.
In any case i need to use pip’s vtk lib.
Any suggestions?

Yes, you do. There are SDK bundles to be compatible with the wheels on the page I linked to here.

I’m trying to build it with your advice, but the lib points to system’s lib

ldd librenderingAnnotation.so
        linux-vdso.so.1 (0x00007fff0d5c1000)
        libvtkRenderingAnnotation-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkRenderingAnnotation-9.4.so.1 (0x000070fe2a751000)
        libvtkRenderingFreeType-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkRenderingFreeType-9.4.so.1 (0x000070fe2a6ab000)
        libvtkRenderingCore-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkRenderingCore-9.4.so.1 (0x000070fe2a400000)
        libvtkCommonCore-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkCommonCore-9.4.so.1 (0x000070fe28a00000)
        libvtksys-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtksys-9.4.so.1 (0x000070fe2a3a8000)
        libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x000070fe28600000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x000070fe2a34e000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x000070fe28200000)
        libvtkFiltersSources-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkFiltersSources-9.4.so.1 (0x000070fe2890c000)
        libvtkFiltersGeneral-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkFiltersGeneral-9.4.so.1 (0x000070fe27c00000)
        libvtkFiltersCore-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkFiltersCore-9.4.so.1 (0x000070fe27000000)
        libvtkCommonExecutionModel-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkCommonExecutionModel-9.4.so.1 (0x000070fe284ed000)
        libvtkCommonDataModel-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkCommonDataModel-9.4.so.1 (0x000070fe26800000)
        libvtkCommonTransforms-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkCommonTransforms-9.4.so.1 (0x000070fe288c7000)
        libvtkCommonMath-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkCommonMath-9.4.so.1 (0x000070fe2889d000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x000070fe27b17000)
        libvtkfreetype-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkfreetype-9.4.so.1 (0x000070fe28435000)
        libvtkCommonColor-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkCommonColor-9.4.so.1 (0x000070fe2a32d000)
        libvtkCommonComputationalGeometry-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkCommonComputationalGeometry-9.4.so.1 (0x000070fe281ca000)
        libvtkFiltersGeometry-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkFiltersGeometry-9.4.so.1 (0x000070fe26e81000)
        libvtkCommonSystem-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkCommonSystem-9.4.so.1 (0x000070fe28415000)
        libvtkCommonMisc-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkCommonMisc-9.4.so.1 (0x000070fe26200000)
        libvtktoken-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtktoken-9.4.so.1 (0x000070fe28881000)
        libvtkdoubleconversion-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkdoubleconversion-9.4.so.1 (0x000070fe27b05000)
        libvtkloguru-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkloguru-9.4.so.1 (0x000070fe27af1000)
        /lib64/ld-linux-x86-64.so.2 (0x000070fe2a897000)
        libvtkFiltersVerdict-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkFiltersVerdict-9.4.so.1 (0x000070fe26e42000)
        libvtkfmt-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkfmt-9.4.so.1 (0x000070fe27ac9000)
        libvtkFiltersReduction-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkFiltersReduction-9.4.so.1 (0x000070fe26621000)
        libvtkpugixml-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkpugixml-9.4.so.1 (0x000070fe261c3000)
        libvtkkissfft-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkkissfft-9.4.so.1 (0x000070fe281c3000)
        libvtkzlib-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkzlib-9.4.so.1 (0x000070fe26e24000)
        libvtkverdict-9.4.so.1 => /usr/local/vtk-9.4.1/lib/libvtkverdict-9.4.so.1 (0x000070fe2618f000)

Can i ask you how to build correctly, step by step? Probably I’m missing somethiing but I don’t know what.
Thanks in advance

@jcfr or @banesullivan Do you have experience using these SDKs?

I think this can be fixed by setting INSTALL_RPATH to something relative to $ORIGIN when you build your project, e.g. $ORIGIN/../vtkmodules. I’m foggy on the details because I haven’t worked with rpath-related settings recently.

Hi all,
I want to share some updates.
After serveral attemps I managed to build the lib with SDK download from here as suggested by Ben.
The SDK appears incomplete because can’t resolve symbols like “vtkObject::GetObjectDescription()”

Python 3.12.7 (main, Feb 12 2025, 12:16:38) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from mymodules import renderingAnnotation
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /home/matteo/test/vtk_modules/Custom/venv/lib/python3.12/site-packages/mymodules/librenderingAnnotation.so: undefined symbol: _ZNK9vtkObject20GetObjectDescriptionB5cxx11Ev

What I’m missing now?
The SDK is vtk-wheel-sdk-9.4.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.tar.xz

In the other way, with full vtk built, this problem goes away, but cannot “communicate” with vtkCommonCore of pip’s version.

The missing symbol seems to be a mangling issue. The symbol provided by the SDK’s libvtkCommonCore.so is:

_ZNK9vtkObject20GetObjectDescriptionEv

But the symbol your build wants is

_ZNK9vtkObject20GetObjectDescriptionB5cxx11Ev

So the build is adding B5cxx11 to the symbol. I’m not familiar with “Bnxxxx” mangling, but you can try adding the following to your CMakeLists.txt, which will match with the C++ std version used by the SDK:

set(CMAKE_CXX_STANDARD 11)

Follow-up: B5cxx11 is abi:cxx11, which is described here:
https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
According to that, abi:cxx11 can be removed from symbols by adding this (e.g. to CMakeLists.txt):

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

The symbol for the GetObjectDescription() method is afflicted because it returns a string, and the ABI for strings changed between c++98 and c++11.

Hi all!
Friday on late I’ve resolved and I promised to myself to reply here today.
But I see only today that David has answer with the same solution that I’ve discovered too.

So thanks for the support and for answering. Probably that isn’t a best practice but for now is the only way that permits me to obtain an external library based on vtk wheel.

In any case if you got a better way to do that i’ll appreciate to know it.
For now thanks to all