Build VTK9.3.0 Msys2/Mingw64 on Win10 - undefined reference to template

Similar to: V9.1.0 Build Error

Building V9.3.0 using msys2/mingw64 with cmake/ninja on Windows10 for a new project. Have used & built VTK in the past but several years ago.

I had to add the following to CMakeLists.txt:

Line 9: add_compile_options(-Os -O3 -fcommon)

The -Os and -O3 options were added to work around a “file too big” error during the compile phase.

The -fcommon option was added to work around “multiple definition” errors during the link phase because the approach used to define functions/globals in the header files is no longer supported in the latest compiler/linker default settings. -fcommon tells the link to fold identical definitions to a single instance.

Ideas? Thanks in advance for any assistance!

================================
$ g++ --version
g++.exe (Rev7, Built by MSYS2 project) 13.1.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
================================
$ ninja
[1/1] Linking CXX shared library bin\libvtkImagingMorphological-9.3d.dll
FAILED: bin/libvtkImagingMorphological-9.3d.dll lib/libvtkImagingMorphological-9.3d.dll.a
cmd.exe /C "cd . && C:\msys64\ucrt64\bin\c++.exe -Os -O3  -mwin32 -mthreads -g  -mthreads -shared -o bin\libvtkImagingMorphologic
al-9.3d.dll -Wl,--out-implib,lib\libvtkImagingMorphological-9.3d.dll.a -Wl,--major-image-version,9,--minor-image-version,3 Imagin
g/Morphological/CMakeFiles/ImagingMorphological.dir/vtkImageConnectivityFilter.cxx.obj Imaging/Morphological/CMakeFiles/ImagingMo
rphological.dir/vtkImageConnector.cxx.obj Imaging/Morphological/CMakeFiles/ImagingMorphological.dir/vtkImageContinuousDilate3D.cx
x.obj Imaging/Morphological/CMakeFiles/ImagingMorphological.dir/vtkImageContinuousErode3D.cxx.obj Imaging/Morphological/CMakeFile
s/ImagingMorphological.dir/vtkImageDilateErode3D.cxx.obj Imaging/Morphological/CMakeFiles/ImagingMorphological.dir/vtkImageIsland
Removal2D.cxx.obj Imaging/Morphological/CMakeFiles/ImagingMorphological.dir/vtkImageNonMaximumSuppression.cxx.obj Imaging/Morphol
ogical/CMakeFiles/ImagingMorphological.dir/vtkImageOpenClose3D.cxx.obj Imaging/Morphological/CMakeFiles/ImagingMorphological.dir/
vtkImageSeedConnectivity.cxx.obj Imaging/Morphological/CMakeFiles/ImagingMorphological.dir/vtkImageSkeleton2D.cxx.obj Imaging/Mor
phological/CMakeFiles/ImagingMorphological.dir/vtkImageThresholdConnectivity.cxx.obj  -lgdi32  lib/libvtkImagingGeneral-9.3d.dll.
a  lib/libvtkImagingSources-9.3d.dll.a  lib/libvtkImagingCore-9.3d.dll.a  lib/libvtkCommonExecutionModel-9.3d.dll.a  lib/libvtkCo
mmonDataModel-9.3d.dll.a  lib/libvtkCommonTransforms-9.3d.dll.a  lib/libvtkCommonMath-9.3d.dll.a  lib/libvtkCommonCore-9.3d.dll.a
  lib/libvtksys-9.3d.dll.a  -lws2_32  -lpsapi  lib/libvtkkissfft-9.3d.dll.a  -lgdi32  -lkernel32 -luser32 -lgdi32 -lwinspool -lsh
ell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: Imaging/Morphological/CMakeF
iles/ImagingMorphological.dir/vtkImageConnectivityFilter.cxx.obj: in function `ExecuteInput<short int>':
C:/Projects/VTK-9.3.0/Imaging/Morphological/vtkImageConnectivityFilter.cxx:481: undefined reference to `vtkImageStencilIterator<s
hort>::vtkImageStencilIterator(vtkImageData*, vtkImageStencilData*, int const*, vtkAlgorithm*, int)'
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: Imaging/Morphological/CMakeF
iles/ImagingMorphological.dir/vtkImageConnectivityFilter.cxx.obj: in function `ExecuteInput<short unsigned int>':
C:/Projects/VTK-9.3.0/Imaging/Morphological/vtkImageConnectivityFilter.cxx:481: undefined reference to `vtkImageStencilIterator<u
nsigned short>::vtkImageStencilIterator(vtkImageData*, vtkImageStencilData*, int const*, vtkAlgorithm*, int)'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
================================