VTK_MODULE_INIT in 9.0.1 ?

In a C++ program that links to VTK, using VTK 8.2 the library was initialized it with

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingContextOpenGL2);
[etc]

Does that still work with 9.0.1? I’m getting compiler errors like

[…]/include/vtk-9.0/vtkAutoInit.h:88:3: error: expected identifier before ‘{’ token

If it matters, for historical reasons my program is not built with cmake.

Thanks,
Steve

I’m not completely certain, but I think the basic workings of autoinit are the same for VTK 9. But the dependencies might have been shuffled around a bit.

Before #include <vtkAutoInit.h>, you will need definitions like this:

#define vtkRenderingCore_AUTOINIT 1(vtkRenderingOpenGL2)

The documents for autoinit are here, they’re cmake-centric but they describe some of the inner workings.

Edit: it might be more like this:

#define vtkRenderingContext2D_AUTOINIT 1(vtkRenderingContextOpenGL2)
#define vtkRenderingCore_AUTOINIT 3(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingOpenGL2)
#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL2)

Things do compile down to VTK_MODULE_INIT, but the interface generated is the code shown above. I think you need to include a header from the module you want to initialize as well, but I’m not sure. I figured it out once to write the new module system’s autoinit support, but it’s way too complicated to stick for long.

I’m afraid I’m not getting much out of the module system documentation. Is there some way to find out which autoinit #defines I need, perhaps from a list of which vtk headers I’m including in my C++ code? Or is there a list of what the options are, so that I can experiment to see which ones eliminate the build errors?

Is it possible to download all of the C++ examples in bulk? I could build them and see what definitions cmake generates for the examples that use the parts of vtk that I need. Would that work?

Thanks,
Steve

Steve,

Not sure it will help you, but here’s the snippet from my Xcode-based project that uses VTK built by CMake:

	// VTK factory methods require "auto-initialization".
	// <http://www.vtk.org/Wiki/VTK/VTK_6_Migration/Factories_now_require_defines>
	#include "vtkAutoInit.h"
	#include "vtkRenderingOpenGLConfigure.h"
	VTK_MODULE_INIT(vtkInteractionStyle)
	VTK_MODULE_INIT(vtkRenderingFreeType)
	VTK_MODULE_INIT(vtkRenderingOpenGL2)

Did you try the #defines that I suggested? You might have missed them if you’re reading through email, since I edited my response via discourse rather than sending an additional reply.

There is a utility to determine the VTK module dependencies for a source file. You can run

python <vtk-source-directory>/Utilities/Maintenance/FindNeededModules.py \
 -j <vtk-build-directory>/modules.json -s <your source file>

Run on the VTK source file Filters/Modeling/Testing/Cxx/TestRotationalExtrusion.cxx, it produces

find_package(VTK
 COMPONENTS
    CommonCore
    FiltersCore
    FiltersModeling
    FiltersSources
    RenderingCore
    TestingCore
    TestingRendering
    # These modules are suggested since they implement an existing module.
    # Uncomment those you need.
    # FiltersParallel         # implements VTK::FiltersCore
    # FiltersParallelGeometry # implements VTK::FiltersCore
    # InteractionStyle        # implements VTK::RenderingCore
    # RenderingFreeType       # implements VTK::RenderingCore
    # RenderingOpenGL2        # implements VTK::RenderingCore
    # RenderingUI             # implements VTK::RenderingCore
)

While this generates a find_package call meant for use in CMake, you can extract the module names listed under COMPONENTS, prepend “vtk” onto them, and you’ve got at least the module dependencies.

Thanks, David. Adding those lines before vtkAutoInit.h didn’t change anything.

Cory – this looks like it’s sending me in the right direction. Do I need to use the results to construct the #defines that David suggested, somehow? Do the commented out lines give me choices of different implementations, where I’m supposed to pick just one?

I’m sorry if I’m being dim. I feel that there are fundamental topics here that I don’t understand, and haven’t found the right references for.

Yeah, it’s more focused on how to use its APIs rather than on how its APIs work internally.

Once you have the WhatModulesVTK.py output (uncommenting some of the implementation modules if you need them), you can write a small project:

cmake_minimum_required(VERSION 3.13)
project(vtktest C CXX)

find_package(VTK) # see WhatModulesVTK.py output
add_library(foo dummy.cxx)
target_link_libraries(foo) # link what VTK libraries you want
vtk_module_autoinit(TARGETS foo MODULES …) # same VTK libraries as above

When you generate this project, there’s a header named vtkModuleAutoInit_<md5hash>.h in the build tree. This is the set of defines you need.

Yes, that’s my understanding. I would include all the commented out lines to be safe.

It’s a complex system. Usually the advice is to use CMake for the build system as the module system is set up to work with find_package.

What happens when a project wants to use vtk but also links with a library that requires a different build system, other than cmake?

I appreciate all the help, but I still have a bunch of questions.

I think I’ve done what Ben suggested above, but I’m getting errors like

CMake Error at /home/langer/lib/cmake/vtk-9.0/vtkModule.cmake:1136 (message):
Failed to determine the real target for the `CommonCore` module.  Is a
`find_package` missing a required component?

My find_package call looks like this:

find_package(VTK
 COMPONENTS
    CommonCore
    CommonDataModel
    CommonExecutionModel
    CommonTransforms
    [and a bunch more]
)

Does the word “component” in the error message mean that something is missing from the COMPONENTS section of the find_package arguments, or is it being used in a non-technical sense to mean that some other arguments are missing?

CMakeError.log only contains errors regarding pthreads (?!), but none of the fixes for that that I found on-line made any difference. Is that something I need to be worried about?

Is foo in the example a literal foo or do I need to replace it with something? I have been assuming it’s a placeholder for something that doesn’t matter because I’m not actually building anything with this code.

The Ubuntu 18 system that I’m using only has cmake 3.10, so I changed the minimum version number. Will that matter? I got the same results on a newer Ubuntu with a newer cmake, > 3.13.

Thanks.

VTK provides its usage API as CMake code and a CMake API. Other build systems would need to figure out how to utilize VTK’s autoinit system on their own. It doesn’t change that often, so doing the “see what VTK’s CMake API does and copy it into a static header in my project” is probably sufficient.

As for the error you’re seeing, it looks like VTK::CommonCore is not a target at some location. Could you please provide the full callstack?

This is the screen output.

CMake Error at /home/langer/lib/cmake/vtk-9.0/vtkModule.cmake:1136 (message):
  Failed to determine the real target for the `CommonCore` module.  Is a
  `find_package` missing a required component?
Call Stack (most recent call first):
  /home/langer/lib/cmake/vtk-9.0/vtkModule.cmake:2865 (_vtk_module_real_target)
  CMakeLists.txt:43 (vtk_module_autoinit)

I’ve uploaded the logs.
Thanks!

CMakeError.log (2.6 KB) CMakeOutput.log (50.5 KB)

And here is the cmake file, in case I’ve completely misunderstood your suggestions!
CMakeLists.txt (1.7 KB)

Ah.you need to do:

vtk_module_autoinit(TARGETS foo MODULES VTK::CommonCore)

The components are the post-:: part of the targets; VTK is the package part.

This MR improves the error message in this case.

That was very helpful, in that it did generate a .h file containing the right sort of #defines, but when they’re inserted into my program before #include <vtkAutoInit.h> I still get the same errors.

This is in vtkutils.C

#define vtkIOExportGL2PS_AUTOINIT 1(vtkIOExportGL2PS)
#define vtkRenderingCore_AUTOINIT 4(vtkInteractionStyle,vtkRenderingFreeType,vtkRenderingOpenGL2,vtkRenderingUI)
#define vtkRenderingOpenGL2_AUTOINIT 1(vtkRenderingGL2PSOpenGL2)
#include <vtkAutoInit.h>

void initialize_vtk() {
  VTK_MODULE_INIT(CommonCore);
 [etc]
}

and this is the output when compiling vtkutils.C:

x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-gnDdqE/python2.7-2.7.17=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -g -UNDEBUG -DDEBUG -I/usr/include/python2.7 -I/home/langer/include/vtk-9.0 -Ibuild/temp.linux-x86_64-2.7-3d/SRC -ISRC -I/usr/include/python2.7 -I/home/langer/include/vtk-9.0 -c ./SRC/common/IO/vtkutils.C -o build/temp.linux-x86_64-2.7-3d/./SRC/common/IO/vtkutils.o -std=c++11
In file included from ./SRC/common/IO/vtkutils.C:60:0:
./SRC/common/IO/vtkutils.C: In function ‘void initialize_vtk()’:
/home/langer/include/vtk-9.0/vtkAutoInit.h:88:3: error: expected identifier before ‘{’ token
   {                                                                                                \
   ^
./SRC/common/IO/vtkutils.C:83:5: note: in expansion of macro ‘VTK_MODULE_INIT’
     VTK_MODULE_INIT(CommonCore);
     ^~~~~~~~~~~~~~~
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

Has the syntax for VTK_MODULE_INIT changed? I assume I need a VTK_MODULE_INIT line for each of the components that I got from FindNeededModules. I’ve tried prefixing “vtk” or “vtk::” to the module name but even if that’s what’s wrong it doesn’t change the results.

Thanks again.

I think you can just drop VTK_MODULE_INIT if you’re using the code that got generated. I don’t think VTK generates that macro at all (other than through the expansions in vtkAutoInit.h.