Tuesday, February 23, 2010

Compiling VTK for Visual Studio 2008

Tried like mad to compile for VS 2008. Followed this link. Works like magic!

http://www.cs.uiowa.edu/~cwyman/classes/common/howto/winVTK.html


Compiling VTK Using CMake and Visual Studio
Preliminaries:

So you've got the CD/DVD from the back of the book "The Visualization Toolkit" or perhaps you've downloaded VTK directly from the website, but now you've got problems compiling it on your home Windows machine.

Fortunately, if you have the textbook compilation is relatively straightforward.

* Please note, the installer on the CD only installs a binary version of VTK useful only as a Tcl interpreter using the VTK Tcl bindings.
* However, the Windows installer also allows you to install an executable version of CMake, which is required to build the version of VTK necessary for class.
* You can skip the CD's installer entirely, but you will need to download and install CMake yourself. And if you plan to use Tcl, you'll need to download that as well.

Using CMake to Setup a VTK Visual Studio Project:

1. Copy the directory from the CD "VTK\Source\VTK\" to your hard drive.
* I put mine in "C:\VTK\Source\VTK\".
* Note: This directory name should have no spaces or other weird characters in it.
* You may also want to copy "C:\VTK\Source\VTKData\" if you plan to use any example code.
2. Open up CMake. You can do this using either a command line or a nice Windows GUI.
* Open the GUI by running "CMakeSetup.exe" -- for me this is in the directory "C:\Program Files\CMake 2.4\bin\"
3. In the box labeled "Where is the source code:" enter the directory containing the VTK source
* Again, for me this was "C:\VTK\Source\VTK\"
4. In the box labeled "Where to build the binaries:" enter some temporary directory.
* I used "C:\VTK\VTK-win\"
* Again, this directory should have no spaces or other weird characters.
5. Once the values have the directories set appropriately, click on the button labeled "Configure."
* A dialogue will pop up asking for the system you plan to use to compile VTK.
* Select the correct version of Visual Studio.
* In my version of CMake, there was no entry for Visual Studio 2008, so pick the entry for Visual Studio 2005 (which VS 2008 can understand). I expect if you download the latest version of CMake, it will work out-of-the-box with Visual Studio 2008.
* Click OK.
6. CMake will spend quite a bit of time detecting information about your system and configuring the Visual Studio project.
* Wait for a bit. Maybe 2-3 minutes
7. Eventually, configuration will be over and a number of red configuration variables will appear in the box entitled "Cache Values." You may change these, depending what you want to build. There are a few you really should change:
* I would encourage you to set "BUILD_EXAMPLES" to "ON"
* If you use the examples, you should set "VTK_DATA_ROOT" to be the directory where you copied the data from the CD's "VTK\Source\VTKData\" directory. CMake detected a really nasty-looking directory name for this, you may want to clean that up.
* You should set the "CMAKE_INSTALL_PREFIX" to be the directory you want to contain the headers (.h) and libraries (.lib) you will need to compile your own programs.
o This directory cannot contain spaces or other weird characters.
o NOTE: The default chosen probably will include spaces (i.e., "C:\Program Files\VTK"). Change this!
o Subdirectories entitiled "include" and "lib" will be automatically created.
o I set this to be "C:\VTK\"
8. Once the cached values have been set appropriately, again click on the button labeled "Configure."
* This will only take a moment.
9. Click the button labeled "OK" to finish generating the Visual Studio project.
* This will take maybe 10 seconds, then CMake will exit.

Compiling VTK with Visual Studio:

1. Go to the temporary directory chosen in CMake to build your binaries.
* Again, for me this was "C:\VTK\VTK-win\"
2. Open up the Visual Studio project "ALL_BUILD.vcproj"
* If you use Visual Studio 2008, it will ask you to convert to a VS 2008 project. Click "Next>" then "Next>" then "Finish".
3. At the top of Visual Studio UI, select a "Release" build instead of a "Debug" build (the default).
* Unless, of course, you plan to use the debugger inside internal VTK functions.
4. In the solution explorer, right click on "ALL_BUILD," select the entry in the menu to "Build."
* Wait while Visual Studio compiles.... This will take a little while.
5. The headers (.h) and libraries (.lib) are now compiled, but are located in all sorts of odd directories.
6. In the solution explorer, right click on "INSTALL," select the entry in the menu to "Build."
* All the headers and libraries will be copied to the directory specified in CMake as "CMAKE_INSTALL_PREFIX."
* The libraries will be in the "lib/" directory and the headers will be in the "include/vtk-5.0/" directory.
7. You are done compiling VTK!

Testing VTK to Double Check Things Worked:

1. Make sure there were no errors reported by Visual Studio when building or installing.
2. Check inside the build path in the directory "bin\Release\" for a number of executables, such as "VTKBenchMark.exe" that you can run to double check things work correctly. Please note most or all of the examples in this directory do not open interactive windows, but rather display a couple images and then quit.
* For me, these test executables are in "C:\VTK\VTK-win\bin\Release\"
3. Check the installed header directory "include\"
* Mine has 1,074 files
4. Check the installed library directory "lib\"
* Mine has 64 files

If you have problems compiling, you can try to use my compiled libraries and headers.
Using VTK in Your Code:

There are two ways to get your code to link to the VTK libraries.

One approach is to continue using CMake. To use CMake, you must create a CMakeList.txt file to be processed while generating a Visual Studio project for your program. To see what a CMakeList.txt looks like, you can take a look at a sample or search for other online CMake documentation.

To link to VTK in a more "usual" Visual Studio fashion, you can:

1. Add the include directory to the "Addional Include Directories".
* This entry is found in: Project Menu -> Properties, then under Configuration Properties -> C/C++ -> General.
* My include directory is "C:\VTK\include\vtk-5.0"
2. Add the library directory to the "Additional Library Directories"
* This entry is found in: Project Menu -> Properties, then under Configuration Properties -> Linker -> General.
* My library directory is "C:\VTK\lib"
3. Include all the VTK libraries (.lib) files you use in your program under "Additional Dependencies"
* This entry is found in: Project Menu -> Properties, then under Configuration Properties -> Linker -> Input.
* You'll have to either include all the VTK libraries here or guess which ones you need.
* A good place to start for the basic cone demo is to link to vtkCommon.lib, vtkFiltering.lib, vtkGraphics.lib, vtkRendering.lib, and vtksys.lib.
4. Link to the OpenGL library ("opengl32.lib") also under "Additional Dependencies".

Last Modified: Thursday, February 5, 2009
by Chris Wyman (cwyman@cs.uiowa.edu)

4 comments:

Unknown said...

Thanks, it works with Visual Studio 2010.

vuvie said...

Sure Edwin! Glad it works with VS2010!

Oleg said...

Thanks!

Oleg said...

One note, if you are using Visual Studio 12, choose in "Configure" Visual Studio 11 instead of Visual Studio 12, this will save your nerves... and time
http://vtk.1045678.n5.nabble.com/Cmake-vtk-td5722557.html