Install and use VTK on centos 4 and ubuntu 10

I have experienced some frustration of using VTK libraries on linux machine (or virtual machine) to compile mesh2vtk program, a utility program included in the package of SPECFEM3D. Some C++ programs in mesh2vtk package utilized VTK library. Therefore to compile and run mesh2vtk program successfully, VTK library must be installed first. To install VTK is straightforward, but  compiling mesh2vtk programs is no fun to me, since the compiler kept yelling me with tons of errors, such as

“g++ -I/home/junwei/VTK/include -I/home/junwei/VTK/include/vtk -Wno-deprecated -o mesh2vtu mesh2vtu.o -L/home/junwei/VTK/lib/vtk -lvtkCommon -lvtkIO -lvtkRendering -lvtkFiltering -lvtkGraphics
/usr/bin/ld: warning: libvtksys.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkCommon.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkDICOMParser.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkIO.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkNetCDF.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkIO.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkNetCDF_cxx.so, needed by /home/junwei/VTK/lib/vtk/libvtkIO.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkmetaio.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkIO.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtksqlite.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkIO.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkpng.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkIO.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkzlib.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkIO.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkjpeg.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkIO.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtktiff.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkIO.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkexpat.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkIO.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkImaging.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkRendering.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkftgl.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkRendering.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkfreetype.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkRendering.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libvtkverdict.so.5.9, needed by /home/junwei/VTK/lib/vtk/libvtkGraphics.so, not found (try using -rpath or -rpath-link)
/home/junwei/VTK/lib/vtk/libvtkIO.so: undefined reference to `vtk_netcdf_nc_get_vara_float’
/home/junwei/VTK/lib/vtk/libvtkGraphics.so: undefined reference to `vtk_v_hex_diagonal’
/home/junwei/VTK/lib/vtk/libvtkGraphics.so: undefined reference to `vtk_v_tet_radius_ratio’
/home/junwei/VTK/lib/vtk/libvtkIO.so: undefined reference to `vtk_png_write_end’
/home/junwei/VTK/lib/vtk/libvtkIO.so: undefined reference to `NcFile::num_vars() const’
/home/junwei/VTK/lib/vtk/libvtkRendering.so: undefined reference to `vtkImageResliceToColors::New()’
/home/junwei/VTK/lib/vtk/libvtkIO.so: undefined reference to `vtk_jpeg_start_decompress’
/home/junwei/VTK/lib/vtk/libvtkGraphics.so: undefined reference to `vtk_v_tri_aspect_frobenius’
/home/junwei/VTK/lib/vtk/libvtkRendering.so: undefined reference to `vtk_freetype_FTC_Manager_LookupSize’
/home/junwei/VTK/lib/vtk/libvtkIO.so: undefined reference to `vtk_netcdf_nc_put_vara_short’
/home/junwei/VTK/lib/vtk/libvtkIO.so: undefined reference to `vtk_TIFFSetField’
/home/junwei/VTK/lib/vtk/libvtkIO.so: undefined reference to `NcFile::~NcFile()’
/home/junwei/VTK/lib/vtk/libvtkIO.so: undefined reference to `vtk_expat_XML_SetUserData’
/home/junwei/VTK/lib/vtk/libvtkGraphics.so: undefined reference to `vtk_v_tri_aspect_ratio’
/home/junwei/VTK/lib/vtk/libvtkRendering.so: undefined reference to `vtkImageClip::SetOutputWholeExtent(int, int, int, int, int, int)'”

After two days’ google-ing, the error may be rooted in way of installing VTK and can be avoided by changing the compiling flag in mesh2vtk. Here is the procedure that worked for me.

(1) $ git clone git://vtk.org/VTK.git VTK  If you have not git installed, search how to install git on your linux version. After that, you will have a VTK folder under your working directory.

(2) install Tcl/Tk from source into VTK folder, i.e., type “./configure –prefix=home/junwei/VTK” -> “make” -> “make install” without quotation marks in folders of tcl and tk. Tcl first and then tk.

(3) under /home/junwei, type: $mkdir VTK-build  $cd VTK-build   $ccmake ../VTK    Note: ccmake must be installed. If not, search for it.

(4) Make sure the following properties were set:
BUILD_SHARED_LIBS: ON
CMAKE_INSTALL_PREFIX: /home/junwei/VTK
TCL_INCLUDE_PATH: /home/junwei/VTK/include
TCL_LIBRARY: /home/junwei/VTK/lib/libtcl8.5.so
TCL_TCLSH: /home/junwei/VTK/bin/tclsh8.5
TK_INCLUDE_PATH: /home/junwei/VTK/include
TK_LIBRARY: /home/junwei/VTK/lib/libtk8.5
VTK_OPENGL_HAS_OSMESA: OFF
VTK_TCL_TK_STATIC: OFF
VTK_USE_TK: ON
VTK_USE_64BIT_IDS: ON
VTK_USE_MPI: ON

(5) Press “c” a few times till you see “g” appears, meaning you can now generate the Makefile. Press “g” to generate.

(6)$make -> $make install   Now you have VTK compiled and installed under /home/junwei/VTK under bin, lib/vtk5.6, include/vtk5.6.Create symbolic link of vtk under lib to vtk5.6 and under include to vtk5.6, respectively.

(7) add vtk.conf to /ect/ld.so.conf.d with contents: /home/junwei/VTK/lib
/home/junwei/VTK/lib/vtk. If you can’t, remember “$export LD_LIBRARY_PATH=/home/junwei/VTK/lib/vtk” before running any compiled VTK related programs.

(8) Now to compile mesh2vtk programs. In the Makefile, change the following for ubuntu 10:
VTK=/home/junwei/VTK
LIBS=-L$(VTK)/lib/vtk -lvtksys -lvtkCommon -lvtkIO -lvtkDICOMParser -lvtkNetCDF -lvtkNetCDF_cxx -lvtkmetaio -lvtksqlite -lvtkpng -lvtkzlib -lvtkjpeg -lvtktiff -lvtkexpat -lvtkImaging -lvtkftgl -lvtkfreetype -lvtkverdict -lvtkRendering -lvtkFiltering -lvtkGraphics

or the following for centos 4
VTK=/home/junwei/VTK
LIBS=-L$(VTK)/lib/vtk  -lvtkRendering -lvtkFiltering -lvtkGraphics -lvtkIO -lvtkzlib -lvtkCommon  -lvtksys -lpthread -ldl

(8) under mesh2vtk folder, just $make all.

(9) $export LD_LIBRARY_PATH=/home/junwei/VTK/lib/vtk -> $./mesh2vtk shows the usage of this program, meaning that you can start using mesh2vtk now.