This page describes how to build and install VTK using Visual Studio on Windows in recipe-style fashion. It is also possible to build VTK on Windows without using Visual Studio directly, this is covered in Building VTK.
Adapted from the Paraview build instructions and VTK wiki. Inspired by This video
For this guide you will need to following:
- CMake CMake version 3.10 or higher and a working compiler.
- Visual Studio with C++ support
- The VTK Source-code
If you have these then you can skip the rest of this section and proceed to BUILD SOLUTION.
CMake is a tool that makes cross-platform building simple. On several systems it will probably be already installed. If it is not, please use the following instructions to install it. There are several precompiled binaries available at the CMake download page. Download version 3.10 or later. Add CMake to your PATH environment variable if you downloaded an archive and not an installer.
This guide was tested using cmake 3.13.4 64bit installed by downloading the .msi installer. [cmake-3.13.4-win64-x64.msi]
This guide uses Visual Studio / C++ as IDE and compiler. Visual studio can be installed from Download. This howto uses the free community edition. During installation select the "desktop development with C++" workload.
Download VTK source for the version you want from https://vtk.org/download/ (zip or tar.gz (Do NOT download the exe - this is not the VTK library.) ) You will probably want the latest one (highest version number) unless you have a specific reason to use an older one.
Alternatively the source-code can be obtained from the repository as well. This is recommended only if you intent to make changes and contribute to VTK.
Use CMake to create a solution that visual studio can open.
- Create a folder for VTK.
- In this folder, create two subfolders; "src" and "build"
- Extract the contents of the VTK folder in the downloaded archive to the subfolder called src
You should now have something like:
c:\data\cpp\vtk\build <--empty
c:\data\cpp\vtk\src
c:\data\cpp\vtk\src\Accelerators
c:\data\cpp\vtk\src\Charts
c:\data\cpp\vtk\src\....
Use CMake to generate a visual studio solution.
-
Open CMake-GUI, either by typing cmake-gui on the command prompt or from the start-menu.
-
Enter the source and build directories
-
Click [Configure]
-
You will now get a selection screen in which you can specify your "generator". Select the one you need. This guide was tested with Visual Studio 15 2017 Win64 in combination with the default options.
This will take some time. You may want to spend this time useful by reading cmake overview and cmake example and even the cmake tutorial.
-
We are now presented with a few options that can be turned on or off as desired. For this guide the only change made is to
- Check the box after CMAKE_CXX_MP_FLAG. This enables building using multi-core.
-
Click [Configure] to apply the changes.
-
Click [Generate]. This will populate the "build" sub-folder.
-
Finally, click [Open Project] to open the generated solution in Visual Studio.
Use Visual Studio to build the .dll files. The last step of CMake will launch Visual Studio and will open the generated solution.
- Set the configuration to "Release"
- Build the "ALL_BUILD" project.
Again, this will take a while [About ten minutes on a 2-core i7].
After this there should be a folder /build/bin/Release containing the created .dll libraries.
To be able to use VTK in other project it first needs to be installed.
-
Start CMake-gui (again)
-
As source-code dir enter the src folder (again).
-
Hit [Configure]
-
Set the "CMKAE_INSTALL_PREFIX" directory.
-
Click [Generate]
-
Click [Open Project]
In Visual Studio
- Build the "ALL_BUILD" project again. Should be very quick this time.
- Build the "INSTALL" project.
At this moment Visual Studio may FAIL because it is not allowed to create the installation folder.
If this happens then you have two options:
- Either repeat the previous steps with a different install directory in CMAKE
- Start Visual Studio as administrator by right-clicking on its icon and selecting "start as administrator".
After installation where VTK should have been installed in the specified installation directory. Something like the following directories should now exist:
c:\program file\VTK\bin c:\program file\VTK\include c:\program file\VTK\lib c:\program file\VTK\share
The /bin folder contains all the .dll files that are needed to run an application using VTK. In order to be able to find these files it needs to be added to the path environment variable.
-
Add the folder /bin folder to the windows path. [start -> Edit the system environment variables -> Advanced -> Environment Variables -> Path -> Edit -> New]
If everything went well then it should now be possible to compile and run the one of the C++ examples.
From vtk-examples pick a simple but appealing one. In this guide we've used this one
-
Downloads or copy-paste the .cxx and CMakeLists.txt files and save them in the same folder.
-
Open CMake and select the folder where the files were saves as "where is the source code" folder.
-
Click [Configure]
-
Verify that the VTK_DIR is set correctly. This folder should contain the file UseVTK.cmake
-
Click [Configure]
-
Click [Generate]
If you get an error then make sure that the file-names specified in CMakeLists.txt match the source file. Visual studio used .cpp as extension for C++ files while the cmake files contain references to .cxx
-
Click [Open Project]
In visual studio:
- Select the example (HighlightPickedActor) as start-up project. (right click -> set as start-up project)
- Run!
If your program complains about missing DLLs then check if the .dll path (last step of INSTALL section) was added correctly.
- VTK 8.2.0
- CMake 3.13.4
- Visual Studio 2017 ; community edition
- x64
- Windows 10