项目中使用到VTK,编译后虽然能够运行,但是vtkOutputWindow弹出,有如下错误信息:
Generic Warning: In D:\vtk-v8.0.0\Rendering\Core\vtkPolyDataMapper.cxx, line 28
Error: no override found for 'vtkPolyDataMapper'.
Warning: In D:\vtk-v8.0.0\Rendering\Core\vtkInteractorStyleSwitchBase.cxx, line 43
vtkInteractorStyleSwitchBase (000002B2C1D6F2D0): Warning: Link to vtkInteractionStyle for default style selection.
Generic Warning: In D:\vtk-v8.0.0\Rendering\Core\vtkPolyDataMapper.cxx, line 28
Error: no override found for 'vtkPolyDataMapper'.
这是由于编译过程中缺少一些定义导致的,如果采用CMake构建,默认是设置好的,如果也存在类似问题,那么就需要再CMakeLists.txt中添加include(${VTK_USE_FILE})。
如果采用VS或者Qt,就需要手动把下面代码放在需要使用VTK的所有代码之前(包括所有可能的预处理):
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include的路径根据工程的路径设置修改,如果你的VTK采用OpenGL后端就把vtkRenderingOpenGL2替换成vtkRenderingOpenGL即可。
也可以把两行define变成一行:
#define vtkRenderingCore_AUTOINIT 2(vtkRenderingOpenGL2, vtkInteractionStyle)
背后原理是什么,可以看如下官方描述https://vtk.org/doc/nightly/html/vtkAutoInit_8h_source.html
// Description:
// Initialize the named module, ensuring its object factory is correctly
// registered. This call must be made in global scope in the
// translation unit of your executable (which can include a shared library, but
// will not work as expected in a static library).
//
// @code{.cpp}
// #include "vtkAutoInit.h"
// VTK_MODULE_INIT(vtkRenderingOpenGL2);
// @endcode
//
// The above snippet if included in the global scope will ensure the object
// factories for vtkRenderingOpenGL2 are correctly registered and unregistered.
类似问题如何查找对应模块呢?以本文为例,在VTK文件夹中搜索字符串"vtkPolyDataMapper"
(含引号),在搜索结果中查找*ObjectFactory,如下:
Rendering/OpenGL2/vtkRenderingOpenGL2ObjectFactory.cxx:
this->RegisterOverride("vtkPolyDataMapper", "vtkOpenGLPolyDataMapper", "Override for vtkRenderingOpenGL2 module", 1, vtkObjectFactoryCreatevtkOpenGLPolyDataMapper);
就可以看出来是哪个module需要注册了。
Reference:
https://stackoverflow.com/questions/18642155/no-override-found-for-vtkpolydatamapper