[vtkusers] Error Compiling Sample Code

Obada Mahdi omahdi at gmx.de
Tue May 22 16:04:35 EDT 2007


Hi Ashivni!

Quick hint: Try using ``g++'' instead of ``gcc'' for compiling C++
programs, and also include dependent libraries on the command line (e.g.
"... -lvtkRendering -lvtkCommon ...").

> $ gcc -Wno-deprecated try.cpp

If the gcc/g++ frontends are used for also linking a program, there are
a number of implicit "rules" applied to the linking process, which are
dependent on how the frontend is invoked.  For example, if you call
``g++'', it will automatically take care to link the proper startup code
as well as a STL impementation to your program.  The default rules for
``gcc'' handle things suitable for ordinary C programs, which is why in
your case those symbols are missing.

As for the missing VTK symbols: all libraries (other than the standard
library, that is) must be given explicitly on the command line, probably along
with search paths (``-L<path>'').  On my system, it works with:

| $ gcc -Wno-deprecated -I/home/omahdi/local/include/vtk-5.1 \
|   try.cpp -L/home/omahdi/local/lib -lvtkRendering -lvtkCommon

I also had to add a
| #include "vtkProperty.h"
at the beginning of the example code, but that probably depends on the VTK
version (I am using the CVS HEAD).

There isn't exactly an automated way for figuring out which libraries to link
against, there is only one for automatically finding their additional
dependencies.  For example, if you do stuff with rendering, you'll need
"libvtkRendering".  If there's an image reader involved, you'd need
"libvtkIO", because that's where those are defined, and so on.  In practice,
educated guessing helps a lot, as does some grepping a la

| $ nm -A <INSTALL_PREFIX>/lib/"libvtk*.so" | grep "vtkSphereSource"

Most VTK libraries depend on "libvtkCommon"--all dependency information can
be found in a file called "VTKLibraryDepends.cmake", which is installed along
with other VTK stuff in "<INSTALL_PREFIX>/lib/vtk-<VTK_VERSION>".


With getting more familiar with VTK and this mailing list, you'll find that
the preferred way of building programs that use VTK is through a CMake build
script, just as what is used to build VTK itself.  This way, CMake will take
care of any peculiar compiler behavior and library dependencies, which makes
projects often easier to maintain, and also helps people on the mailing list
diagnose other build problems, should you ever run into more :-)

Especially for small programs, you do not need to know much about CMake to set
up your own build script.  In fact, "CMakeLists.txt" files that come with the
numerous C++ VTK examples (like "Examples/Tutorial/Step1/Cxx/CMakeLists.txt"
in the VTK source tree) are perfect starting points and often only need minor
modification.

Having said that, there's of course nothing wrong with directly invoking the
compiler/linker; there are just some more things and probably side-effects to
be taken into consideration.


HTH,

Obada

On Tue, 22 May 2007, Ashivni Shekhawat wrote:
> Hi!
>   I am new to vtk and to graphics and rendering.
> I have installed vkt 5.0.3. When I try to compile the example code for
> creating a sphere http://vtk.org/example-code.php I get the errors
> appended. While it is apparent that I am not linking to the required
> libraries, I could not find information on which libraries to link to
> and how. Any suggestions?
>
> Thanks in advance
> -Ashivni.
>
> $ gcc -Wno-deprecated try.cpp
> /tmp/ccSQdQ8V.o: In function
> `__static_initialization_and_destruction_0(int, int)':
> try.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
> /tmp/ccSQdQ8V.o: In function `__tcf_0':
> try.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
> /tmp/ccSQdQ8V.o: In function `main':
> try.cpp:(.text+0x84): undefined reference to `vtkSphereSource::New()'
> try.cpp:(.text+0xde): undefined reference to `vtkPolyDataMapper::New()'
> try.cpp:(.text+0xec): undefined reference to 
> `vtkPolyDataAlgorithm::GetOutput()'
> try.cpp:(.text+0xfb): undefined reference to
> `vtkPolyDataMapper::SetInput(vtkPolyData*)'
> try.cpp:(.text+0x100): undefined reference to `vtkActor::New()'
> try.cpp:(.text+0x129): undefined reference to `vtkActor::GetProperty()'
> try.cpp:(.text+0x143): undefined reference to
> `vtkProperty::SetColor(double, double, double)'
> try.cpp:(.text+0x148): undefined reference to `vtkRenderer::New()'
> try.cpp:(.text+0x150): undefined reference to `vtkRenderWindow::New()'
> try.cpp:(.text+0x173): undefined reference to 
> `vtkRenderWindowInteractor::New()'
> try.cpp:(.text+0x188): undefined reference to
> `vtkRenderWindowInteractor::SetRenderWindow(vtkRenderWindow*)'
> try.cpp:(.text+0x19a): undefined reference to 
> `vtkRenderer::AddActor(vtkProp*)'
> /tmp/ccSQdQ8V.o:(.eh_frame+0x11): undefined reference to 
> `__gxx_personality_v0'
> collect2: ld returned 1 exit status



More information about the vtkusers mailing list