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?
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.
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.
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
VTK_MODULE_INIT(vtkRenderingContextOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
int main(int argc, char *argv[]){
// your code goes here
}
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
CMakeList.txt
ReadOBJ.cxx
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
Configure
Generate
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?
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:
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:
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
“VTK::CommonColor;VTK::CommonCore;VTK::CommonDataModel;VTK::CommonMisc;VTK::IOImage;VTK::IOGeometry;VTK::FiltersCore;VTK::FiltersPoints;VTK::FiltersModeling;VTK::FiltersSources;VTK::FiltersVerdict;VTK::FiltersGeneral;VTK::CommonComputationalGeometry”
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.
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:
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
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.
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.
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?