因为之前调试VTK8.1.2没有问题,虽然现在VTK已经更新到9.0版本了,但是还是希望安装VTK8.1.2,顺便把安装过程记录下来
一、下载VTK8.1.2
https://github.com/Kitware/VTK/tree/v8.1.2
二、解压安装包并进入,新建build文件夹
unzip VTK-8.1.2.zip
cd VTK-8.1.2
mkdir build && cd build
三、使用cmake配置参数
刚开始直接cmake .. 不报错,但是一加python支持就报错
/usr/bin/ld: 找不到 -lsocket
collect2: error: ld returned 1 exit status
make[1]: *** [CMakeFiles/cmTC_e950c.dir/build.make:87:cmTC_e950c] 错误 1
make[1]: 离开目录“/home/firefly/Downloads/VTK-8.1.2/build/CMakeFiles/CMakeTmp”
make: *** [Makefile:121:cmTC_e950c/fast] 错误 2
后来网上查了以下,看这好像是python2的错误,我就想是不是python版本问题。就安装了python-is-python3,解决问题
sudo apt install python-is-python3
cmake命令:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D VTK_FORBID_DOWNLOADS=ON \
-D VTK_WRAP_PYTHON=ON \
-D VTK_PYTHON_OPTIONAL_LINK=ON \
-D PYTHON3_EXECRTABLE=/usr/bin/python3.8 \
-D PYTHON3_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.8.so \
-D BUILD_TESTING=OFF # 默认OFF,如果打开的话,编译时会由于下载测试数据所用url过旧而报错,建议OFF
-D VTK_FORBID_DOWNLOADS=ON # 默认OFF,建议打开,否则编译会报错,理由同BUILD_TESTING
-D Python3_INCLUDE_DIR=/usr/include/python3.8 ..
有的说需要依赖包,不确定是不是必须,先写下了备用
sudo apt-get install cmake-curses-gui
sudo apt-get install freeglut3-dev
剩下就是make 和install了
make -j8
sudo make install
之前cmake过了,但是make不过,报错
cannot convert ‘std::nullptr_t’ to ‘Py_ssize_t’ {aka ‘long int’} in initialization
解决办法:官方补丁:patch内容如下:
From 00e84cfcc5a0812503b431238fc26c349b0d714f Mon Sep 17 00:00:00 2001
From: David Gobbi <david.gobbi@gmail.com>
Date: Tue, 20 Aug 2019 17:02:24 -0600
Subject: [PATCH] Compatibility for Python 3.8
The PyTypeObject struct was modified in Python 3.8, this change is
required to avoid compile errors.
---
Utilities/PythonInterpreter/vtkPythonStdStreamCaptureHelper.h | 6 ++++++
Wrapping/PythonCore/PyVTKMethodDescriptor.cxx | 2 +-
Wrapping/PythonCore/PyVTKNamespace.cxx | 2 +-
Wrapping/PythonCore/PyVTKReference.cxx | 8 ++++----
Wrapping/PythonCore/PyVTKTemplate.cxx | 2 +-
Wrapping/PythonCore/vtkPythonCompatibility.h | 8 +++++++-
Wrapping/Tools/vtkWrapPythonClass.c | 2 +-
Wrapping/Tools/vtkWrapPythonEnum.c | 2 +-
Wrapping/Tools/vtkWrapPythonType.c | 2 +-
9 files changed, 23 insertions(+), 11 deletions(-)
diff --git a/Utilities/PythonInterpreter/vtkPythonStdStreamCaptureHelper.h b/Utilities/PythonInterpreter/vtkPythonStdStreamCaptureHelper.h
index b1c12c8..14ccfbe 100644
--- a/Utilities/PythonInterpreter/vtkPythonStdStreamCaptureHelper.h
+++ b/Utilities/PythonInterpreter/vtkPythonStdStreamCaptureHelper.h
@@ -140,6 +140,12 @@ static PyTypeObject vtkPythonStdStreamCaptureHelperType = {
#if PY_VERSION_HEX >= 0x03040000
0, // tp_finalize
#endif
+#if PY_VERSION_HEX >= 0x03080000
+ 0, // tp_vectorcall
+#if PY_VERSION_HEX < 0x03090000
+ 0, // tp_print
+#endif
+#endif
};
static PyObject* vtkWrite(PyObject* self, PyObject* args)
diff --git a/Wrapping/PythonCore/PyVTKMethodDescriptor.cxx b/Wrapping/PythonCore/PyVTKMethodDescriptor.cxx
index 2b0d443..3840038 100644
--- a/Wrapping/PythonCore/PyVTKMethodDescriptor.cxx
+++ b/Wrapping/PythonCore/PyVTKMethodDescriptor.cxx
@@ -186,7 +186,7 @@ PyTypeObject PyVTKMethodDescriptor_Type = {
sizeof(PyMethodDescrObject), // tp_basicsize
0, // tp_itemsize
PyVTKMethodDescriptor_Delete, // tp_dealloc
- nullptr, // tp_print
+ 0, // tp_vectorcall_offset
nullptr, // tp_getattr
nullptr, // tp_setattr
nullptr, // tp_compare
diff --git a/Wrapping/PythonCore/PyVTKNamespace.cxx b/Wrapping/PythonCore/PyVTKNamespace.cxx
index 71ee2a3..5cf5bfb 100644
--- a/Wrapping/PythonCore/PyVTKNamespace.cxx
+++ b/Wrapping/PythonCore/PyVTKNamespace.cxx
@@ -49,7 +49,7 @@ PyTypeObject PyVTKNamespace_Type = {
0, // tp_basicsize
0, // tp_itemsize
PyVTKNamespace_Delete, // tp_dealloc
- nullptr, // tp_print
+ 0, // tp_vectorcall_offset
nullptr, // tp_getattr
nullptr, // tp_setattr
nullptr, // tp_compare
diff --git a/Wrapping/PythonCore/PyVTKReference.cxx b/Wrapping/PythonCore/PyVTKReference.cxx
index 943ac71..b710409 100644
--- a/Wrapping/PythonCore/PyVTKReference.cxx
+++ b/Wrapping/PythonCore/PyVTKReference.cxx
@@ -1010,7 +1010,7 @@ PyTypeObject PyVTKReference_Type = {
sizeof(PyVTKReference), // tp_basicsize
0, // tp_itemsize
PyVTKReference_Delete, // tp_dealloc
- nullptr, // tp_print
+ 0, // tp_vectorcall_offset
nullptr, // tp_getattr
nullptr, // tp_setattr
nullptr, // tp_compare
@@ -1067,7 +1067,7 @@ PyTypeObject PyVTKNumberReference_Type = {
sizeof(PyVTKReference), // tp_basicsize
0, // tp_itemsize
PyVTKReference_Delete, // tp_dealloc
- nullptr, // tp_print
+ 0, // tp_vectorcall_offset
nullptr, // tp_getattr
nullptr, // tp_setattr
nullptr, // tp_compare
@@ -1124,7 +1124,7 @@ PyTypeObject PyVTKStringReference_Type = {
sizeof(PyVTKReference), // tp_basicsize
0, // tp_itemsize
PyVTKReference_Delete, // tp_dealloc
- nullptr, // tp_print
+ 0, // tp_vectorcall_offset
nullptr, // tp_getattr
nullptr, // tp_setattr
nullptr, // tp_compare
@@ -1181,7 +1181,7 @@ PyTypeObject PyVTKTupleReference_Type = {
sizeof(PyVTKReference), // tp_basicsize
0, // tp_itemsize
PyVTKReference_Delete, // tp_dealloc
- nullptr, // tp_print
+ 0, // tp_vectorcall_offset
nullptr, // tp_getattr
nullptr, // tp_setattr
nullptr, // tp_compare
diff --git a/Wrapping/PythonCore/PyVTKTemplate.cxx b/Wrapping/PythonCore/PyVTKTemplate.cxx
index be20098..340fe79 100644
--- a/Wrapping/PythonCore/PyVTKTemplate.cxx
+++ b/Wrapping/PythonCore/PyVTKTemplate.cxx
@@ -268,7 +268,7 @@ PyTypeObject PyVTKTemplate_Type = {
0, // tp_basicsize
0, // tp_itemsize
nullptr, // tp_dealloc
- nullptr, // tp_print
+ 0, // tp_vectorcall_offset
nullptr, // tp_getattr
nullptr, // tp_setattr
nullptr, // tp_compare
diff --git a/Wrapping/PythonCore/vtkPythonCompatibility.h b/Wrapping/PythonCore/vtkPythonCompatibility.h
index 4a76784..be208fa 100644
--- a/Wrapping/PythonCore/vtkPythonCompatibility.h
+++ b/Wrapping/PythonCore/vtkPythonCompatibility.h
@@ -64,7 +64,13 @@
#endif
// PyTypeObject compatibility
-#if PY_VERSION_HEX >= 0x03040000
+#if PY_VERSION_HEX >= 0x03090000
+#define VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED \
+ 0, 0, 0, 0,
+#elif PY_VERSION_HEX >= 0x03080000
+#define VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED \
+ 0, 0, 0, 0, 0,
+#elif PY_VERSION_HEX >= 0x03040000
#define VTK_WRAP_PYTHON_SUPPRESS_UNINITIALIZED \
0, 0, 0,
#else
diff --git a/Wrapping/Tools/vtkWrapPythonClass.c b/Wrapping/Tools/vtkWrapPythonClass.c
index b1e45f8..4d558ea 100644
--- a/Wrapping/Tools/vtkWrapPythonClass.c
+++ b/Wrapping/Tools/vtkWrapPythonClass.c
@@ -521,7 +521,7 @@ void vtkWrapPython_GenerateObjectType(
" sizeof(PyVTKObject), // tp_basicsize\n"
" 0, // tp_itemsize\n"
" PyVTKObject_Delete, // tp_dealloc\n"
- " nullptr, // tp_print\n"
+ " 0, // tp_vectorcall_offset\n"
" nullptr, // tp_getattr\n"
" nullptr, // tp_setattr\n"
" nullptr, // tp_compare\n"
diff --git a/Wrapping/Tools/vtkWrapPythonEnum.c b/Wrapping/Tools/vtkWrapPythonEnum.c
index b933702..1249362 100644
--- a/Wrapping/Tools/vtkWrapPythonEnum.c
+++ b/Wrapping/Tools/vtkWrapPythonEnum.c
@@ -145,7 +145,7 @@ void vtkWrapPython_GenerateEnumType(
" sizeof(PyIntObject), // tp_basicsize\n"
" 0, // tp_itemsize\n"
" nullptr, // tp_dealloc\n"
- " nullptr, // tp_print\n"
+ " 0, // tp_vectorcall_offset\n"
" nullptr, // tp_getattr\n"
" nullptr, // tp_setattr\n"
" nullptr, // tp_compare\n"
diff --git a/Wrapping/Tools/vtkWrapPythonType.c b/Wrapping/Tools/vtkWrapPythonType.c
index 744cb1b..0a1375e 100644
--- a/Wrapping/Tools/vtkWrapPythonType.c
+++ b/Wrapping/Tools/vtkWrapPythonType.c
@@ -709,7 +709,7 @@ void vtkWrapPython_GenerateSpecialType(
" sizeof(PyVTKSpecialObject), // tp_basicsize\n"
" 0, // tp_itemsize\n"
" Py%s_Delete, // tp_dealloc\n"
- " nullptr, // tp_print\n"
+ " 0, // tp_vectorcall_offset\n"
" nullptr, // tp_getattr\n"
" nullptr, // tp_setattr\n"
" nullptr, // tp_compare\n"
--
2.10.1 (Apple Git-78)
执行以下命令:patch –p1 < vtk.patch # 打补丁,使得成为新版本文件
配置环境变量:
在 Ubuntu 中,系统库路径通常被添加到默认的共享库路径中,因此你不需要手动配置环境变量。但是,如果你使用了自定义的库目录,你需要将该目录添加到 LD_LIBRARY_PATH
环境变量中,以便系统可以找到 VTK 库。
另外,如果你使用 Python3,你还需要将 PYTHONPATH
环境变量设置为 VTK Python 模块的路径,以便 Python 解释器可以找到 VTK 模块。
为了方便起见,你可以将这些环境变量添加到你的 .bashrc
文件中:
export LD_LIBRARY_PATH=/path/to/vtk/lib:$LD_LIBRARY_PATH
export PYTHONPATH=/path/to/vtk/lib/python3.x/site-packages:$PYTHONPATH
my Address
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
export PYTHONPATH=/usr/local/lib/python3.8/site-packages:$PYTHONPATH
其中 /path/to/vtk
是 VTK 库的安装路径,python3.x
是你使用的 Python3 版本号
添加python支持
你可以通过在终端中输入来查看PYTHONPATH中存储了哪些路径:
echo $PYTHONPATH
(在我的安装中,它默认是空的。)
然后您可以将 vtk 文件夹位置添加到终端中的 PYTHONPATH:
export PYTHONPATH=$PYTHONPATH:/usr/local/vtk8.1/lib/python3.8/site-packages
检查vtk是否可用:
$ python3
>>> import vtk
如果有效,您可以将上面的 export...
行添加到您的 ~/.bashrc
或 ~/.profile
(根据您的发行版安装)永久加载 PYTHONPATH 中的选项。
参考链接:https://blog.csdn.net/stareclipse/article/details/129326264
人工智能回答:
在Ubuntu下编译安装VTK并支持Python,可以按照以下步骤进行:
- 首先,安装必要的依赖库:
sudo apt-get update
sudo apt-get install build-essential cmake python-dev python-numpy libtiff-dev libjpeg-dev zlib1g-dev libpng-dev libxt-dev libgl1-mesa-dev libglu1-mesa-dev libssl-dev libexpat1-dev libxml2-dev
- 下载VTK源代码:
可以从官网下载最新的VTK源代码,也可以使用以下命令下载VTK 9.0版本:
wget https://vtk.org/files/release/9.0/VTK-9.0.1.tar.gz
tar -zxvf VTK-9.0.1.tar.gz
- 创建一个build目录,进入该目录,并使用cmake进行配置:
mkdir VTK-9.0.1/build
cd VTK-9.0.1/build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D VTK_FORBID_DOWNLOADS=ON -D VTK_WRAP_PYTHON=ON -D VTK_PYTHON_OPTIONAL_LINK=ON -D PYTHON3_EXECRTABLE=$(which python3) -D PYTHON3_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.8.so -D BUILD_TESTING=OFF -D VTK_FORBID_DOWNLOADS=ON -D Python3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..
其中,-DVTK_WRAP_PYTHON=ON指定编译VTK时支持Python,
-DPYTHON_EXECUTABLE指定Python可执行文件的路径,
-DPYTHON_INCLUDE_DIR指定Python头文件的路径,
-DPYTHON_LIBRARY指定Python库文件的路径,这些路径可能因Python版本和Linux发行版而异,需要根据实际情况进行修改。
- 使用make命令进行编译:
make -j4
其中,-j4指定使用4个核心进行编译,可以根据机器的实际情况进行修改。
- 安装VTK:
sudo make install
- 配置LD_LIBRARY_PATH环境变量:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
- 测试Python绑定:
python3
>>> import vtk
如果没有报错,说明VTK已经成功编译并支持Python了。