VMTK编译时遇到的天坑

VMTK是一个很小众的包,因为要用一些itk contrib的模块,无法用python实现,只能自己编译VMTK跟ITK、VTK一起使用。

最开始想的太简单,VTK和ITK都用的最新版本vtk9和ITK5,后两个简简单单编译成功。到了VMTK这里,虽然cmakelist里有USE_SYSTEM_ITK和USE_SYSTEM_VTK的选项。

 

但。。想想也知道不可能成功,VTK从8到9疯狂的改了一堆东西。去VTK官方论坛逛,发现有个大佬花了将近2个月全职工作才把vmtk在vtk9.0下编译成功。我发了邮件,问愿不愿意开源出来,还没回我。估计是不愿意的。然后看他后面的帖子,就算强行编译成功了,后面也一堆问题,根本跑不起来(他问update函数是干啥的,为啥总报错,然而熟悉ITK、VTK的人都知道,update函数是itk、vtk库的pipeline虚函数,几乎所有类都有这个update)。

所以,为了工作还能继续开展,就用vmtk自带的环境吧。直接让它自己下载并自动编译itk和vtk(自带的itk版本是4.13.0, vtk是8.1.0)

然而,编译过程中遇到了错误:

cannot convert ‘std::nullptr_t’ to ‘Py_ssize_t’ {aka ‘long int’} in initialization

去VTK论坛发现了这个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)

不过我当时用3.8与另一个包产生了冲突,于是改换python3.7

顺说,这里千万不能用anaconda,否则无法用visual studio调试,anaconda到现在也不提供python的debug lib。只能用python官网的安装包安装。

 安装后,需要在cmake里指定python所在位置(如果只有python3.7可能不需要)

我在cmake指定的变量如下:

 之后就来到最后一个坑,python3.6更新到3.7后的代码改变

 

 这里,python3.6返回的是char * ,而python3.7返回的是const char * 

diff --git a/Wrapping/PythonCore/vtkPythonArgs.cxx b/Wrapping/PythonCore/vtkPythonArgs.cxx
index 1a82af08021fd21ecec3fd711859a1eacce1915c..b7334589750e871982c52694ffa2601e01c38e39 100644
--- a/Wrapping/PythonCore/vtkPythonArgs.cxx
+++ b/Wrapping/PythonCore/vtkPythonArgs.cxx
@@ -95,13 +95,21 @@ bool vtkPythonGetStringValue(PyObject *o, T *&a, const char *exctext)
 {
   if (PyBytes_Check(o))
   {
+#if PY_VERSION_HEX >= 0x03070000
+    a = const_cast<char *>(PyBytes_AS_STRING(o));
+    return true;
+#else
     a = PyBytes_AS_STRING(o);
     return true;
+#endif
   }
 #ifdef Py_USING_UNICODE
   else if (PyUnicode_Check(o))
   {
-#if PY_VERSION_HEX >= 0x03030000
+#if PY_VERSION_HEX >= 0x03070000
+    a = const_cast<char *>(PyUnicode_AsUTF8(o));
+    return true;
+#elif PY_VERSION_HEX >= 0x03030000
     a = PyUnicode_AsUTF8(o);
     return true;
 #else

改完这个,编译成功,折腾一周多。。。完结撒花

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值