vscode failed to build for one example but could build another one successfully

Hello, I am using MSVC as code generator and trying to build examples using vscode. I have chosen two official examples, one is DisplacementPlot and the other is Hello

The DisplacementPlot example could be built and ran without errors, however, as for the Hello example, When I configured it, it pops out many warnings like this:

......errors...
[cmake] 
[cmake]   IMPORTED_IMPLIB not set for imported target "VTK::RenderingGL2PSOpenGL2"
[cmake]   configuration "RelWithDebInfo".
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.
[cmake] 
[cmake] CMake Warning (dev) in CMakeLists.txt:
[cmake]   Policy CMP0111 is not set: An imported target missing its location property
[cmake]   fails during generation.  Run "cmake --help-policy CMP0111" for policy
[cmake]   details.  Use the cmake_policy command to set the policy and suppress this
[cmake]   warning.
[cmake] 
[cmake]   IMPORTED_IMPLIB not set for imported target "VTK::RenderingOpenGL2"
[cmake]   configuration "RelWithDebInfo".
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.
[cmake] 
[cmake] -- Generating done
[cmake] -- Build files have been written to: D:/Study/VTK/Tutorial-MinGW/Hello/build

and when I build it, it reported errors:

...errors...
[build] 
[build] LINK : warning LNK4044: 无法识别的选项“/lgdi32”;已忽略 [D:\Study\VTK\Tutorial-MinGW\Hello\build\Hello.vcxproj]
[build] Hello.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __cdecl vtkDebugLeaksManager::vtkDebugLeaksManager(void)" (__imp_??0vtkDebugLeaksManager@@QEAA@XZ),函数 "void __cdecl `dynamic initializer for 'vtkDebugLeaksManagerInstance''(void)" (??__EvtkDebugLeaksManagerInstance@@YAXXZ) 中引用了该符号 [D:\Study\VTK\Tutorial-MinGW\Hello\build\Hello.vcxproj]
[build] Hello.obj : error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __cdecl vtkDebugLeaksManager::~vtkDebugLeaksManager(void)" (__imp_??1vtkDebugLeaksManager@@QEAA@XZ),函数 "void __cdecl `dynamic atexit destructor for 'vtkDebugLeaksManagerInstance''(void)" (??__FvtkDebugLeaksManagerInstance@@YAXXZ) 中引用了该符号 [D:\Study\VTK\Tutorial-MinGW\Hello\build\Hello.vcxproj]
[build] Hello.obj : error LNK2019: 无法解析的外部符号 "void __cdecl vtkInteractionStyle_AutoInit_Construct(void)" (?vtkInteractionStyle_AutoInit_Construct@@YAXXZ),函数 "public: __cdecl `anonymous namespace'::vtkRenderingCore_AutoInit::vtkRenderingCore_AutoInit(void)" (??0vtkRenderingCore_AutoInit@?A0xc79f0987@@QEAA@XZ) 中引用了该符号 [D:\Study\VTK\Tutorial-MinGW\Hello\build\Hello.vcxproj]
[D:\Study\VTK\Tutorial-MinGW\Hello\build\Hello.vcxproj]
[proc] The command: "D:\Program Files\CMake\bin\cmake.EXE" --build d:/Study/VTK/Tutorial-MinGW/Hello/build --config Debug --target ALL_BUILD -j 18 -- exited with code: 1
[driver] Build completed: 00:00:01.093
[build] Build finished with exit code 1

I am confused about this because both the two examples are grabbed from official examples and should both work under the same condition.

BTW the CMakeLists.txt of the two are almost the same:

CMakeLists.txt for DisplacementPlot:

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(DisplacementPlot)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersCore
  FiltersGeneral
  IOLegacy
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "DisplacementPlot: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(DisplacementPlot MACOSX_BUNDLE DisplacementPlot.cxx )
  target_link_libraries(DisplacementPlot PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS DisplacementPlot
  MODULES ${VTK_LIBRARIES}
)

CMakeLists.txt for Hello:

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(Hello)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersCore
  FiltersHybrid
  IOLegacy
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "Hello: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(Hello MACOSX_BUNDLE Hello.cxx )
  target_link_libraries(Hello PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS Hello
  MODULES ${VTK_LIBRARIES}
)

It looks like VTK is built one way (Debug perhaps?) and your project is RelWithDebInfo. These are incompatible due to MSVC runtime things. Both projects need to use compatible configurations (Debug is standalone and Release/RelWithDebInfo are compatible).

Hi, I tried to delete all build files and configurations and regenerate the project. I tried this 5 or 6 times and I could run it the last time. I don’t know why but it could run now

For future reference, it helps to take meticulous notes when doing these kinds of debugging routines.

1 Like

I have the same problem like this, I am using VTK 9.2 and Qt 5.13, I built BarChartQt example with cmake, and the configuration and generating were ok with no errors, but when I open the .sln project and set up as a startup project (the BarChartQt) in release mode, I have this error: (also I checked the lib and dll files and this GUISupportQt file, was existed)