VTK C++ application and Visual Studio

Dear VTK Experts,

I have recently installed VTK for C++ with Visual Studio Community 2022.
I followed an online tutorial that uses CMake to prepare the files needed by Visual Studio.

I am trying to build my own C++ application that uses VTK.
I noticed that all the VTK C++ examples make use of CMake to build the executable.
I tried to build my application directly with Visual Studio. It failed systematically regardless of what I entered in the fields of the project properties, after adding the path to VTK bin, lib, and include folders to my user PATH environment variable.

Does any C++ application that uses VK have to be built by CMake?
Is it possible to build a C++ application, that uses VTLK libraries, directly with Visual Studio?

Thank you in advance for your attention


The answer is no, but it is not easy to see what you are doing wrong without an example. You may end up in a situation where it build but simply crashes. It could be due to some missing AUTO_INIT for the rendering.

No, you don’t have to. Did you compile your VTK from the source using the same VS that you were trying to run the examples? If the answer is yes, then how did it “systematically fail”? Link error? or it couldn’t find headers and whatnot? You didn’t provide any information. Remember if it is some linking error, you must provide the full path of your .libs to the linker, e.g. C:/Program Files/vtk/libs/libvtkCommonComputationalGeometry-8.2.lib.

Good luck

Thank you.

Can you please explain how to set the AUTO_INIT you mention?
Is it a CMake parameter?
Is it a variable I can set in CMake or Visual Studio?

At the time being I do not get any errors about the files.

I have built several VTK C++ geometric examples. I followed the procedure illustrated on https://gitlab.kitware.com/vtk/vtk/-/blob/master/Documentation/dev/build_windows_vs.md#test-with-an-example

Most of the time when I run the executable with Visual Studio I get a black window where the geometric object should be displayed. Rarely something is displayed
Sometimes I get a Load Library failed with error 87.

I would like to show more but I am not allowed to upload evidence of my work.

It would greatly help me if someone could provide the complete explicit procedure to build a VTK C++ geometric example with CMake and Visual Studio Community on Windows 10.

Best regards

I didn’t mention auto_init, but it is a header you can include in your main.cpp file. There are also some macros that you might want to use, I give you an example:

#include <vtkAutoInit.h>

VTK_MODULE_INIT(vtkRenderingOpenGL2) // VTK was built with vtkRenderingOpenGL2

int main(int argc, char *argv[]){
   // your code goes here


Regarding the CMAKE part, VTK has a lot of informative examples. Check this one for example https://examples.vtk.org/site/Cxx/IO/ReadOBJ/

Add a folder (name it build, for example) to your working directory, open your CMAKE GUI and address your source (working directory) and build folder, configure, generate and open it with VS. In your project property select the same c++ standard that you built vtk with, and build the code. CMAKE is very efficient in finding VTK headers and libraries, both developed by Kitware.

Thank you for the valuable suggestions.
For the moment I am experimenting with the provided C++ VTK examples.
I tried ReadOBJ as suggested.
When I open the tar archive I get the following in the ReadOBJ folder:
build (directory

I open CMake gui.
I set the “WHERE is the source code” field to folder ReadOBJ
I set the “Where to build the binaries” field to folder ReadOBJ.build
I click on
Open Project

Visual Studio pops up.
In the solution view menu I right-click on ReadOBJ and select Properties.
In the Properties menu I choose the C++ version.
I repeated this experiement 3 times. each time I chose a different C++ version (14 / 17 / 20) because I cannot remember which C++ version I used when I compiled and installed VTK.
In any case I get no error when I build the entire solution.
I right-click on ReadOBJ again and set it as the startup project.
In any case when a window pops up with the following message.
That has happened with most of the C++ VTK examples I tried

C:\Users\MMONVILL\Desktop\Test_VTK\ReadOBJ\build\Debug\ReadOBJ.exe (process 1432) exited with code 1.
Press any key to close this window . . .

Shall I reinstall VTK or CMake or Visual Studio Community 2022 or all of them?

Thank you again

No, as far as I can tell, this is a runtime error and you don’t need to reinstall your vtk or Cmake. Let’s try something simple without rendering stuff.

create a folder in your desktop (for example) and put this main.cpp in it:

#include <vtkCylinderSource.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkPolyData.h>

int main(int argc, char *argv[]){

   vtkNew<vtkCylinderSource> cylinder;
    std::cout<< cylinder->GetOutput()->GetNumberOfPoints()<<std::endl;
   return 0;

This should print out number 32 every time you run it. Now create your CMakeLists.txt file, and put it in the same folder as main.cpp.

cmake_minimum_required(VERSION 3.16)

set(SOURCE_FILES main.cpp)

find_package(VTK REQUIRED COMPONENTS vtkCommonColor

add_executable("${PROJECT_NAME}" "${SOURCE_FILES}")
target_link_libraries( "${VTK_LIBRARIES}")

create a build folder in the same directory as main.cpp and your CMakeLists.txt, Cmake it, then compile it and let me know if you get any errors.

Or alternatively, you can modify the ReadOBJ.cxx file, clear the build folder, and recompile everything. Since I’m still using the older version of vtk, the cmake txt file I provided might be wrong. Modify ReadOBJ.cxx as follow:

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOBJReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <string>

int main(int argc, char* argv[])
  // Parse command line arguments
  if (argc != 2)
    std::cout << "Usage: " << argv[0] << "Filename(.obj) e.g trumpet.obj"
              << std::endl;
    return EXIT_FAILURE;

  std::string filename = argv[1];
  vtkNew<vtkOBJReader> reader;
  std::cout<< reader->GetOutPut()->GetNumberOfPoints() <<std::endl;

  return EXIT_SUCCESS;

This will print the number of points in your .obj mesh, with no rendering. Let me know if it fails or not.

I have uploaded the screenshot showing CMake configuration errors

Other errors in CMake output:

Looking for pthread_create in pthreads - not found
Looking for pthread_create in pthread
Looking for pthread_create in pthread - not found
Found Threads: TRUE
CMake Error at CMakeLists.txt:23 (target_link_libraries):
Cannot specify link libraries for target
which is not built by this project.

Please take a look at the post that I followed [quote=“kaveh yousefpouran, post:8, topic:11936, username:apteroti”]
Since I’m still using the older version of vtk, the cmake txt file I provided might be wrong
[/quote]. My Cmake file is suitable for the older version. So in your ReadOBJ example folder, modify the ReadOBJ.cxx and recompile.

I modified ReadOBJ as instructed.
CMake configuration completed with no error

After Generate I open the project with Visual Studio.
I right-clicked on ReadOBJ in the solution view and set it as a startup project then I ran it by clicking the green arrow in Visual Studio and got a runtime error:

I carried on after the displayed error and got a more self-explanatory error message:

This is not a run time error btw, this was my miss-typing of a line of code, your code did Not compile! I’m sorry, please change the line printing the number of points as follows:

//std::cout<< reader->GetOutPut()->GetNumberOfPoints() <<std::endl; //GetOutPut is wrong, my mistake
std::cout<< reader->GetOutput()->GetNumberOfPoints() <<std::endl; //it should be GetOutput instead. 

Sorry for the delay. We had an internet outage in the afternoon.
I am now running remotely from home.

I corrected the typo as you indicated.
I ran CMake, configured, generated, and opened the project.
In Visual Studio I set ReadOBJ as the startup project and ran. The executable exits with code 1
that is the program cannot find an object

What am I supposed to do?
Thank you

This is good, the code works fine, and your vtk is compiled well. I believe your problem with running the vtk examples is not providing a path to a .obj file (or any external geometry). e.g. you have to run the exe file like this: ReadOBJ.exe C:/Desktop/some folders/myMesh.obj.

To confirm it, run this example, and check if it works (it doesn’t need a .obj file, it creates the geometry and renders it):


I tried the cylinder example. The behavior is the same as all the other examples I tried. There is no compilation and no building error but Visual Studio only produces a black panel. See attached picture.

Yesterday the IT technician has updated the driver of the graphic card to exclude any h/w cause.

I am at my wit’s end.


Thank you.
So far I have tried several C++ VTK examples.
My goal is to understand the correct way to successfully build an executable, through CMake and Visual Studio, that works.

I do not get any compilation or linking error.
At runtime Visual Studio shows a black panel, instead of showing the geometric shape I have picked. Sometimes the process exits with a big negative error and no panel is shown.

I reckon CMake and Visual Studio Community are not correctly integrated.
That’s why I cannot proceed with my own code.

I would be grateful to anyone who can provide a C++ VTK graphic example (C++ source code and CMakeLists.txt) that works. That is it should show something.


I would be grateful to anyone who can provide a C++ VTK graphic example (C++ source code and CMakeLists.txt) that works.

The last I checked there were thousands of tests that “work” :-): https://open.cdash.org/index.php?project=VTK

If you haven’t already, you may also want to try out the VTK examples: https://examples.vtk.org/site/. The examples have explanatory text that can be helpful.

I tried several C++ VTK examples. I use CMake and Visual Studio Community 2022.
No compilation or linking error.
At runtime Visual Studio shows a black panel instead of showing the selected geometric shape.
Sometimes no panel pops up as the process exits with a big negative error number.
I have contacted the MicroSoft group who implemented Visual Studio. They are not getting any better results. They are investigating.

Does anyone know which Visual Studio release and which CMake version were used to successfully build the C++ VTK geometric (involving rendering) examples?

Thank you